题解 | #彩色的砖块#

彩色的砖块

https://www.nowcoder.com/practice/8c29f4d1bea84d6ba2847e079b7420f7

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String str = in.nextLine();
        Set<Character> set = new HashSet<Character>();
        for(int i = 0;i<str.length();i++){
            set.add(str.charAt(i));
        }
        if(set.size()==1)   System.out.println(1);
        else if(set.size()==2)  System.out.println(2);
        else    System.out.println(0);
    }
}

使用set元素去重,然后分析set中不重复的元素个数。

全部评论

相关推荐

小覃1:硕士了还投助理岗位吗,一般不都直接干工程师了吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务