首页 > 技术文章 > 新华三:字符串不分大小写去重

zdtiio 2017-09-22 21:18 原文

题目:

字符串去重,只有大小写的英文字母,英文字母去重时不分大小写,且第一个出现的为大写就输出大写,第一个出现的为小写就输出小写

 

 

Java:字符串不分大小写去重

 1 import java.util.Arrays;
 2 import java.util.Scanner;
 3 
 4 public class Main {
 5     
 6     public static void main(String[] args) {
 7         Scanner sc=new Scanner(System.in);
 8         while(sc.hasNext()){
 9             String s = sc.nextLine();
10             int[] state = new int[26];
11             for(int i = 0; i < s.length(); i++){
12                 if((s.charAt(i)-65) <= 25){
13                     if(state[s.charAt(i)-65] == 0){
14                         System.out.print(s.charAt(i));
15                         state[s.charAt(i)-65] = 1;
16                     }
17                     
18                 }
19                 else{
20                     if(state[s.charAt(i)-97] == 0){
21                         System.out.print(s.charAt(i));
22                         state[s.charAt(i)-97] = 1;
23                     }
24                 }
25             }
26             System.out.println();
27         }
28         sc.close();
29     }
30     
31 }

 

推荐阅读