華為線上編程-字元個數統計__華為線上編程

來源:互聯網
上載者:User

題目: 題目描述:
編寫一個函數,計算字串中含有的不同字元的個數。字元在ACSII碼範圍內(0~127)。不在範圍內的不作統計。 輸入描述:
輸入N個字元,字元在ACSII碼範圍內。 輸出描述:
輸出範圍在(0~127)字元的個數。

例子:

輸入:abc輸出:3

思路:跟統計不同字元個數的思路一樣,只不過在它基礎上加上一個對ACSII碼大小的限制(求字元的ACS,直接讓該字元賦值給int類型的變數即可) 代碼:

import java.util.*;public class Main{    public static void main(String[] args){        Scanner scan = new Scanner(System.in);        String str = scan.nextLine();        String result = "";        for(int i = 0; i < str.length(); i++){            int acs = str.charAt(i);            if(acs >= 0 && acs <= 127){                if(result.indexOf(str.charAt(i)) == -1){                    result += str.charAt(i);                }else{                    while(i < str.length()-1 && str.charAt(i) == str.charAt(i+1)){                        i++;                    }                }            }else{                continue;            }        }        System.out.println(result.length());    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.