Java十大经典排序算法的实现图解
279
2022-08-19
剑指Offer之Java算法习题精讲数组与字符串
题目一
解法
class Solution {
public int findLengthOfLCIS(int[] nums) {
if(nums.length==1) return 1;
int fast = 1;
int tmp = 1;
int max = Integer.MIN_VALUE;
while(fast if(nums[fast]>nums[fast-1]){ tmp++; max = Math.max(max,tmp); }else{ max = Math.max(max,tmp); tmp = 1; } fast++; } return max; } } 题目二 解法 class Solution { public boolean validPalindrome(String s) { int left = 0; int right = s.length()-1; while(left if(s.charAt(left)==s.charAt(right)){ left++; right--; }else{ JwBpPoS String tmp = s.substring(left, right + 1); return validPalindrome(tmp,1,tmp.length()-1)||validPalindrome(tmp,0,tmp.length()-2); } } JwBpPoS return true; } public boolean validPalindrome(String s, int low, int high) { for (int i = low, j = high; i < j; ++i, --j) { char c1 = s.charAt(i), c2 = s.charAt(j); if (c1 != c2) { return false; } } return true; } } 题目三 解法 class Solution { public double findMaxAverage(int[] nums, int k) { int w = nums.length-k; int max = Integer.MIN_VALUE; for(int i = 0;i<=w;i++){ int res = 0; for(int j = 0;j res = nums[i+j]+res; } max = Math.max(max,res); } double ans = (double)max/k; return ans; } } 题目四 解法 class Solution { public int findShortestSubArray(int[] nums) { int[] n = new int[50001]; for(int i = 0;i n[nums[i]]+=1; } int max = Integer.MIN_VALUE; ArrayList for(int i = 0;i max = Math.max(n[i],max); } for(int i = 0;i if(n[i]==max){ list.add(i); } } int min = Integer.MAX_VALUE; for(int i = 0;i int res = list.get(i); int left = 0; int right = nums.length-1; while(nums[left]!=res){ left++; } while(nums[right]!=res){ right--; } min = Math.min(min,right-left+1); } return min; } }
if(nums[fast]>nums[fast-1]){
tmp++;
max = Math.max(max,tmp);
}else{
max = Math.max(max,tmp);
tmp = 1;
}
fast++;
}
return max;
}
}
题目二
解法
class Solution {
public boolean validPalindrome(String s) {
int left = 0;
int right = s.length()-1;
while(left if(s.charAt(left)==s.charAt(right)){ left++; right--; }else{ JwBpPoS String tmp = s.substring(left, right + 1); return validPalindrome(tmp,1,tmp.length()-1)||validPalindrome(tmp,0,tmp.length()-2); } } JwBpPoS return true; } public boolean validPalindrome(String s, int low, int high) { for (int i = low, j = high; i < j; ++i, --j) { char c1 = s.charAt(i), c2 = s.charAt(j); if (c1 != c2) { return false; } } return true; } } 题目三 解法 class Solution { public double findMaxAverage(int[] nums, int k) { int w = nums.length-k; int max = Integer.MIN_VALUE; for(int i = 0;i<=w;i++){ int res = 0; for(int j = 0;j res = nums[i+j]+res; } max = Math.max(max,res); } double ans = (double)max/k; return ans; } } 题目四 解法 class Solution { public int findShortestSubArray(int[] nums) { int[] n = new int[50001]; for(int i = 0;i n[nums[i]]+=1; } int max = Integer.MIN_VALUE; ArrayList for(int i = 0;i max = Math.max(n[i],max); } for(int i = 0;i if(n[i]==max){ list.add(i); } } int min = Integer.MAX_VALUE; for(int i = 0;i int res = list.get(i); int left = 0; int right = nums.length-1; while(nums[left]!=res){ left++; } while(nums[right]!=res){ right--; } min = Math.min(min,right-left+1); } return min; } }
if(s.charAt(left)==s.charAt(right)){
left++;
right--;
}else{
JwBpPoS String tmp = s.substring(left, right + 1);
return validPalindrome(tmp,1,tmp.length()-1)||validPalindrome(tmp,0,tmp.length()-2);
}
}
JwBpPoS return true;
}
public boolean validPalindrome(String s, int low, int high) {
for (int i = low, j = high; i < j; ++i, --j) {
char c1 = s.charAt(i), c2 = s.charAt(j);
if (c1 != c2) {
return false;
}
}
return true;
}
}
题目三
解法
class Solution {
public double findMaxAverage(int[] nums, int k) {
int w = nums.length-k;
int max = Integer.MIN_VALUE;
for(int i = 0;i<=w;i++){
int res = 0;
for(int j = 0;j res = nums[i+j]+res; } max = Math.max(max,res); } double ans = (double)max/k; return ans; } } 题目四 解法 class Solution { public int findShortestSubArray(int[] nums) { int[] n = new int[50001]; for(int i = 0;i n[nums[i]]+=1; } int max = Integer.MIN_VALUE; ArrayList for(int i = 0;i max = Math.max(n[i],max); } for(int i = 0;i if(n[i]==max){ list.add(i); } } int min = Integer.MAX_VALUE; for(int i = 0;i int res = list.get(i); int left = 0; int right = nums.length-1; while(nums[left]!=res){ left++; } while(nums[right]!=res){ right--; } min = Math.min(min,right-left+1); } return min; } }
res = nums[i+j]+res;
}
max = Math.max(max,res);
}
double ans = (double)max/k;
return ans;
}
}
题目四
解法
class Solution {
public int findShortestSubArray(int[] nums) {
int[] n = new int[50001];
for(int i = 0;i n[nums[i]]+=1; } int max = Integer.MIN_VALUE; ArrayList for(int i = 0;i max = Math.max(n[i],max); } for(int i = 0;i if(n[i]==max){ list.add(i); } } int min = Integer.MAX_VALUE; for(int i = 0;i int res = list.get(i); int left = 0; int right = nums.length-1; while(nums[left]!=res){ left++; } while(nums[right]!=res){ right--; } min = Math.min(min,right-left+1); } return min; } }
n[nums[i]]+=1;
}
int max = Integer.MIN_VALUE;
ArrayList
for(int i = 0;i max = Math.max(n[i],max); } for(int i = 0;i if(n[i]==max){ list.add(i); } } int min = Integer.MAX_VALUE; for(int i = 0;i int res = list.get(i); int left = 0; int right = nums.length-1; while(nums[left]!=res){ left++; } while(nums[right]!=res){ right--; } min = Math.min(min,right-left+1); } return min; } }
max = Math.max(n[i],max);
}
for(int i = 0;i if(n[i]==max){ list.add(i); } } int min = Integer.MAX_VALUE; for(int i = 0;i int res = list.get(i); int left = 0; int right = nums.length-1; while(nums[left]!=res){ left++; } while(nums[right]!=res){ right--; } min = Math.min(min,right-left+1); } return min; } }
if(n[i]==max){
list.add(i);
}
}
int min = Integer.MAX_VALUE;
for(int i = 0;i int res = list.get(i); int left = 0; int right = nums.length-1; while(nums[left]!=res){ left++; } while(nums[right]!=res){ right--; } min = Math.min(min,right-left+1); } return min; } }
int res = list.get(i);
int left = 0;
int right = nums.length-1;
while(nums[left]!=res){
left++;
}
while(nums[right]!=res){
right--;
}
min = Math.min(min,right-left+1);
}
return min;
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~