leetcode:Longest Common Prefix【Python版】
1、当strs为空,直接输出“”
2、当strs中含有“”,直接输出“”
3、strs[0]的最长长度由最短公共长度l决定(code line:15)
class Solution:
# @return a string
def longestCommonPrefix(self, strs):
if strs == []:
return ""
for i in range(1,len(strs)):
l1 = len(strs[0])
l2 = len(strs[i])
if l1>l2:
l = l2
else:
l = l1
if l==0:
return ""
strs[0]=strs[0][0:l]
for j in range(l):
if strs[0][j] != strs[i][j]:
strs[0] = strs[0][0:j]
break
return strs[0]
leetcode:Longest Common Prefix【Python版】的更多相关文章
- leetcode Longest Common Prefix python
class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- Leetcode Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...
- Leetcode::Longest Common Prefix && Search for a Range
一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...
- LeetCode: Longest Common Prefix 解题报告
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- [LeetCode] Longest Common Prefix 字符串公有前序
Write a function to find the longest common prefix string amongst an array of strings. Hide Tags Str ...
- LeetCode——Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 写一个函数找出字符串数组中 ...
- [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀
题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...
- LeetCode Longest Common Prefix 最长公共前缀
题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...
随机推荐
- English trip -- VC(情景课)9 B Outside chores 室外家务
Vocabulary focus 核心词汇 cutting the grass 修剪草坪 getting the mail 收到邮件 taking out the trash 把垃圾带出去 wal ...
- (转)代号为Purley的新一代服务器平台
英特尔(Intel)正式发布了代号为Purley的新一代服务器平台,包括代号为Skylake的新一代至强(Xeon)CPU,命名为英特尔至强可扩展处理器(Intel Xeon Scalable Pro ...
- Intent在Activity之间传值的几种方式
发这篇博客主要讲一下Android中Intent中如何传值的几种方法: 1:基本数据类型,包含了Java八种基本数据类型和CharSequece文本2:八种数据类新对应数组和CharSequece文本 ...
- PHP函数总结 (三)
<?php/** * PHP变量的范围 * 1.局部变量(内部变量) * 在函数内部声明的变量,作用域仅限于函数内部,参数也是局部变量:执行完毕后函数内部的变量都被释放 * 若需要使用函数内的变 ...
- 『Glob』常用方法记录
glob.glob(file) 返回匹配的文件 glob.glob(./flower_photos/tulips/*.jpg) Out[1]:<br># ['./flower_photos ...
- linux单用户模式
linux单用户模式 2014年11月11日 17:18 在grub上相应要启动的内核上按“e”. 进入下一界面,继续按“e”. 在进入文本界面后,输入“single”回车. 进入grub界面后,按“ ...
- WebSocket教程(二)
运行环境:jdk8 tomcat8 无须其他jar包. package com.reach.socketController; import java.io.IOException; import j ...
- 使用HttpClient 发送 GET、POST、PUT、Delete请求及文件上传
package org.caeit.cloud.dev.util; import java.io.File; import java.io.IOException; import java.io.Un ...
- hadoop redis install (4)
reference: http://dblab.xmu.edu.cn/blog/131/ https://github.com/dmajkic/redis https://blog.csdn ...
- 2.strcpy使用注意(2)
分析下述代码: void test2() { char string[10],str1[10]; int i; for(i=0;i<10;i++) { srtr1='a'; } strcpy(s ...