10 2021 档案

摘要:这道题可以用贪心算法和动态规划来解,贪心算法思路:先按照每个数组最右值大小排序,我们需要尽量找右边值最小数组作为保留的数组(这样可以留下更多数组),然后在将当前数组的右值和下一个数组的最左值比较。如果合适则加入。动态规划的思路:找以每个区间作为结尾的最大区间数。 public int eraseOv 阅读全文
posted @ 2021-10-25 22:16 毅毅毅毅毅 阅读(62) 评论(0) 推荐(0)
摘要:这道题可以直接暴力法解出答案,但是如果对时间有要求,可以对每一行使用二分进行查找。 public boolean searchMatrix(int[][] matrix, int target) { for (int[] row : matrix) { int index = search(row, 阅读全文
posted @ 2021-10-25 00:49 毅毅毅毅毅 阅读(55) 评论(0) 推荐(0)
摘要:这道题最初没有什么思路,所以算是按暴力来求解,将数组分层,一圈一圈的循环计算,从外圈到内圈。 public int[][] generateMatrix(int n) { int level=n%2==0?n/2:n/2+1; int temp=1; int [][] res=new int [n] 阅读全文
posted @ 2021-10-21 17:20 毅毅毅毅毅 阅读(49) 评论(0) 推荐(0)
摘要:这道题属于Easy题,要求到目标行数,只需要一直递归上一行的内容即可。 public List<Integer> getRow(int rowIndex) { List<Integer> res=new LinkedList<>(); if(rowIndex==0) { res.add(1); re 阅读全文
posted @ 2021-10-19 20:03 毅毅毅毅毅 阅读(49) 评论(0) 推荐(0)
摘要:在mysql8 之前的版本,因为没有rank()方法的存在,所以在对字段进行排名时,使用的是自定义自变量的方法,比如: select id,name,@rank:=@rank+1 as ranks from user u, (select @rank:=0) ran order by u.age 自 阅读全文
posted @ 2021-10-16 03:26 毅毅毅毅毅 阅读(467) 评论(0) 推荐(0)
摘要:这道题如果不考虑进阶方法,可以很轻松的使用排序直接得到答案: public void sortColors(int[] nums) { Arrays.sort(nums); } 但如果需要按照进阶的要求,不允许使用自带排序并要求一次遍历,我们可以用双指针进行求解。我们通过两个指针,pre0和pre1 阅读全文
posted @ 2021-10-13 22:12 毅毅毅毅毅 阅读(97) 评论(0) 推荐(0)
摘要:最简单的方法就是:直接排序然后取中间的值,两行搞定 public int majorityElement(int[] nums) { Arrays.sort(nums); return nums[nums.length/2]; } 阅读全文
posted @ 2021-10-12 12:47 毅毅毅毅毅 阅读(87) 评论(0) 推荐(0)
摘要:这题如果用一般数据结构来解决,存在很多种解法,比如:通过hashmap记录出现的次数,最后遍历得到次数为1的数。或者通过hashset添加数字,如果无法添加就删除该数字。 public int singleNumber(int[] nums) { Set<Integer> hset=new Hash 阅读全文
posted @ 2021-10-08 11:21 毅毅毅毅毅 阅读(55) 评论(0) 推荐(0)