《LeetBook》leetcode题解(14):Longest Common Prefix[E]
2016-04-16 15:15
253 查看
我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/
Subscribe to see which companies asked this question
书的地址:https://hk029.gitbooks.io/leetbook/
014.Longest Common Prefix[E]
问题
Write a function to find the longest common prefix string amongst an array of strings.Subscribe to see which companies asked this question
思路
这个没啥思路的,怎么都要两重循环,因为是最长公共子串,随便找一个(一般是第一个作为基准),然后拿拿的首部慢慢去匹配后面的字符串就行了。public class Solution { public String longestCommonPrefix(String[] strs) { String s = ""; if(strs.length == 0) return ""; for(int i = 0; i < strs[0].length();i++) { char c = strs[0].charAt(i); for(int j = 1;j < strs.length;j++) { if(i >= strs[j].length() || strs[j].charAt(i) != c) return s; } s += c; } return s; } }
相关文章推荐
- 网络的介数中心性(betweenness)及计算方法
- Python install easy_install and pip
- javaScript随机生成一个5位以内的数,及数组对象Array原型forEach解析
- TexturePacker 命令行批量处理 导位图字体
- ubuntu 15 安装Qt
- ubuntu15.04在安装完vmware11后打开提示 VMware Kernel Module Updater
- maven的setting 文件阅读
- Python 练习实例27
- 常用函数
- 什么是渐进增强,优雅降级
- oracle用户默认表空间
- 字符串数组元素排列与组合的Java递归实现
- 获取所有安装的apk详细信息及常规操作
- SuperMap
- Android事件处理之回调事件
- Spring中父子容器的实现实例
- Java技术_Java千百问(0030)_package和import作用是什么
- 李航博士:浅谈我对机器学习的理解
- iOS应用访问不了网络的解决方法 App Transport Security has blocked a cleartext HTTP
- 笔试题27. LeetCode OJ (14)