[LinkedIn] Word Distance Finder (Find distance in dictionary)
来源:互联网 发布:sql 计算查询结果合计 编辑:程序博客网 时间:2024/05/19 20:49
Example:
WordDistanceFinder finder = new WordDistanceFinder(Arrays.asList(“the”, “quick”, “brown”, “fox”, “quick”));
assert(finder.distance(“fox”,”the”) == 3);
assert(finder.distance(“quick”, “fox”) == 1);
/**Solution: go step by step, keep updating the max distance**/String targetString = "quick";String targetString2 = "fox";int index1 = -1, index2 = -1;int minDistance = Integer.MAX_VALUE, tempDistance = 0;for (int x = 0; x < strings.length; x++) { if (strings[x].equals(targetString)) { index1 = x; } if (strings[x].equals(targetString2)) { index2 = x; } if (index1 != -1 && index2 != -1) { // both words have to be found tempDistance = (int) Math.abs(index2 - index1); if (tempDistance < minDistance) { minDistance = tempDistance; } }}System.out.println("Distance:\t" + minDistance);
0 0
- [LinkedIn] Word Distance Finder (Find distance in dictionary)
- Distance
- distance
- distance
- Distance
- leetcode--Shortest Word Distance
- Leetcode: Shortest Word Distance
- *LeetCode-Shortest Word Distance
- [leetcode243] Shortest Word Distance
- Shortest Word Distance
- Shortest Word Distance II
- [LeetCode243]Shortest Word Distance
- LeetCode Shortest Word Distance
- Leetcode243: Shortest Word Distance
- Leetcode Shortest Word Distance
- Shortest Word Distance
- Shortest Word Distance II
- Shortest Word Distance III
- 咦,你也在混日子啊 .
- 那些害死程序员的细节 .
- 上课觉得刚好鬼离开家就很尴尬
- 2015.04.01 Leetcode Insert interval
- 坑坑的程序员
- [LinkedIn] Word Distance Finder (Find distance in dictionary)
- 一切成功源于积累——20150401 黄金 2800点
- [LeetCode]Two Sum
- ruby on rails rotate log file(日志文件)
- [LinkedIn] String Permutation
- ADSL拨号
- Best Time to Buy and Sell Stock IV
- HTTP深入浅出 http请求
- 关于goto语句