关注
有点问题需要考虑等于情况 public class Solution { public int[] LIS (int[] arr) { int[] nums = new int[arr.length]; int[] temp = new int[arr.length]; nums[0] = 1; int tempIndex = 0; temp[tempIndex] = arr[0]; for (int i = 1; i < arr.length; ++i) { int left = 0, right = tempIndex; if (arr[i] > temp[tempIndex]) { ++tempIndex; nums[i] = tempIndex + 1; temp[tempIndex] = arr[i]; } else { while (left <= right) { // 注意这里 left <= right 而不是 left < right,我们要替换的是第一个比 arr[i] 大的元素 int mid = (right + left) / 2; if (temp[mid] > arr[i]) { right = mid - 1; } else { left = mid + 1; } } if(left>0&&temp[left-1]==arr[i]){ left--; } temp[left] = arr[i]; nums[i] = left + 1; } } int[] res = new int[tempIndex + 1]; for (int i = nums.length - 1; i >= 0; --i) { if (nums[i] == tempIndex + 1) { res[tempIndex] = arr[i]; --tempIndex; } } return res; } }
点赞
相关推荐
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 牛客新年AI问运 #
7499次浏览 113人参与
# 你喜欢工作还是上学 #
89308次浏览 883人参与
# 牛客AI体验站 #
16479次浏览 288人参与
# 你找工作的时候用AI吗? #
173230次浏览 888人参与
# 被AI治愈的瞬间 #
90548次浏览 685人参与
# 有必要和同事成为好朋友吗? #
1184次浏览 23人参与
# 听劝,这个公司值得去吗 #
665281次浏览 1996人参与
# 为了秋招你都做了哪些准备? #
32587次浏览 534人参与
# 这个工作能去吗 #
115044次浏览 662人参与
# 多益网络工作体验 #
63242次浏览 306人参与
# 秋招吐槽大会 #
304563次浏览 1524人参与
# 你觉得什么岗位会被AI替代 #
41066次浏览 275人参与
# 工作中的卑微时刻 #
33500次浏览 199人参与
# 数字马力求职进展汇总 #
331639次浏览 2381人参与
# 非技术岗薪资爆料 #
490427次浏览 3041人参与
# 我们是不是被“优绩主义”绑架了? #
32825次浏览 487人参与
# 产品面经 #
261209次浏览 2173人参与
# 如何提高实习转正率? #
86708次浏览 505人参与
# 大学最后一个寒假,我想…… #
89370次浏览 809人参与
# 正在实习的碎碎念 #
1645468次浏览 13716人参与

