Example 1:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd"
Output: "bb"
class Solution { public String longestPalindrome(String s) { if(s.length()==1)return s; int max=0; String res=new String(); for (int i=0;i<s.length();i++){ int L=i;//aba int R=i; String temp=getPlength(s,L,R); if(temp.length()>max){ max=temp.length(); res=temp; } if(i !=s.length()-1){ L=i;R=i+1;//abba temp=getPlength(s,L,R); if (temp.length()>max){ max=temp.length(); res=temp; } } } return res; } private String getPlength(String s,int L,int R){ while (L>=0 && R<s.length() && s.charAt(L)==s.charAt(R)){ L--;R++; } return s.substring(L+1,R); } }
11. Maximum area of container
/*Use two pointers to draw close to the middle from both ends. If the left end segment is shorter than the right end, move the left end to the right. Otherwise, move the right end to the left until the left and right ends move to the middle and coincide. Record the volume of each composition barrel in this process and return the largest one. When the left end segment L is smaller than the right end segment R, we move l to the right. At this time, we discard the barrels composed of L and other right end segments (R-1, R-2,...). It is unnecessary to judge these barrels, because the volume of these barrels is certainly not as large as that of L and r. * /
import java.util.*; class Solution { public int maxArea(int[] height) { int max=0; int len=height.length; int left=0; int right=len-1; while (left<right){ max=Math.max(max,Math.min(height[left],height[right])*(right-left)); if(height[left]<height[right]){ left++; }else { right--; } } return max; } }