[LeetCode]Shortest Word Distance
2015-11-27 16:14
295 查看
public class Solution { public int shortestDistance(String[] words, String word1, String word2) { int pre1 = -1; int pre2 = -1; int result = Integer.MAX_VALUE; for (int i = 0; i < words.length; i++) { String word = words[i]; if (word.equals(word1)) { if (pre2 != -1) { result = Math.min(i - pre2, result); } pre1 = i; } else if (word.equals(word2)) { if (pre1 != -1) { result = Math.min(i - pre1, result); } pre2 = i; } } return result; } }
相关文章推荐
- js 数组和对象
- 轻松学习C#的异常处理
- How does the Visitor Pattern come from?如何推导出访问者模式
- 排序——基本排序算法
- OpenCV - 区域生长算法
- java 生成excel根据jxl
- 直方图均衡化原理
- codeforces 601A The Two Routes(BFS)
- jquery 设置select的默认值
- ORM之三:DbProvider与DbFactory
- js字符串转json
- (转)Jquery之ShowLoading遮罩组件
- Android-类qq功能(二)
- INI文件操作类
- Hibernate配置
- 插入Mysql时中文乱码解决方案
- size_t 类型
- Android主题和样式之系统篇(上)
- ORM之二:核心接口与扩展操作
- LeetCode 58:Length of Last Word