Leetcode算法刷题:第14题 Longest Common Prefix
Longest Common Prefix
题目
给予一个列表,元素为字符串,写一个程序找出最长公共前缀
解题思路
先比较两个字符串,如果第一个字符不一样,则返回空值,比较完成后,用这个公共字符串和下一个元素继续比较,以此类推
class Solution:
# @param {string[]} strs
# @return {string}
def longestCommonPrefix(self, strs):
if not strs:
return ''
if len(strs) == 1:
return strs[0]
i = 0
while i+1 < len(strs):
temp = ''
for j in range(min(len(strs[i]), len(strs[i+1]))):
if strs[i][0] != strs[i+1][0]:
return ''
if strs[i][j] == strs[i+1][j]:
temp += strs[i][j]
i += 1
strs[i] = temp
return strs[len(strs)-1]
Leetcode算法刷题:第14题 Longest Common Prefix的更多相关文章
- LeetCodeOJ刷题之14【Longest Common Prefix】
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- 【LeetCode算法-14】Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- 【LeetCode OJ 14】Longest Common Prefix
题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...
- # Leetcode 14:Longest Common Prefix 最长公共前缀
公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...
- LeetCode 14: Longest Common Prefix
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- LeetCode(14)Longest Common Prefix
题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 该题目是求一个 ...
- C# 写 LeetCode easy #14 Longest Common Prefix
14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- 【LeetCode】LeetCode——第14题:Longest Common Prefix
14. Longest Common Prefix My Submissions Question Editorial Solution Total Accepted: 97052 Total Sub ...
随机推荐
- DataSnap
一. DataSnap REST - http://docwiki.embarcadero.com/RADStudio/Berlin/en/DataSnap_REST 1. URI Mapping: ...
- JS+html实现简单的飞机大战
摘要:通过原生的js+html实现简单的飞机大战小游戏效果,如图所示: 实现代码如下: 1.自己的飞机实现 飞机html: <!DOCTYPE html> <html lang=&q ...
- Java 学习 第三篇;面向对象
1:Java的常用包: 核心类在java 包中:扩展类在javax包中 java.lang 系统默认自动导入 包含String Math System Thread等类 java.util 包含了工具 ...
- 2014.8.4我出的模拟赛【你的名字叫czy是吧】
你的名字叫czy是吧 (mynameisczy.pas/.c/.cpp) 尽管czy放了那么多只NTR酋长,也没能拦住黄巨大.黄巨大和czy相遇了…… “你的名字叫czy是吧” “……” “我们来单挑 ...
- Android Studio参考在线文章
This article is From :http://www.android-studio.org/index.php/docs/guide Gradle使用手册(一):为什么要用Gradle? ...
- 关于memcpy和memmove的一点说明
今天看到书上降到memcpy和memmove的区别才突然发现原来两者之间有如此区别,以前只知道这两个函数是 实现同样的功能,没有接触到其不同. memcpy和memmove在MSDN的定义如下: 从两 ...
- GDB+GDBServer调试Linux应用程序
参考:http://blog.csdn.net/shanghaiqianlun/article/details/7820401 一.gdb+gdbserver总体介绍 远程调试环境由宿主机GDB和目标 ...
- No.26
"信是未见之事的实底,是所望之事的确据".
- IOS 下雪动画修改版本
#define SNOW_IMAGENAME @"snow" #define IMAGE_X arc4random()%(int)Main_Screen_Width #define ...
- IOS 排序算法
/** * @brief 冒泡排序法 * * @param arr 需要排序的数组 */ -(void)BubbleSort:(NSMutableArray *)arr { // 取第一个与其邻接的对 ...