14. Longest Common Prefix
来源:互联网 发布:ubuntu删除文件夹加 编辑:程序博客网 时间:2024/05/16 10:27
Write a function to find the longest common prefix string amongst an array of strings.
找出字符串数组的最长公共前缀
Trick:利用排序,只比较第一个和最后一个字符串即可
class Solution(object): def longestCommonPrefix(self, strs): if strs == []: return "" strs.sort() str1 = strs[0] str2 = strs[-1] common_len = 0 if len(str1) <= len(str2): cnt = len(str1) else: cnt = len(str2) for i in xrange(cnt): if str1[i] == str2[i]: common_len = i+1 else: break return str1[:common_len]
阅读全文
0 0
- 14. Longest Common Prefix
- 14.Longest Common Prefix
- 14. Longest Common Prefix
- 14. Longest Common Prefix
- 14. Longest Common Prefix
- 14. Longest Common Prefix
- 14. Longest Common Prefix
- 14. Longest Common Prefix
- 14. Longest Common Prefix
- 14. Longest Common Prefix
- 14. Longest Common Prefix
- 14. Longest Common Prefix
- 14. Longest Common Prefix
- 14. Longest Common Prefix
- 14. Longest Common Prefix
- 14. Longest Common Prefix
- 14. Longest Common Prefix
- 14. Longest Common Prefix
- 多线程。
- PhotoShop CS5 /ps5 激活码(全部验证有效)+破解验证方式
- 音响设备开关顺序
- 数据回显(13)
- 使用Jekyll + GitHub Pages搭建个人博客
- 14. Longest Common Prefix
- Spring依赖注入(DI)
- 【专题】倍增LCA
- 利比亚行动
- 1228. Matrix
- ios 换电脑继续使用csr 证书等。
- markdown备用
- SQLite小结
- PHP模拟发送POST请求之三、用Telnet和fsockopen()模拟发送POST信息