Lintcode487-Name Deduplication-Easy

487. Name Deduplication

Given a list of names, remove the duplicate names. Two name will be treated as the same name if they are equal ignore the case.

Return a list of names without duplication, all names should be in lowercase, and keep the order in the original list.

Example

Example 1:

Input:["James", "james", "Bill Gates", "bill Gates", "Hello World", "HELLO WORLD", "Helloworld"]


Output:["james", "bill gates", "hello world", "helloworld"]

Clarification

You can assume that the name contains only uppercase and lowercase letters and spaces.

思路:

用hashmap去除重复,先把字符串转小写,调用toLowerCase方法,返回值要重新赋值给str!! 

代码:

 1 public class Solution {
 2     /**
 3      * @param names: a string array
 4      * @return: a string array
 5      */
 6     public List<String> nameDeduplication(String[] names) {
 7         List<String> result = new ArrayList<String>();
 8         Map<String, Integer> map = new HashMap<String, Integer>();
 9         for (String str : names) {
10             str = str.toLowerCase();
11             if (!map.containsKey(str)) {
12                 map.put(str, 1);
13                 result.add(str);
14             }
15         }
16         return result;
17     }
18 }

 

posted @ 2019-04-09 16:43  IreneZh  阅读(136)  评论(0编辑  收藏  举报