2017网易---下厨房

题目描述

牛牛想尝试一些新的料理,每个料理需要一些不同的材料,问完成所有的料理需要准备多少种不同的材料。

输入描述:

每个输入包含 1 个测试用例。每个测试用例的第 i 行,表示完成第 i 件料理需要哪些材料,各个材料用空格隔开,输入只包含大写英文字母和空格,输入文件不超过 50 行,每一行不超过 50 个字符。

输出描述:

输出一行一个数字表示完成所有料理需要多少种不同的材料。
示例1

输入

BUTTER FLOUR
HONEY FLOUR EGG

输出

4

题目链接:https://www.nowcoder.com/practice/ca5c9ba9ebac4fd5ae9ba46114b0f476?tpId=85&tqId=29832&tPage=1&rp=1&ru=/ta/2017test&qru=/ta/2017test/question-ranking

法一:直接用hashSet去重,直接调用库函数。代码如下(耗时24ms):
 1 public class Main {
 2 
 3     public static void main(String[] args) throws IOException {
 4         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 5         String line = null;
 6         Set<String> set = new HashSet<String>();
 7         while((line = in.readLine()) != null) {
 8             String[] s = line.split(" ");
 9             for(int i = 0; i < s.length; i++) {
10                 set.add(s[i]);
11             }
12         }
13         System.out.println(set.size());
14     }
15 
16 }
View Code
原文地址:https://www.cnblogs.com/cing/p/8615114.html