天津星创网站建设有限公司微信小商店分销系统
news/
2025/9/24 21:01:32/
文章来源:
天津星创网站建设有限公司,微信小商店分销系统,济南建设网点电话,旅游手机网站建设977. 有序数组的平方y
思路#xff0c;原数组是有序的#xff0c;但是因为负数平方后可能变无序了#xff0c;因此利用双指针遍历原数组#xff0c;比较
nums[left]*nums[left]和nums[right]*nums[right]谁更大#xff0c;然后对新数组赋值
class Solution {public int…977. 有序数组的平方y
思路原数组是有序的但是因为负数平方后可能变无序了因此利用双指针遍历原数组比较
nums[left]*nums[left]和nums[right]*nums[right]谁更大然后对新数组赋值
class Solution {public int[] sortedSquares(int[] nums) {//双指针非递减新建一个数组int right nums.length - 1;int left 0;int[] resnew int[nums.length];int indexres.length-1;//指向新数组res的最后一位while(leftright){if(nums[left]*nums[left]nums[right]*nums[right]){res[index]nums[left]*nums[left];index--;left;}else{res[index]nums[right]*nums[right];index--;right--;}}return res;}
}螺旋矩阵 II
没有思路参考的题解Spiral Matrix II 模拟法设定边界代码简短清晰 - 螺旋矩阵 II - 力扣LeetCode
思路
4个方向左右顶底
从左到右
从顶到底
从右到左
从底到顶 class Solution {public int[][] generateMatrix(int n) {int[][] resnew int[n][n];int l 0, r n - 1, t 0, b n - 1;//左右顶底int num 1, tar n * n;while(numn*n){//left to rightfor(int il;ir;i){res[t][i]num;num;}t;//top to bottomfor(int it;ib;i){res[i][r]num;num;}r--;//right to leftfor(int ir;il;i--){res[b][i]num;num;}b--;//bottom to topfor(int ib;it;i--){res[i][l]num;num;}l;}return res;}
}
209. 长度最小的子数组
滑动窗口需要有个slow指针控制窗口左侧边界fast控制窗口右侧边界,如果sumtarget那就收缩左侧边界
class Solution {public int minSubArrayLen(int target, int[] nums) {//给定一个含有 n 个正整数的数组和一个正整数 target 。int sum0;int count0;int slow0;for(int fast0;fastnums.length;fast){sumnums[i];while(sumtarget){// System.out.println(count);//4countcount0? fast-slow1: Math.min(count,fast-slow1);sum-nums[slow];slow;}}return count;}
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/916232.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!