LintCode Anagrams

(记得import java.util.HashMap及Arrays,

首先字符串若为空或者数量为零, 则返回一个空的LinkedList)

1. 把string变为char数组, 再进行排序, 之后重新合为一个string叫mark

2. 建循环, 若hashmap(map)没有mark这个key, 就建一个LinkedList或ArrayList(放字符串的), 再把此哈希值(mark, LinkedList)放入哈希表map

3. 获取(或者已有mark)此时哈希表map里mark对应的value(LinkedList), 再将此时循环的这个字符串放入此时mark对应的LinkedList里

4. 将哈希表map里所有的mark及对应的value都找出来, 若value的表里的字符串的数量大于一的那个LinkedList, 则全都放入一个新的LinkedList(answer)里

5. 将此answer返回

public class Solution {
    /**
     * @param strs: A list of strings
     * @return: A list of strings
     */
    public List<String> anagrams(String[] strs) {
        if(strs == null) return new LinkedList<String>();
        if(strs.length == 0) return new LinkedList<String>();

        HashMap<String, List<String>> map = new HashMap<String, List<String>>();
        for(int i = 0; i < strs.length; i++){
            char[] arr = strs[i].toCharArray();
            Arrays.sort(arr);
            String mark = new String(arr);

            if(!map.containsKey(mark)){
                List<String> list = new LinkedList<String>();
                map.put(mark, list);
            }
            List<String> l = map.get(mark);
            l.add(strs[i]);
        }

        List<String> answer = new LinkedList<String>();
        for(String k : map.keySet()){
            List<String> list = map.get(k);
            if(list.size() > 1){
                answer.addAll(list);
            }
        }
        return answer;
        // write your code here
    }
}
时间: 2024-10-10 07:08:08

LintCode Anagrams的相关文章

[lintcode medium]Anagrams

Anagrams Given an array of strings, return all groups of strings that are anagrams. Example Given ["lint", "intl", "inlt", "code"], return ["lint", "inlt", "intl"]. Given ["ab"

[Lintcode] Substring Anagrams

Substring Anagrams Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 40,000. The order of outp

LintCode Two Strings Are Anagrams

1. 把string变为char数组 2. 排序Arrays.sort() public class Solution { /** * @param s: The first string * @param b: The second string * @return true or false */ public boolean anagram(String s, String t) { if(s == null || t == null) return false; if(s.length(

【LeetCode】49. Group Anagrams

Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"], ["nat",

[lintcode the-smallest-difference]最小差(python)

题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/ 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|).返回最小差. 排好序后用两个指针分别扫描两个数组,每次更新他们的差值的绝对值.并且依据他们两个数字的大小来决定谁来移动指针. 1 class Solution: 2 # @param

lintcode.44 最小子数组

最小子数组 描述 笔记 数据 评测 给定一个整数数组,找到一个具有最小和的子数组.返回其最小和. 注意事项 子数组最少包含一个数字 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? Airbnb Amazon LinkedIn Cryptic Studios Dropbox Apple Epic Systems TinyCo Yelp Hedvig Zenefits Uber Snapchat Yahoo Microsoft Bloomberg Facebook Google

Substring Anagrams

Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 40,000. The order of output does not matter.

lintcode 66.67.68 二叉树遍历(前序、中序、后序)

AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: The r

[LintCode/LeetCode]——两数和、三数和、四数和

LintCode有大部分题目来自LeetCode,但LeetCode比较卡,下面以LintCode为平台,简单介绍我AC的几个题目,并由此引出一些算法基础. 1)两数之和(two-sum) 题目编号:56,链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 题目描述: 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target. 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标.注意这里下标的范围是 1