小红书笔试第一次字符串压缩求助
一直都之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());
}
}
#小红书#
查看1道真题和解析