小红书笔试第一次字符串压缩求助

一直都之A 80%,实在想不到哪里错了。有大佬全A了么?

附上我的代码:
import java.util.Scanner;

public class RedBookMain1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        if(s== null || s.length() == 0){
            System.out.println(s);
        }
        char pre = s.charAt(0);
        long cnt = 0;
        StringBuilder res = new StringBuilder("");
        for(int i = 1;i < s.length(); i++){
            char c = s.charAt(i);
            if(!Character.isAlphabetic(c)){
                res = res.append(c);
                continue;
            }
            if(c == pre){
                cnt++;
            }else{
                if(cnt == 0){
                    res = res.append(pre);
                }else{
                    res = res.append(cnt).append(pre);
                }
                cnt = 0;
                pre = c;
            }
        }
        if(cnt == 0){
            res = res.append(pre);
        }else{
            res = res.append(cnt).append(pre);
        }
        System.out.println(res.toString());
    }
}



#小红书#
全部评论
我之前也是80% 输出没输出空格就对了 java
点赞 回复 分享
发布于 2018-09-19 00:05
有空白字符的
点赞 回复 分享
发布于 2018-09-18 23:58
空格注意一下
点赞 回复 分享
发布于 2018-09-18 23:54
写代码的时候总是各种问题,不过最后还是通过了,花了不少时间调整。 你看看我的吧 import java.util.Scanner; public class Main {     public static void main(String[] args) {         Scanner s = new Scanner(System.in);         String str = s.nextLine()+"#";         s.close();         StringBuffer sb = new StringBuffer();         for (int i = 0; i < str.length();i++) {             for (int j = i + 1; j < str.length(); j++) {                 if (str.charAt(i) != str.charAt(j)){                     if (j - i > 1) {sb.append(j - i - 1);}                     sb.append(str.charAt(i));                                                             i=j-1;                     break;                 }             }         }         System.out.println(sb);     } }
点赞 回复 分享
发布于 2018-09-18 20:54
我的过了,就是二叉树搞不定
点赞 回复 分享
发布于 2018-09-18 20:48
用C++啊
点赞 回复 分享
发布于 2018-09-18 20:48
第一题我写了貌似20行?一下就过了。。。我自己都惊讶,不过代码没留下
点赞 回复 分享
发布于 2018-09-18 20:44
getline(cin, str) 过 100%,cin >> str过80%
点赞 回复 分享
发布于 2018-09-18 20:44
超过11个连续字符没考虑把。。
点赞 回复 分享
发布于 2018-09-18 20:35
import sys if __name__=='__main__':     string=sys.stdin.readline().strip('')     result=''     count=0     for i in range(1,len(string)):         value=string[i-1]         if string[i]==string[i-1]:             count+=1         else:             if count!=0:                 result+=str(count)+value             else:                 result+=value             count=0     print(result)
点赞 回复 分享
发布于 2018-09-18 20:35
要读取一行,我用的c++,空格也要的
点赞 回复 分享
发布于 2018-09-18 20:35

相关推荐

04-17 18:32
门头沟学院 Java
野猪不是猪🐗:他跟你一个学校,你要是进来之后待遇比他好,他受得了?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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