496. Next Greater Element I(C语言版本)
2017-07-05 16:03
330 查看
You are given two arrays (without duplicates)
elements are subset of
elements in the corresponding places of
The Next Greater Number of a number x in
the first greater number to its right in
number.
解释:题目的意思是有两个数组,nums2 为原始数组,数组nums1是nums2元素的子集,要求得到nums1中的数据,在nums2中的位置的右侧比其本身
的值要大的值 的 第一个位置。如果没有的话,值为-1
解题思路:
1 应为nums2中的整数是不相同的,所以可以定义一个hashnum数组,数组中的索引为nums1中存储的数,具体值为右侧比它大的值(最近的)
举个例子例如:
[0,7,8,9]
nums1[1] = 7; 离它最近的比它大的值为nums1[2] = 8 ,所以hashnum[nums1[1]] = nums1[2]
改进方法:
因为这个题是在stack 这个tag下,所以很自然的就想用stack去实现,但是想了很久都没有弄清要如何实现,看了题目给的solution,豁然开朗。
具体的流程,除了和上面的方法一样,需要建立 hashnum数组,还要建立一个stack,stack里面的数都是从大到小排列,依次与最新的整数比较,如果栈顶的元素小于当前值,在hashname数组相应位置插入相应的值,出栈,继续判断栈顶的值与当前值的大小,直到栈空,或者栈顶元素大于当前值。将当前值入栈。
nums1and
nums2where
nums1’s
elements are subset of
nums2. Find all the next greater numbers for
nums1's
elements in the corresponding places of
nums2.
The Next Greater Number of a number x in
nums1is
the first greater number to its right in
nums2. If it does not exist, output -1 for this
number.
解释:题目的意思是有两个数组,nums2 为原始数组,数组nums1是nums2元素的子集,要求得到nums1中的数据,在nums2中的位置的右侧比其本身
的值要大的值 的 第一个位置。如果没有的话,值为-1
解题思路:
1 应为nums2中的整数是不相同的,所以可以定义一个hashnum数组,数组中的索引为nums1中存储的数,具体值为右侧比它大的值(最近的)
举个例子例如:
[0,7,8,9]
nums1[1] = 7; 离它最近的比它大的值为nums1[2] = 8 ,所以hashnum[nums1[1]] = nums1[2]
/** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */ int* nextGreaterElement(int* findNums, int findNumsSize, int* nums, int numsSize, int* returnSize) { int* returnArray = NULL; int* numsHash = NULL; *returnSize = findNumsSize; if(findNums != NULL && findNumsSize != 0 && nums != NULL && numsSize != 0) { /* initalize the returnArray */ returnArray = malloc((*returnSize) * sizeof(int)); /* find the max element in the nums */ int max = 0; for(int i = 0; i < numsSize; i++) { if(max < nums[i]) { max = nums[i]; } } /* initialize the numsHash */ numsHash = malloc((max+1) * sizeof(int)); /* hash the nums' element to numsHash, the key is nums's element, */ /* the velue is the next greater element, or -1 if does not exist */ /* nums[numsSize-1] is the last element of nums, don't have next, */ /* so set it to -1 directly */ for(int i = 0; i < numsSize-1; i++) { numsHash[nums[i]] = -1; for(int j = i+1; j < numsSize; j++) { if(nums[j] > nums[i]) { numsHash[nums[i]] = nums[j]; break; } } } numsHash[nums[numsSize-1]] = -1; /* find the next greater element */ for(int i = 0; i < findNumsSize; i++) { returnArray[i] = numsHash[findNums[i]]; } } return returnArray; }
改进方法:
因为这个题是在stack 这个tag下,所以很自然的就想用stack去实现,但是想了很久都没有弄清要如何实现,看了题目给的solution,豁然开朗。
具体的流程,除了和上面的方法一样,需要建立 hashnum数组,还要建立一个stack,stack里面的数都是从大到小排列,依次与最新的整数比较,如果栈顶的元素小于当前值,在hashname数组相应位置插入相应的值,出栈,继续判断栈顶的值与当前值的大小,直到栈空,或者栈顶元素大于当前值。将当前值入栈。
public int[] nextGreaterElement(int[] findNums, int[] nums) { Map<Integer, Integer> map = new HashMap<>(); // map from x to next greater element of x Stack<Integer> stack = new Stack<>(); for (int num : nums) { while (!stack.isEmpty() && stack.peek() < num) map.put(stack.pop(), num); stack.push(num); } for (int i = 0; i < findNums.length; i++) findNums[i] = map.getOrDefault(findNums[i], -1); return findNums; }
相关文章推荐
- Win32 C语言版本的GUID函数
- 通讯录小项目(C语言动态版本)
- 用树莓派实现RGB LED的颜色控制——C语言版本
- 1602动态显示字符--C语言版本
- 多线程版本的Item_KNN的C语言实现
- 趣味编程:从字符串中提取信息(C语言版本)
- C语言的发展及其版本
- 堆排序(C语言版本)
- UNIX网络编程之epoll 的accept , read, write(重要) C语言版本
- PAT 数字分类 C语言版本
- 计数排序(C语言版本)
- 设计模式 行为模式 中介者模式 c语言 版本实现
- 541. Reverse String II(C语言版本)
- 编程之美 格格取数 C语言版本
- 学生信息管理系统(C语言版本)
- c语言之ls目录的简单实现和window版本dirent.h
- C语言、python、go、C#版本选择排序
- 堆排序算法的C语言和Python版本源码实现
- C语言版本--工资管理系统
- 趣味编程:从字符串中提取信息(C语言版本)