1.有序数组的平方
1.1暴力法
每个数平凡之后,排序
class Solution { public int[] sortedSquares(int[] nums) { int[] ans = new int[nums.length]; for (int i = 0; i < nums.length; ++i) { ans[i] = nums[i] * nums[i]; } Arrays.sort(ans); //平方后排序 return ans; } }
1.2双指针
1.这是从小到大的数组,平方后最小最大的各在两边
class Solution { public int[] sortedSquares(int[] nums) { int right = nums.length - 1; int left = 0; int[] result = new int[nums.length]; int index = result.length - 1; while (left <= right) { if (nums[left] * nums[left] > nums[right] * nums[right]) { //result[index--]把旧数组右边的值平方后放在新数组的左边并移动index右移一位, result[index--] = nums[left] * nums[left]; //再旧数组指向第二位 ++left; } else { //旧数组左边的数据放在新数组左边 -1平方=1小于4, result[index--] = nums[right] * nums[right]; //再把旧数组 右边指针前移一位 --right; } } return result; } }
数组长度为3 riht=2 left=0 result=3 index=2
class Solution { public int[] sortedSquares(int[] nums) { int l = 0; int r = nums.length - 1; int[] res = new int[nums.length]; int j = nums.length - 1; while(l <= r){ if(nums[l] * nums[l] > nums[r] * nums[r]){ res[j--] = nums[l] * nums[l++]; }else{ res[j--] = nums[r] * nums[r--]; } } return res; } }
2 长度最小的子数组
给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的 连续 子数组,并返回其长度。如果不存在符合条件的子数组,返回 0。
-
输入:s = 7, nums = [2,3,1,2,4,3]
-
输出:2
-
解释:子数组 [4,3] 是该条件下的长度最小的子数组。
2.1暴力法
class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { int result = INT32_MAX; // 最终的结果 int sum = 0; // 子序列的数值之和 int subLength = 0; // 子序列的长度 for (int i = 0; i < nums.size(); i++) { // 设置子序列起点为i sum = 0; for (int j = i; j < nums.size(); j++) { // 设置子序列终止位置为j sum += nums[j]; if (sum >= s) { // 一旦发现子序列和超过了s,更新result subLength = j - i + 1; // 取子序列的长度 result = result < subLength ? result : subLength; break; // 因为我们是找符合条件最短的子序列,所以一旦符合条件就break } } } // 如果result没有被赋值的话,就返回0,说明没有符合条件的子序列 return result == INT32_MAX ? 0 : result; } };
2.2滑动窗口
class Solution { // 滑动窗口 public int minSubArrayLen(int s, int[] nums) { int left = 0; int sum = 0; //int result = Integer.MAX_VALUE; 将 result 变量的值设置为 2,147,483,647,即整数的最大值。这行代码的作用是将 result 初始化为最大值,以便在后续的代码中使用。 int result = Integer.MAX_VALUE; // for (int right = 0; right < nums.length; right++) { //从0加起 sum += nums[right]; //如果大于等于s while (sum >= s) { //min返回a和b的小值 result = Math.min(result, right - left + 1); sum -= nums[left++]; } } //,当循环结束时,检查 result 的值是否等于 Integer.MAX_VALUE。如果是,则返回 0,否则返回 result 的值。 return result == Integer.MAX_VALUE ? 0 : result; } }
3.l螺旋矩阵
给定一个正整数 n,生成一个包含 1 到 n^2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。
示例:
输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
class Solution { public int[][] generateMatrix(int n) { int loop = 0; // 控制循环次数 int[][] res = new int[n][n]; int start = 0; // 每次循环的开始点(start, start) int count = 1; // 定义填充数字 int i, j; while (loop++ < n / 2) { // 判断边界后,loop从1开始 // 模拟上侧从左到右 for (j = start; j < n - loop; j++) { res[start][j] = count++; } // 模拟右侧从上到下 for (i = start; i < n - loop; i++) { res[i][j] = count++; } // 模拟下侧从右到左 for (; j >= loop; j--) { res[i][j] = count++; } // 模拟左侧从下到上 for (; i >= loop; i--) { res[i][j] = count++; } start++; } if (n % 2 == 1) { res[start][start] = count; } return res; } }