l****r 发帖数: 118 | 1 0 minutes ago Accepted 796 ms java
0 minutes ago Compile Error N/A java
2 minutes ago Accepted 776 ms java
5 minutes ago Accepted 752 ms java
6 minutes ago Wrong Answer N/A java
30 minutes ago Accepted 716 ms java |
s**x 发帖数: 7506 | 2 把 code 贴出来吧, 大家一起 review. |
e*****i 发帖数: 182 | 3 不改也会有浮动的~
【在 l****r 的大作中提到】 : 0 minutes ago Accepted 796 ms java : 0 minutes ago Compile Error N/A java : 2 minutes ago Accepted 776 ms java : 5 minutes ago Accepted 752 ms java : 6 minutes ago Wrong Answer N/A java : 30 minutes ago Accepted 716 ms java
|
l****r 发帖数: 118 | 4 Anagrams 那道题,谢谢大家帮忙review。
public ArrayList anagrams(String[] strs) {
ArrayList res = new ArrayList();
boolean[] added = new boolean[strs.length];
for(int i = 0; i< added.length; i++)
added[i] = false;
if (strs == null)
return null;
if (strs.length <=1)
return res;
Hashtable map = new Hashtable();
for(int i = 0; i< strs.length; i++)
{
char[] c = strs[i].toCharArray();
Arrays.sort(c);
String str = new String(c);
if(map.containsKey(str))
{
added[map.get(str)] = true;
added[i] = true;
}
else
{
map.put(str, i);
}
}
for(int j = 0; j
{
if(added[j] == true)
{
res.add(strs[j]);
}
}
return res;
}
}
【在 s**x 的大作中提到】 : 把 code 贴出来吧, 大家一起 review.
|
q********c 发帖数: 1774 | 5 3 minutes ago Accepted 664 ms java
My code:
public class Solution {
public ArrayList anagrams(String[] strs) {
ArrayList words = new ArrayList();
HashMap hm = new HashMap();
HashSet hset = new HashSet();
for(int i = 0; i < strs.length; ++i) {
char[] ch = strs[i].toCharArray();
Arrays.sort(ch);
String ana = new String(ch);
if(hm.containsKey(ana)) {
words.add(strs[i]);
String s = hm.get(ana);
if(!hset.contains(s)) {
words.add(s);
hset.add(s);
}
}
else {
hm.put(ana, strs[i]);
}
}
return words;
}
} |
s**x 发帖数: 7506 | 6 java 连个 string.sort 的函数都没有? sigh. 解法大同小异。 |
z****8 发帖数: 5023 | 7 都是
char[] =str.toArrays();
Arrays.sort(char[]);
String str = new String(char[])
【在 s**x 的大作中提到】 : java 连个 string.sort 的函数都没有? sigh. 解法大同小异。
|