有点问题需要考虑等于情况 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; } }
点赞

相关推荐

钱嘛数字而已:辅导员肯定不能同意,不然你出事了,他要承担责任。但是,脚和脑子都长在你自己身上,使用它还需要向辅导员报告么? 辅导员必须按流程拒绝你,然后你拿出成年人的态度,做自己的选择。
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务