题解 | #提取不重复的整数#

提取不重复的整数

http://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1

import java.util.*;
public class Main{
    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int num = sc.nextInt();
            // 存储输入的整数
            StringBuilder sb=new StringBuilder();
            sb.append(num);
            int strLength=sb.length();
            // 用来记录0出现的个数
            int count=0;
            // 判断最末尾是否为0
            if(sb.charAt(strLength-1)=='0'){
                    // 找出0的个数,末尾可能不止一个0
                    for (int i = 0; i < strLength; ++i) {
                        if (sb.charAt(i)=='0'){
                            count++;
                        }
                    }
                    // 删除末尾的0
                    for (int i = 0; i < count; ++i) {
                        if(sb.charAt(strLength-1)=='0'){
                            sb.deleteCharAt(strLength-1);
                            strLength--;
                        }
                    } 
            }
            // 去重(正反去重都行,因为末尾的0已经去掉)
            for(int i=strLength-1;i>=0;--i){
                char c = sb.charAt(i);
                for (int j = i-1; j >=0 ; --j) {
                    if ( c == sb.charAt(j)){
                        sb.deleteCharAt(j);
                        strLength--;
                    }
                }
              }
            // 反转
            sb.reverse();
            System.out.println(sb.toString());
    }
}
全部评论
思考应该还算全面,感觉有点冗余,望大佬指点
点赞 回复 分享
发布于 2022-06-26 10:42

相关推荐

点赞 评论 收藏
分享
牛客38347925...:9,2学生暑期实习失利开始投小厂,给这群人整自信了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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