classSolution { publicintsearch(int[] nums, int target) { int left=0; int right=nums.length-1; while (left<=right){ intmid=(left+right)/2; if (nums[mid]==target){ return mid; } if (target>nums[mid]){ left=mid+1; }else{ right=mid-1; } } return -1; } }
classSolution { publicint[] sortedSquares(int[] nums) { int length=nums.length; int []array=newint[length]; int right=array.length-1; int flag=array.length-1; int left=0; while (right>=left) { if (Math.abs(nums[right]) >= Math.abs(nums[left])) { array[flag]=nums[right]*nums[right]; flag--; right--; } else { array[flag]=nums[left]*nums[left]; flag--; left++; }
publicintminSubArrayLen(int target, int[] nums) { int slowIndex; int fastIndex=0; int result=nums.length; int sum=0; boolean flag=false; for (slowIndex=0;fastIndex<nums.length;fastIndex++){ sum=sum+nums[fastIndex]; if (sum>=target&&result>=fastIndex-slowIndex+1){ flag=true; sum=sum-nums[slowIndex]-nums[fastIndex]; result=fastIndex-slowIndex+1; slowIndex++; fastIndex--; }
} if(flag){ return result; }else{ return0; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution {
// 滑动窗口 publicintminSubArrayLen(int s, int[] nums) { intleft=0; intsum=0; //利用result的赋值巧妙赋值避开result=num.length的错误 intresult= Integer.MAX_VALUE; for (intright=0; right < nums.length; right++) { sum += nums[right]; while (sum >= s) { result = Math.min(result, right - left + 1); sum -= nums[left++]; } } returnresult== Integer.MAX_VALUE ? 0 : result; } }