字节0827测开笔试

第一道题字符串由01组成,比如0111000100,一次性删除一个子串(全1或全0),多少次可以删到空串
a了

import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.next();
        int num1 = 0;
        int num0 = 0;
        int i = 0;
        while(i < str.length()){
            if(i < str.length() &amp;&amp; str.charAt(i) == '0'){   //遇到0
                num0++;
                while(i < str.length() &amp;&amp; str.charAt(i) == '0')
                    i++;
            }
            if(i < str.length() &amp;&amp; str.charAt(i) == '1'){   //遇到0
                num1++;
                while(i < str.length() &amp;&amp; str.charAt(i) == '1')
                    i++;
            }
        }
        int result = num0 < num1 ? num0 + 1 : num1 + 1;
        System.out.println(result);
    }
}

第二道题两个字符串,最长前缀为a,最长后缀为b,相似比为a*b,假设修改字符串中的一个,相似比最大为多少?
0.8 不知到哪里出问题了
全部评论

相关推荐

评论
1
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务