Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4fe9f44

Browse files
author
applewjg
committed
Anagrams
Change-Id: I9a1764ca825de629dce15809f5f9b7458dfd87a1
1 parent12e004c commit4fe9f44

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

‎Anagrams.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 25, 2014
4+
Problem: Anagrams
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/anagrams/
7+
Notes:
8+
Given an array of strings, return all groups of strings that are anagrams.
9+
Note: All inputs will be in lower-case.
10+
11+
Solution: Sort the string to see if they're anagrams.
12+
*/
13+
14+
importjava.util.Map.Entry;
15+
publicclassSolution {
16+
publicList<String>anagrams(String[]strs) {
17+
ArrayList<String>res =newArrayList<String>();
18+
HashMap<String,ArrayList<String>>group =newHashMap<String,ArrayList<String>>();
19+
if (strs.length ==0)returnres;
20+
for (inti =0;i <strs.length; ++i) {
21+
char[]tmp =strs[i].toCharArray();
22+
Arrays.sort(tmp);
23+
Strings =String.valueOf(tmp);
24+
if(group.containsKey(s))
25+
(group.get(s)).add(strs[i]);
26+
else {
27+
ArrayList<String>t =newArrayList<String>();
28+
t.add(strs[i]);
29+
group.put(s,t);
30+
}
31+
}
32+
Iterator<Entry<String,ArrayList<String>>>iter =group.entrySet().iterator();
33+
while (iter.hasNext()) {
34+
Map.Entryentry = (Map.Entry)iter.next();
35+
ArrayList<String>val = (ArrayList<String>)entry.getValue();
36+
if (val.size() >1)res.addAll(val);
37+
}
38+
returnres;
39+
}
40+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp