977. 有序数组的平方
题目链接:LeetCode - The World's Leading Online Programming Learning Platform
思路:知道用双指针做题,也做出来了。但是先找到的中间值再去从中间从小到大找到了两边。多了很多判断条件并且跑了三次才跑通。
总结
要学会逆向思维,从两边往中间,从大到小来得到数组
Java中&& 和& 的区别:
&& 得到了结果就会停止执行operands
&执行完全部operands
执行方向:从左向右
边界条件应该写在&&的第一个避免之后的代码执行时out of index
class Solution {
public int[] sortedSquares(int[] nums) {
int[] res = new int[nums.length];
// int start = 0;
// while(start<nums.length&&nums[start]<0){ // || stop excute if get the result | will excute all the operands
// start++;
// }
// int left = start-1, right = start;
// int index = 0;
// while(index<nums.length){
// if(right==nums.length){
// res[index++]=(int)Math.pow(nums[left--],2);
// continue;
// }
// if(left==-1){
// res[index++]=(int)Math.pow(nums[right++],2);
// continue;
// }
// if(Math.abs(nums[left])<nums[right])res[index++]=(int)Math.pow(nums[left--],2);
// else res[index++]=(int)Math.pow(nums[right++],2);
// }
// return res;
int left = 0, right = nums.length-1;
int index = nums.length-1;
while(left<=right){
if(Math.abs(nums[left])>Math.abs(nums[right])){
res[index]=(int)Math.pow(nums[left++],2);
}else{
res[index]=(int)Math.pow(nums[right--],2);
}
index--;
}
return res;
}
}
209.长度最小的子数组
题目链接:https://leetcode.com/problems/minimum-size-subarray-sum/
第一想法是动态规划,有个类似的子数组的题目,但是后来看了下全部是正数,好像也没那么麻烦。用滑动窗口解决就好。滑动窗口的框架不是很熟悉,这个题目相对简单,还是能写,需要复习。
暴力算法外层循环是循环的起始位置
滑动窗口其实是通过在外层循环来遍历终止位置,再根据窗口条件来移动起始位置
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int left = 0, right = 0;
int sum = 0;
int res = Integer.MAX_VALUE;
while(right<nums.length){ //[left,right)
sum+=nums[right];//add right num into window first
right++;//shift the right index;
while(left<=right&&sum>=target){
res = Math.min(res,right-left);//right 先动在算,left先算再动;所以[left,right)
sum-=nums[left];
left++;
}
}
return res==Integer.MAX_VALUE?0:res;//read the question
}
}
59.螺旋矩阵II
循环不变量原则,避免发生错误
坚持了每条边左闭右开的原则
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;
}
}
//取巧办法,用四个bond
class Solution {
public int[][] generateMatrix(int n) {
int upperbound = 0, leftbound = 0, lowerbound = n-1, rightbound = n-1;
int count = 1;
int[][] res = new int[n][n];
while(count<=n*n){
for(int i=leftbound;i<=rightbound;i++){
res[upperbound][i]=count++;
}
upperbound++;
for(int i = upperbound;i<=lowerbound;i++){
res[i][rightbound] = count++;
}
rightbound--;
for(int i=rightbound;i>=leftbound;i--){
res[lowerbound][i]=count++;
}
lowerbound--;
for(int i=lowerbound;i>=upperbound;i--){
res[i][leftbound]=count++;
}
leftbound++;
}
return res;
}
}
loop控制边界(第几圈)
start 是起点; i j 需要是全局变量
中心点通过奇偶讨论