力扣5 :最長回文數串
阿新 • • 發佈:2019-03-19
超時 public subst ali bsp 暴力窮舉 charat solution pen
方法一:暴力窮舉。顯然超時了。90/103。
class Solution { public boolean isHuiwen(String s){ for(int i = 0,j = s.length()-1;i <= j;i++,j--){ if(s.charAt(i)!=s.charAt(j)) return false; } return true; } public String longestPalindrome(String s) { StringBuffer list = new StringBuffer(); if(s == null || s.length() == 0) return ""; if(s.length() == 1) return s; for(int i = 0; i < s.length();i++){ for(int j = i+1; j < s.length();j++ ){ if(isHuiwen(s.substring(i,j+1))){ if(s.substring(i,j+1).length() > list.length()){ list = list.delete(0,list.length()); list.append(s.substring(i,j+1)); } } } } if(list.length()==0){ return new Character(s.charAt(0)).toString(); }else{ return list.toString(); } } }
力扣5 :最長回文數串