百词斩字符串

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        byte[] line = scanner.nextLine().getBytes();
//        byte[] line1 = "aaasddasuuuuuuujjjjjjssssss".getBytes();
//        byte[] line1 = "aaasdd".getBytes();
        test2(line);

    }
    public static void test2(byte[] line){
        int count = 1 ;
        byte tmp = 0 ;
        String res = "";
        for (int i = 0; i < line.length ; i++) {
            if( tmp == 0){
                tmp = line[ i ];
                res = res + new String(new byte[]{line[ i ]});
            }
            if( (i < line.length-1 && tmp != line[ i + 1 ] ) || i == line.length -1 ){
                tmp = 0 ;
            }else{
                count ++ ;
            }

            if( count > 1 && tmp == 0 ){
                res = res + count;
                count = 1 ;
            }
        }

        System.out.println( res );
    }

#笔试题目#
全部评论

相关推荐

2 1 评论
分享
牛客网
牛客企业服务