日期:2014-05-20  浏览次数:20665 次

小思路,突然短路了
输入一个二进制串0111000111100 打印出1或者0出现最大次数 比如结果为 4;
来个实现

------解决方案--------------------
Java code
public static void main(String[] args) {
        String s = "0111000111100";
        char[] c = s.toCharArray();
        int num = 1;
        int max = 1;
        for (int i = 1; i < c.length; i++) {
            if (c[i] == c[i - 1]) {
                num++;
            } else {
                max = Math.max(max, num);
                num = 1;
            }
        }
        System.out.println(max);
    }

------解决方案--------------------
Java code

public class Find01
{
    public static void main(String[] args)
    {
        String str="0111000111100";
        String str1=str.replaceAll("11*"," ");
        String str2=str.replaceAll("00*"," ");
        String[] s0=str1.split(" ");
        String[] s1=str2.split(" ");
        int num0=0;
        int num1=0;
        for(int i=0;i<s0.length;i++)
        {
            if(num0<s0[i].length())
            {
                num0=s0[i].length();
            }
        }
        System.out.println("num0 is  "+num0);
        for(int i=0;i<s1.length;i++)
        {
            if(num1<s1[i].length())
            {
                num1=s1[i].length();
            }
        }
        System.out.println("num1 is  "+num1);
            
    }
}

------解决方案--------------------
貌似这样也是可以的。。。。
Java code
package com.fenghua;

public class FindZeroOne {
    /**
     * 此方法为仅有两个字符(可多次出现)返回出现次数最多的字符的次数
     * @param str
     * @return
     */
    public static int max01(String str){
        int b1=1;
        int a1=0;
        char[] a=str.toCharArray();
        for(int i=0;i<a.length-1;i++){
            if(a[i]==a[i+1]){
                b1++;
            }else{
                a1=a1+b1;
                b1=a1-b1;
                a1=a1-b1;
                b1++;
            }
        }
        return Math.max(a1, b1);
    }
    public static void main(String[] args){
        String str="01010101000000000111111";
        System.out.println(max01(str));
    }

}

------解决方案--------------------
额 没看清楚需求。。。我检讨
探讨
貌似这样也是可以的。。。。

Java code
package com.fenghua;

public class FindZeroOne {
/**
* 此方法为仅有两个字符(可多次出现)返回出现次数最多的字符的次数
* @param str
* @return
*/
public static int max01……

------解决方案--------------------
高手!
探讨

Java code

public class Find01
{
public static void main(String[] args)
{
String str="0111000111100";
String str1=str.replaceAll("11*"," ");
String str2=str.replaceAl……