LeetCode Anagrams My solution

Anagrams

Given an array of strings, return all groups of strings that are anagrams.

Note: All inputs will be in lower-case.

我的解题思路是这种:1.事实上所谓的anagrams就是字母同样就ok了

2.把每一个数组里面的数字以字典序进行又一次排序形成fingerprint

3.推断排序过得串是否在hashmap里面,假设在里面就加一,说明这个fingerprint 有了

有的话表明这个fingerprint相应的String是anagrams

4.用strs里面的string 扫一下map ,假设fingerprint相应的数目大于1,

则表示这个String就是所要的了,记录结果就好。

用到的函数

String.toCharArray();

Arrays.toString();

Hashmap.containsKey();

Hashmap.put(String,Integer);

public class Solution {
    public List<String> anagrams(String[] strs) {
        List<String> result = new ArrayList<String>();
        if (strs == null || strs.length == 0 ) {
            return result;
        }
     HashMap<String,Integer> hmaps = new HashMap<String,Integer>();

     for (int i = 0; i < strs.length; i++) {
         String curSort = sortString(strs[i]);
         if (hmaps.containsKey(curSort)) {
           hmaps.put(curSort, hmaps.get(curSort) + 1);
         } else {
           hmaps.put(curSort, 1);
         }
     }
     for(int i = 0; i < strs.length; i++) {
         if (hmaps.containsKey(sortString(strs[i])) && hmaps.get(sortString(strs[i])) > 1) {
             result.add(strs[i]);
         }
     }
     return result;
    }
    String sortString(String str) {
        char [] charArr = str.toCharArray();
        Arrays.sort(charArr);
        return Arrays.toString(charArr);
    }
}
时间: 2024-12-11 07:50:26

LeetCode Anagrams My solution的相关文章

[leetcode]Anagrams @ Python

原题地址:https://oj.leetcode.com/problems/anagrams/ 题意: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 解题思路:anagram的意思是:abc,bac,acb就是anagram.即同一段字符串的字母的不同排序.将这些都找出来.这里使用了哈希表,即Python中的dic

LeetCode ---Anagrams() 详解

Notice: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. For example: Input: ["tea","and","ate","eat","den"] Output:   ["tea",&qu

LeetCode: Anagrams [048]

Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions and analyzing the topology o

Leetcode:Anagrams 回文构词法

戳我去解题 Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. Anagram(回文构词法)是指打乱字母顺序从而得到新的单词 回文构词法有一个特点:单词里的字母的种类和数目没有改变,只是改变了字母的排列顺序.因此,将几个单词按照字母顺序排序后,若它们相等,则它们属于同一组anagrams class Solution {

[Leetcode] Anagrams 颠倒字母构成词

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题意:anagrams的意思是回文构词法.回文构词法有一个特点:单词里的字母的种类和数目没有改变,只是改变了字母的排列顺序.如: Input: ["tea","and","ate","eat",&qu

[leetcode] Anagrams

Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 思路: 什么是anagrams?傻逼了,百度吧.一百度全是别人的方法,看到了有map存在,正是这个提示导致此题并没有用很长的时间. 方法很简单,将vector中的每个string先排序,然后在map中寻找是否已经有排序后的string,如果有则说明找到

LeetCode: Anagrams 题解

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题解: 判断字符串是否为回文构词法生成的.找出所有由同一回文构词法生成的字符串对. 使用map用于散列. 将strs中的字符串strs[i],在串内进行字典排序,生成key,原始s[i]不变. 将该字符串s[i]映射到key所对应位置.map[key].push_bac

LeetCode: Anagrams 解题报告

AnagramsGiven an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 思路: 建Hashtable,用排序过的string作为key,它的anagram作为ArrayList 这道题之前用暴力写的O(N^2)的TLE了,改用Hashtable来写题目的意思是给一个String数组,找出其中由相同字母组成的单词.例如:S =

LeetCode——Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 原题链接:https://oj.leetcode.com/problems/anagrams/ 易位构词游戏的英文词汇是 anagram,这个词来源于有"反向"或"再次"的含义的希腊语字根ana-和有"书写"."