题解 | #提取不重复的整数#
提取不重复的整数
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());
}
}