classSolution{publicintsearch(int[] nums,int target){int l =0, r = nums.length -1, m;while(l <= r){
m =(l + r)>>>1;if(nums[m]< target){
l = m +1;}elseif(nums[m]> target){
r = m -1;}else{return m;}}return-1;}}classSolution{publicintsearch(int[] nums,int target){int l =0, r = nums.length, m;while(l < r){
m =(l + r)>>>1;if(nums[m]< target){
l = m +1;}elseif(nums[m]> target){
r = m;}else{return m;}}return-1;}}
35.搜索插入位置
classSolution{publicintsearchInsert(int[] nums,int target){int l =0, r = nums.length -1, m;while(l <= r){
m =(l + r)>>>1;if(nums[m]< target){
l = m +1;}elseif(nums[m]> target){
r = m -1;}else{return m;}}return l;}}classSolution{publicintsearchInsert(int[] nums,int target){int l =0, r = nums.length, m;while(l < r){
m =(l + r)>>>1;if(nums[m]< target){
l = m +1;}elseif(nums[m]> target){
r = m;}else{return m;}}return l;}}
27.移除元素
classSolution{publicintremoveElement(int[] nums,int val){int s =0;for(int f =0; f < nums.length; f++){if(nums[f]!= val){
nums[s]= nums[f];
s++;}}return s;}}
26. 删除有序数组中的重复项
classSolution{publicintremoveDuplicates(int[] nums){if(nums.length ==1)return1;int s =0;for(int f =1; f < nums.length; f++){if(nums[f]!= nums[s]){
s++;
nums[s]= nums[f];}}return++s;}}
283. 移动零
classSolution{publicvoidmoveZeroes(int[] nums){int s =0;for(int f =0; f < nums.length; f++){if(nums[f]!=0){
nums[s]= nums[f];
s++;}}while(s < nums.length){
nums[s]=0;
s++;}}}classSolution{publicvoidmoveZeroes(int[] nums){int s =0;for(int f =0; f < nums.length; f++){if(nums[f]!=0){int t = nums[f];
nums[f]= nums[s];
nums[s]= t;
s++;}}}}
classSolution{publicint[]sortedSquares(int[] nums){int l =0, r = nums.length -1;int[] res =newint[nums.length];for(int i = nums.length -1; i >=0; i--){if(nums[l]* nums[l]> nums[r]* nums[r]){
res[i]= nums[l]* nums[l];
l++;}else{
res[i]= nums[r]* nums[r];
r--;}}return res;}}
209. 长度最小的子数组
classSolution{publicintminSubArrayLen(int target,int[] nums){int sum =0, j =0, res =Integer.MAX_VALUE;for(int i =0; i < nums.length; i++){
sum += nums[i];while(sum >= target){
res =Math.min(res, i - j +1);
sum -= nums[j];
j++;}}return res ==Integer.MAX_VALUE?0: res;}}
904. 水果成篮
classSolution{publicinttotalFruit(int[] fruits){int left =0, res =0;Map<Integer,Integer> map =newHashMap();// 哈希表,记录元素出现次数for(int right =0; right < fruits.length; right++){// 快指针遍历数组
map.put(fruits[right], map.getOrDefault(fruits[right],0)+1);// 每遍历一个元素,把它在map中的v+1while(map.size()>2){// 一旦size>2 说明种类超过了两个,应该开始从左边开始删除元素
map.put(fruits[left], map.get(fruits[left])-1);// 从左边开始删除元素,这里一定能get到if(map.get(fruits[left])==0){// 因为一旦元素v为0,就remove
map.remove(fruits[left]);}
left++;// 删除完别忘了移动慢指针}
res =Math.max(res, right - left +1);// 外层循环每进行一次,都要更新一下res}return res;// 最后返回res}}
76. 最小覆盖子串
classSolution{publicStringminWindow(String s,String t){int left =0, res =Integer.MAX_VALUE, start =0;int[] countS =newint[128];// 创建两个数组作为哈希表int[] countT =newint[128];for(char c : t.toCharArray()){
countT[c]++;// 遍历T数组,得到哈希数组}for(int right =0; right < s.length(); right++){
countS[s.charAt(right)]++;// 快指针走一步,数组更新一次while(check(countS, countT)){// 一旦check成功,说明s这部分包含了t,可能要更新res,进入内层循环if(right - left +1< res){
res = right - left +1;
start = left;// 更新的时候记录start位置}
countS[s.charAt(left)]--;// 更新完left指针移动
left++;}}return res ==Integer.MAX_VALUE?"": s.substring(start, start + res);}// check函数的逻辑,一旦发现countT某个位置元素比S大,就返回false,要么比你多要么你没有privatebooleancheck(int[] countS,int[] countT){for(int i =0; i <128; i++){if(countS[i]< countT[i]){returnfalse;}}returntrue;}}
59.螺旋矩阵II
classSolution{publicint[][]generateMatrix(int n){int l =0, r = n -1, b =0, t = n -1, num =1;int[][] res =newint[n][n];while(num <= n * n){for(int i = l; i <= r; i++){
res[b][i]= num++;}
b++;for(int i = b; i <= t; i++){
res[i][r]= num++;}
r--;for(int i = r; i >= l; i--){
res[t][i]= num++;}
t--;for(int i = t; i >= b; i--){
res[i][l]= num++;}
l++;}return res;}}
publicclassSolution{publicListNodegetIntersectionNode(ListNode headA,ListNode headB){ListNode tA = headA;ListNode tB = headB;while(tA != tB){
tA = tA ==null? headB : tA.next;
tB = tB ==null? headA : tB.next;}return tA;}}
142.环形链表Ⅱ
publicclassSolution{publicListNodedetectCycle(ListNode head){ListNode f = head, s = head;while(f !=null&& f.next !=null){
f = f.next.next;
s = s.next;if(f == s){
f = head;while(f != s){
f = f.next;
s = s.next;}return f;}}returnnull;}}