牛客網——華爲機試(題10:字符個數統計)(Java)

題目描述:

編寫一個函數,計算字符串中含有的不同字符的個數。字符在ACSII碼範圍內(0~127)。不在範圍內的不作統計。

輸入描述:

輸入N個字符,字符在ACSII碼範圍內。

輸出描述:

輸出範圍在(0~127)字符的個數。

示例1:

輸入:

abc

輸出:

3

代碼: 

import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main {
	public static void main ( String[] args ) {
		Scanner in = new Scanner( System.in );
		String s = in.nextLine();
		Set<Character> c = new TreeSet<Character>();
		int n = 0;
		for( int i = 0; i < s.length(); i++ ) {
			c.add( s.charAt( i ) );
		}
		for( Character i : c ) {
			if( i >= 0 && i <= 127 ) {
				n += 1;
			}
		}
		System.out.println( n );
		in.close();
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章