求一个二进制串中连续的1或连续的0出现的最大次数
求一个二进制串中连续的1或连续的0出现的最大次数。例如:s = “11100001111100011” 则返回:5
又例如:s=”01111000” 则返回:4
public static void main(String[] args) { // TODO Auto-generated method stub String s="10000011110000011111111111111111100000111001"; System.out.println(getMaxLength(s)); } public static int getMaxLength(String s){ StringTokenizer stk=new StringTokenizer(s, "0"); int l=0; while(stk.hasMoreTokens()){ String temp=stk.nextToken(); l=l>temp.length()?l:temp.length(); } stk=new StringTokenizer(s, "1"); while(stk.hasMoreTokens()){ String temp=stk.nextToken(); l=l>temp.length()?l:temp.length(); } return l; }
public static void main(String[] args) { String b = "10000011110000011111111111111111100000111001"; int max = 0; for (int i = 1, j = 1; i < b.length(); ++i) if (b.charAt(i - 1) == b.charAt(i)) ++j; else { max = Math.max(max, j); j = 1; } System.out.println(max == 0 ? b.length() : max); }