输入一个字符串,去掉重复的字符,并按ASCII值排序-华为机试

 1 import java.util.Scanner;
 2 
 3 //输入字符串,去掉重复的字符,并按ASSIC码值排序
 4 public class quChong {
 5 
 6     public static void main(String arg[]){
 7         Scanner cin = new Scanner(System.in);
 8         String str = cin.nextLine();
 9         char c[] = str.toCharArray();
10         int count=0;
11         int flag=0;
12         for(int i=0;i<c.length-count;i++){
13             char temp;            
14             if(count>i&&flag==1){
15                 i=0;
16             }else if(count<=i&&flag==1){
17                 i=i-count;
18             }
19             temp = c[i];
20             flag = 0;
21             for(int j =i+1;j<c.length-count;j++){
22                 if(temp==c[j]){            
23                     for(int z =i;z<c.length-count-1;z++){
24                         c[z]=c[z+1];
25                     }
26                     count++;
27                     flag=1;
28                     break;
29                 }
30             }        
31         }
32         for(int i=0;i<c.length-count;i++){
33             for(int j=0;j<c.length-count-1;j++){
34                 char temp;
35                 if(c[j]>c[j+1]){
36                     temp=c[j];
37                     c[j]=c[j+1];
38                     c[j+1]=temp;
39                 }
40             }
41         }
42         for(int i=0;i<c.length-count;i++){
43             System.out.print(c[i]);
44         }
45         
46         
47     }
48 }

要点:字符串可用部分长度在变化,前面的比较起始位置在变

测试用例:

输入:qqqwww1212
输出:12qw

原文地址:https://www.cnblogs.com/Janejxt/p/5796071.html