劍指Offer之Java算法習(xí)題精講數(shù)組與字符串
更新時間:2022年03月18日 15:18:42 作者:明天一定.
跟著思路走,之后從簡單題入手,反復(fù)去看,做過之后可能會忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會發(fā)現(xiàn)質(zhì)的變化
題目一

?解法
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<nums.length){
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<right){
if(s.charAt(left)==s.charAt(right)){
left++;
right--;
}else{
String tmp = s.substring(left, right + 1);
return validPalindrome(tmp,1,tmp.length()-1)||validPalindrome(tmp,0,tmp.length()-2);
}
}
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<k;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<nums.length;i++){
n[nums[i]]+=1;
}
int max = Integer.MIN_VALUE;
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 0;i<n.length;i++){
max = Math.max(n[i],max);
}
for(int i = 0;i<n.length;i++){
if(n[i]==max){
list.add(i);
}
}
int min = Integer.MAX_VALUE;
for(int i = 0;i<list.size();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;
}
}
到此這篇關(guān)于劍指Offer之Java算法習(xí)題精講數(shù)組與字符串的文章就介紹到這了,更多相關(guān)Java 數(shù)組內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- 劍指Offer之Java算法習(xí)題精講數(shù)組查找與字符串交集
- 劍指Offer之Java算法習(xí)題精講鏈表與字符串及數(shù)組
- 劍指Offer之Java算法習(xí)題精講數(shù)組與字符串題
- 劍指Offer之Java算法習(xí)題精講N叉樹的遍歷及數(shù)組與字符串
- 劍指Offer之Java算法習(xí)題精講數(shù)組與列表的查找及字符串轉(zhuǎn)換
- 劍指Offer之Java算法習(xí)題精講字符串操作與數(shù)組及二叉搜索樹
- 關(guān)于java數(shù)組與字符串相互轉(zhuǎn)換的問題
- java 字符串轉(zhuǎn)化為字符數(shù)組的3種實現(xiàn)案例
- Java數(shù)組與字符串深入探索使用方法
相關(guān)文章
解決子線程中獲取不到HttpServletRequest對象的問題
這篇文章主要介紹了解決子線程中獲取不到HttpServletRequest對象的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
Springboot視圖解析器ViewResolver使用實例
這篇文章主要介紹了Springboot視圖解析器ViewResolver使用實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
使用jpa的實體對象轉(zhuǎn)json符串時懶加載的問題及解決
這篇文章主要介紹了使用jpa的實體對象轉(zhuǎn)json符串時懶加載的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02

