
数组
红茶晚报丶
在校大学生,正在勤奋中
展开
-
力扣-->#剑指Offer 206.反转链表
方法一:双指针(1)定义一个指针cur指向头结点,再定义一个指针pre指向null,然后开始反转(2)在反转之前需要保存cur.next指针,不然就找不到了class Solution { public ListNode reverseList(ListNode head) { if(head==null) return head; ListNode pre=null; ListNode cur=head; List..原创 2022-03-23 15:35:29 · 127 阅读 · 0 评论 -
力扣-->#剑指Offer 54. 螺旋矩阵
这个矩阵有可能是长方形的,但是和正方形的螺旋矩阵差不多,只需要注意两点:即跳出循环的条件以及For循环的条件(1)他是矩阵,先创建二维数组new int[m][n];(2)数字是从左到右,从上到下,从右到左,从下到上的顺序(顺时针)(3)从左到右的时候,二维数组的第一个参数不变,第二个参数变(4)从上到下的时候,二维数组的第一个参数变, 第二个参数不变(5)从右到左的时候,二维数组的第一个参数不变,第二个参数变(6)从下到上的时候,二维数组的第一个参数变, 第二个参数不变(.原创 2022-03-22 16:14:57 · 127 阅读 · 0 评论 -
力扣-->#剑指Offer 59. 螺旋矩阵 II组
解题思路:(1)他是矩阵,先创建二维数组new int[n][n];(2)数字是从左到右,从上到下,从右到左,从下到上的顺序(顺时针)(3)从左到右的时候,二维数组的第一个n不变,第二个n变(4)从上到下的时候,二维数组的第一个n变, 第二个n不变(5)从右到左的时候,二维数组的第一个n不变,第二个n变(6)从下到上的时候,二维数组的第一个n变, 第二个n不变class Solution { public int[][] generateMatrix(int n) ..原创 2022-03-13 20:52:45 · 347 阅读 · 0 评论 -
力扣-->#剑指Offer 209. 长度最小的子数组
方法一:蛮力法:通过两层for循环直接比较时间复杂度:$O(n^2)$ 空间复杂度:$O(1)$class Solution { public int minSubArrayLen(int target, int[] nums) { int result = Integer.MAX_VALUE; int sum=0; int subLength=0; int length=nums.length; for原创 2022-03-12 21:05:03 · 152 阅读 · 0 评论 -
力扣-->#剑指Offer 977. 有序数组的平方
方法一:蛮力法,把数组的所有树平方,再排序class Solution { public int[] sortedSquares(int[] nums) { int length=nums.length; for(int i=0;i<length;i++){ nums[i]=nums[i]*nums[i]; } Arrays.sort(nums); return nums; ..原创 2022-03-12 20:56:47 · 82 阅读 · 0 评论 -
力扣-->#剑指Offer 169. 多数元素
class Solution { public int majorityElement(int[] nums) { int length=nums.length; // Arrays.sort(nums); int count=1,cand_num=nums[0]; for(int i=0;i<length;i++){ if(cand_num==nums[i]){ ...转载 2022-03-09 21:05:58 · 86 阅读 · 0 评论 -
力扣-->#剑指Offer 118.杨辉三角
class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> rest=new ArrayList<List<Integer>>(); for(int i=0;i<numRows;i++){ List<Integer> ...原创 2022-03-05 17:30:23 · 165 阅读 · 0 评论 -
力扣-->#剑指Offer 566. 重塑矩阵
思路(1)二维数组的行列是我们需要第一确认的数(2)通过rPos和cPos两个下标来确定新的二维数组class Solution { public int[][] matrixReshape(int[][] mat, int r, int c) { int rowLength=mat.length; int colLength=mat[0].length; if(rowLength*colLength!=r*c){ ...原创 2022-03-05 16:49:05 · 152 阅读 · 0 评论 -
力扣-->#剑指Offer 350. 两个数组的交集 II
解题思路:(1)首先需要给两个数组排序,方便从小到大进行比较(2)因为 元素的出现次数不一致的话需要取最小值,所以不可以单纯的使用for循环来判断数组的下边,因为for循环的下标不容易更改,需要用index1,index2分别来记录两个数组的下表(3)最重要的一点!!!!!!copyOfRange函数的使用!!!因为我们一开始固定好了数组的长度,如果我们的并集的结果 没有达到数组的长度,其他的默认值为0,所以我们需要把这些默认值去除掉Java中copyOfRange()函数深度理解..原创 2022-03-04 15:07:34 · 318 阅读 · 0 评论