题解 | #Where in 和Not in#
最长回文子串
http://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); String str = in.nextLine(); int len = str.length(); int max = 1; for(int i = 0;i < len; i++){ int len1 = findLongest(str,i,i); int len2 = findLongest(str,i,i+1);
max = Math.max(max,Math.max(len1,len2));
}
System.out.println(max);
}
public static int findLongest(String str,int left,int right){
int len = str.length();
int max = 1;
while(left >=0 && right < len && str.charAt(left) == str.charAt(right)){
max = Math.max(max,right - left +1);
left -- ;
right ++;
}
return max;
}
}
我居南半坡 文章被收录于专栏
多刷题,积蓄力量,欢迎讨论