【发布时间】:2019-02-28 02:15:13
【问题描述】:
当我尝试打印我的 char 数组时,我得到一堆框 [][][]。
这是我的代码。我如何打印这个并得到数字值数组,例如[1,2,3]?
package Com;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Solution l1 = new Solution();
// int[] x = {3,3,7,7,10,10,11};
String[] x = {"eat", "tea", "tan", "ate", "nat", "bat"};
l1.groupAnagrams(x);
};
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> ans = new ArrayList<>();
Map<Integer,List<String>> m = new HashMap<>();
for(String s: strs) {
char[] chars = new char[26];
for (char c : s.toCharArray()) {
chars[c-'a']++;
}
System.out.println(chars);
}
return ans;
};
};
【问题讨论】:
-
为什么要将整数存储在 char 数组中?
-
看起来
char[] chars应该包含字符数。这些计数肯定是整数,而不是字符? -
@shmosel 将其转换成hashcode,然后比较eat和tea等两个对象
-
嗯?什么哈希码?
-
你真正的问题是“我怎样才能产生一个哈希码,它对于具有不同顺序的相同字母的字符串是相同的,但对于不同字母的字符串是不同的”?