我最近接受了一些技术性采访,问题是:
问题1两个字符串被给予“你好”和“世界”。打印第一个字符串中的唯一字符,而不是第二个字符串中的唯一字符。 输出:
He。
我的答案是:将一个字符串的每个字符与第二个字符的每个字符进行比较,这一点都不是最优的(显然,错误的)。
Q.2
ABCABBABCAB, OUTPUT:4A5B2C`,(基本上计算每个字符的出现次数) 这是在一次传递中完成的,而不是字符串中的多次遍历,在那里,另一次以最优的方式执行。
同样的,在那里也很少有其他的问题。
我的核心问题是:
另外,如果有这类主题的书籍,请告诉我
任何帮助-书籍,参考资料和链接将对学习和理解有很大帮助.
的重要性:我需要实时场景,其中的数据结构是实现的
我学习过,收集API,不是彻底,而是一个总结的想法,层次和主要的数据结构类。我知道如何使用它们,但是在哪里,为什么要使用它们,我却避之不及?
发布于 2012-10-23 14:19:31
public class G {
public static void main(String[] args) {
new G().printCharacterCount("ABCABBABCAB");
System.out.println();
new G().printUniqueCharacters("Hello", "world");
}
void printUniqueCharacters(String a, String b) {
Set<Character> set = new HashSet<Character>();
for (int i = 0; i < a.length(); i++)
set.add(a.charAt(i));
for (int i = 0; i < b.length(); i++)
set.remove(b.charAt(i));
for (Character c : set)
System.out.print(c);
}
void printCharacterCount(String a) {
Map<Character, Integer> map = new TreeMap<Character, Integer>();
for(int i = 0; i < a.length(); i++) {
char c = a.charAt(i);
if(!map.containsKey(c))
map.put(c, 0);
map.put(c, map.get(c) +1);
}
for(char c : map.keySet()) {
System.out.print(map.get(c) + "" + c);
}
}
}发布于 2012-10-23 14:20:36
您可以使用的算法示例。
Q1。
Q2。
Map<Character, Integer>中存储字母的出现数我知道如何使用它们,但是在哪里,为什么要使用它们,我却避之不及?
试着自己解决那种难题;-)
发布于 2012-10-23 14:24:26
Set<Character> set1=new HashSet<Character>(Arrays.asList(ArrayUtils.toObject("Hello".toCharArray())));
Set<Character> set2=new HashSet<Character>(Arrays.asList(ArrayUtils.toObject("World".toCharArray())));
set1.removeAll(set2);
System.out.println(set1);使用apache ArrayUtils.toObject(char[]数组) .You可以编写util方法。
https://stackoverflow.com/questions/13032288
复制相似问题