- 浏览: 171747 次
- 性别:
- 来自: 济南
文章分类
最新评论
Given an array of strings, group anagrams together.
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
Return:
[
["ate", "eat","tea"],
["nat","tan"],
["bat"]
]
Note:
For the return value, each inner list's elements must follow the lexicographic order.
All inputs will be in lower-case.
有关异构字符串的题目,这道题让我们从一个字符串数组中将异构字符串分组,并且每组中是按照字母顺序排列的。
我们可以借助哈希表来完成。创建一个哈希表,让它的key为String,value为一个链表。每次从数组中取一个元素后,我们可以先将它转化为字符数组,然后排序后转换为一个字符串,这样一来所有的异构字符串都会变为相同一个在一个字符串,哈希表中的key就存放这个转换后的串。这样就可以把异构字符串分组了。代码如下:
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
Return:
[
["ate", "eat","tea"],
["nat","tan"],
["bat"]
]
Note:
For the return value, each inner list's elements must follow the lexicographic order.
All inputs will be in lower-case.
有关异构字符串的题目,这道题让我们从一个字符串数组中将异构字符串分组,并且每组中是按照字母顺序排列的。
我们可以借助哈希表来完成。创建一个哈希表,让它的key为String,value为一个链表。每次从数组中取一个元素后,我们可以先将它转化为字符数组,然后排序后转换为一个字符串,这样一来所有的异构字符串都会变为相同一个在一个字符串,哈希表中的key就存放这个转换后的串。这样就可以把异构字符串分组了。代码如下:
public class Solution { public List<List<String>> groupAnagrams(String[] strs) { HashMap<String, List<String>> hm = new HashMap<String, List<String>>(); List<List<String>> llist = new ArrayList<List<String>>(); if(strs == null || strs.length == 0) return llist; for(int i = 0; i < strs.length; i++) { char[] tem = strs[i].toCharArray(); Arrays.sort(tem); String s = new String(tem); if(!hm.containsKey(s)) { hm.put(s, new ArrayList<String>()); } hm.get(s).add(strs[i]); } for(List list : hm.values()) { Collections.sort(list); llist.add(list); } return llist; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 221Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 219You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 336Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 325Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 443Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 498Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 423Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 611Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 420The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 378Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 511Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 521Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 364All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 853Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 876Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 547Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 592Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 757Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 724You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 623For a undirected graph with tre ...
相关推荐
Anagrams by Robert Rayment.Finding anagrams quickly for up to 9 letters in VB is impossible challenge!).
简单java swing小游戏anagrams,这是一个把单词中字母顺序打乱后猜正确顺序的小游戏,非常简单,适合java初学者,我自己把从netbeans官网版的做了一些小改动,改成了更加随机的生成方式,提高了一点点游戏难度。
Programming with anagrams
Anagrams (Updated):Quickly finds anagrams for 2 to 9 letters. Using method suggested by Shannon Harmon. Use is made of the UKACD word list - see UKACD.txt.
给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。 题目参考: 实现版本: 题解参考: week 2 removeHexPrefix : 去除16进制的前缀,并转换为小写 实现版本: sanitizeHex : 为...
字母异位词(Anagrams) 基于前缀树的自动补全(Autocomplete Using Trie) 条形码验证器(Barcode Validator) Boyer-Moore搜索算法(Boyer Moore Search) 字符串能否重排成回文(Can String Be Rearranged As ...
资源来自pypi官网。 资源全名:anagrams-0.0.4.tar.gz
资源来自pypi官网。 资源全名:anagrams-0.0.2.tar.gz
Anagram programs for your
leetcode 2 和 c 力码 运行时统计: 前 0-20% : 9 次前 20-40% : 3 次前 40-60% : 4 次前 60-80% ...Group Anagrams ...Group Anagrams ...groupAnagrams ( self , strs : List [ str ]) -> List [ List
Stores the anagrams in ad in a shelf. filename: string file name of shelf ad: dictionary that maps strings to list of anagrams Anagrams是一种文字游戏,通过重新排列某个词或短语的字母来形成一个新的词...
字谜Android App-Anagram游戏
字谜 找到字谜和相似的发音词 运行示例 node example . js 单元测试 通过命令npm test 应用程序接口 findAnagrams(words) 对于输入的words数组,作为字谜的单词被组合在一起。 该函数返回一个数组数组,每个内部...
字谜应用 此应用程序是在名为Android的Applied CS的研讨会中开发的基本文字游戏。 字谜是通过重新排列另一个单词的字母而形成的单词。 例如,电影是冰人的七巧板。
git clone https://github.com/andyL89/anagrams_and_antigrams.git ⓘ注意:如果您有任何问题,请确保您的HTTPS代码正确! 上面的示例可能不是最新的HTTPS代码! 然后,在该过程完成后,使用命令。 code .补充说明...
Anagrams 排序 unordered_map Minimum Window Substring 两个指针遍历 map Maximal Rectangle 栈 局部递增 或者 动态规划 Binary Tree Inorder Traversal 栈 递归 Single Number 异或 Copy List with Random Pointer...
$ npm install wikipedia-anagrams 要在浏览器中使用,请使用 。 用法 var getAnagrams = require ( 'wikipedia-anagrams' ) ; getAnagrams( 资源, [opts,] clbk ) 在页面中查找。 该函数需要两个参数: 资源:...
vscode提交leetcode 我的leetcode练习笔记 结构 代码在根路径中,每个cpp文件都是一个问题的解决方案 有关特定问题的解决方案的一些说明在目录中。 我使用的工具 我使用扩展来测试和调试本地并提交我确定我的解决...
ANAGRAMS 7 COUNTING ELEMENTS 日 问题描述 问题和解决方案链接 Git 解决方案页面 8 Middle of the Linked List 9 Backspace String Compare 10 Min Stack 11 Diameter of Binary Tree 12 Last Stone Weight 13 ...