【leetcode】Anagrams (middle)

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

Note: All inputs will be in lower-case.

anagrams 的意思是两个词用相同的字母组成  比如 “dog" "god"


把单词排序 如 dog 按字母排序变为 dgo

用unordered_map<string, int> 记录排序后序列第一次出现时,字符串在输入string向量中的位置

用vector<bool> 记录每个输入字符串是否为anagram, 如果在map中发现已经存在了,就记录当前和初始的字符串都是anagram

class Solution {
    vector<string> anagrams(vector<string> &strs) {
        vector<string> ans;
        vector<bool> isanagrams(strs.size(), false);
        unordered_map<string, int> hash;
        if(strs.size() == 0)
            return ans;

        for(int i = 0; i < strs.size(); i++)
            string cur = strs[i];
            sort(cur.begin(), cur.end());
            if(hash.find(cur) == hash.end()) //没出现过
                hash[cur] = i; //记录第一次出现是strs中的哪一个
            else //出现过
                isanagrams[hash[cur]] = true;
                isanagrams[i] = true;

        for(int j = 0; j < strs.size(); j++)
            if(isanagrams[j] == true)

        return ans;
时间: 03-10

【leetcode】Anagrams (middle)的相关文章


Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. Anagrams:即字母个数和字母都相同,但是字母顺序不相同的词 e.g. "tea","and","ate","eat","dan".   return &qu

【leetcode】Combinations (middle)

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 思路:有点像0-1背包问题, 对于从1-n的每一个数字都可以选择放入答案 和不放入答案. 当长度达到k时就是一个符合条件的解. 递归的

【leetcode】Permutations (middle)

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]. 求没有重复数字的全排列 思路:用的标准回溯法,一次AC class Solution { public: vector<vector<int>

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu

【Leetcode】Unique Paths II

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

【Leetcode】Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】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 it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->