【LeetCode】Anagrams 解题报告

【题目】

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

Note: All inputs will be in lower-case.

【解析】

题意:给定一个字符串数组,返回所有是“换位词”的字符串。

所谓“换位词/变位词”就是包含相同字母,但字母顺序可能不同的字符串。比如“abc", "bca", "cab", "acb", "bac", "cba"都互为换位词。

很容易想到,这些换位词如果按字母序排序后都是”abc“,所以可以对原数组中每个字符串自身进行排序,然后出现过两次以上的就是我们要找的字符串。

需要注意的是,要保持原字符串数组不变,那么就需要一个同样大小的数组来存储排序后的每个字符串。

接下来的问题是,如何找出出现两次以上的字符串对应的原字符串,这个用代码比较难以实现,两层for循环的暴力解决会超时。

我这里用了一个比较笨的方法实现:再用一个同样大小的数组标记相应下标位置的原字符串是否为“换位词”,这样需要一边遍历,然后再一边遍历把相应位置的原字符串添加到结果集中。时间复杂度为O(n)。

public class Solution {
    public List<String> anagrams(String[] strs) {
        if (strs.length < 1) return null;
        List<String> ans = new ArrayList<String>();
        String[] strs2 = new String[strs.length];
        for (int i = 0; i < strs.length; i++) {
            char[] chs = strs[i].toCharArray();
            Arrays.sort(chs);
            strs2[i] = String.valueOf(chs);
        }

        boolean[] flags = new boolean[strs.length];
        Arrays.fill(flags, false);
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        for (int i = 0; i < strs2.length; i++) {
            if (map.containsKey(strs2[i])) {
                int last = map.put(strs2[i], i);
                flags[last] = true;
                flags[i] = true;
            } else {
                map.put(strs2[i], i);
            }
        }

        for (int i = 0; i < strs.length; i++) {
            if (flags[i]) {
                ans.add(strs[i]);
            }
        }

        return ans;
    }
}
时间: 2024-10-11 21:08:06

【LeetCode】Anagrams 解题报告的相关文章

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: Permutations 解题报告

Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. SOLUTION 1: 经典的递归回溯题目,一次ACCEPT. 请也参考上一个题目LeetCode: Combination

[LeetCode]3Sum,解题报告

题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The so

[LeetCode]Candy, 解题报告

前言 回学校写论文差不多快二周的时间了,总结一句话,在学校真舒服.花了7天时间搞定了毕业论文初稿(之所以这么快肯定跟我这三年的工作量相关了),不过今天导师批复第二章还是需要修改.此外,最近迷上打羽毛球,确实很爽,运动量仅此于篮球,可以场地有限,哎,且打且珍惜吧. 题目 There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these chi

Single Number | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/single-number/ 题目描述: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without usin

Add Two Numbers | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/add-two-numbers/ 题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i

Two Sum | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/two-sum/ 题目描述: Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, whe

LeetCode: isSameTree1 解题报告

isSameTree1 Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. SOLUTION 1 & SOLUTION 2: 以下是递归及非递归解法: 1. 递归解法就是判断当前节点是

LeetCode: Subsets 解题报告

Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1