LeetCode之Next Greater Element I

举报
chenyu 发表于 2021/07/27 00:24:10 2021/07/27
【摘要】   1、题目 You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corr...

 

1、题目


   
  1. You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.
  2. The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.
  3. Example 1:
  4. Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
  5. Output: [-1,3,-1]
  6. Explanation:
  7. For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
  8. For number 1 in the first array, the next greater number for it in the second array is 3.
  9. For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
  10. Example 2:
  11. Input: nums1 = [2,4], nums2 = [1,2,3,4].
  12. Output: [3,-1]
  13. Explanation:
  14. For number 2 in the first array, the next greater number for it in the second array is 3.
  15. For number 4 in the first array, there is no next greater number for it in the second array, so output -1.

给你两个数组,为数组1和数组2,数组1为数组2的子集。找出数组1的每一个元素在数组2中对应的元素a,然后找到元素a后侧第一个比a大的数构成一个数组,即是我们需要的答案。如果不存在,则为-1。


   
  1. Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
  2. Output: [-1,3,-1]

 
 


2、代码实现


   
  1. public class Solution {
  2. public int[] nextGreaterElement(int[] findNums, int[] nums) {
  3. if (findNums == null || nums == null) {
  4. return null;
  5. }
  6. int findLength = findNums.length;
  7. int numsLength = nums.length;
  8. int[] result = new int[findLength];
  9. boolean flag = false;
  10. for (int i = 0; i < findLength; ++i) {
  11. for (int j = 0; j < numsLength; ++j) {
  12. if (findNums[i] == nums[j]) {
  13. if (j + 1 == numsLength) {
  14. result[i] = -1;
  15. } else {
  16. for (int k = j + 1; k < numsLength; ++k) {
  17. if (nums[j] < nums[k]) {
  18. result[i] = nums[k];
  19. flag = true;
  20. break;
  21. }
  22. }
  23. if (!flag) {
  24. result[i] = -1;
  25. }
  26. }
  27. flag = false;
  28. }
  29. }
  30. }
  31. return result;
  32. }
  33. }

 
 


3、总结我遇到的问题

我一开始写这里的代码的时候

   
  1. for (int k = j + 1; k < numsLength; ++k) {
  2. if (nums[j] < nums[k]) {
  3. result[i] = nums[k];
  4. flag = true;
  5. break;
  6. }
  7. }
忘记了写break;
然后导致第一个元素符合条件了,但是后面也有符合条件的,导致result[i],取到的元素是最后面符合条件的元素,就错了,所以,这个时候条件是说,得到第一个大于前面的数
就可以了,我们需要用break跳出循环,防穿透,防止继续便利后面的元素,所以以后编程,看到符合后面第一个元素满足要求的时候,要记得用break;形成条件反射。

 

 

 

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/65954225

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。