/*
* @lc app=leetcode.cn id=14 lang=c
*
* [14] 最长公共前缀
*
* https://leetcode-cn.com/problems/longest-common-prefix/description/
*
* algorithms
* Easy (32.10%)
* Total Accepted: 54.4K
* Total Submissions: 169.3K
* Testcase Example: '["flower","flow","flight"]'
*
* 编写一个函数来查找字符串数组中的最长公共前缀。
*
* 如果不存在公共前缀,返回空字符串 ""。
*
* 示例 1:
*
* 输入: ["flower","flow","flight"]
* 输出: "fl"
*
*
* 示例 2:
*
* 输入: ["dog","racecar","car"]
* 输出: ""
* 解释: 输入不存在公共前缀。
*
*
* 说明:
*
* 所有输入只包含小写字母 a-z 。
*
*/ char* longestCommonPrefix(char** strs, int strsSize) {
if(strsSize == ) {
return "";
}
int index = , i = ;
char flag = strs[][index];
while(flag) {
for(i = ; i < strsSize; i++) {
if(strs[i][index] != flag)
break;
}
if(i < strsSize)
break;
flag = strs[][++index];
}
strs[][index] = '\0';
return strs[];
}

这个函数有两个参数,一个是 strs指针的数字(可以理解为二维数组,行代表第n个字符串,列代表其中的第m个字符) strssize是字符串的个数(可以理解为这个二维数组的行数)

如果长度是0的话,那么直接放回空 即 " "即可。

然后flag为 第一个字符串中的第一个字符。循环条件是flag依旧存在。flag从第一个字符串的第一个字符循环到最后一位。

在这个循环中嵌套一个循环,此循环为循环字符串,即第二个字符串,第三个。。第四个。。 比对的都是相同的位置的字符。

如果出现了不相等的情况,就退出循环,如果此时的i小于字符串的个数,则说明 没有达到 公共前缀的要求 ,就退出整个大循环。

循环外把 第一个字符串的每循环到的位置的字符变为终结符。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=14 lang=python3
#
# [14] 最长公共前缀
#
# https://leetcode-cn.com/problems/longest-common-prefix/description/
#
# algorithms
# Easy (32.10%)
# Total Accepted: 54.4K
# Total Submissions: 169.3K
# Testcase Example: '["flower","flow","flight"]'
#
# 编写一个函数来查找字符串数组中的最长公共前缀。
#
# 如果不存在公共前缀,返回空字符串 ""。
#
# 示例 1:
#
# 输入: ["flower","flow","flight"]
# 输出: "fl"
#
#
# 示例 2:
#
# 输入: ["dog","racecar","car"]
# 输出: ""
# 解释: 输入不存在公共前缀。
#
#
# 说明:
#
# 所有输入只包含小写字母 a-z 。
#
#
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if len(strs)==0:
return ''
if len(strs)==1:
return strs[0]
a = min(strs) #这里是ascii码的大小
b = max(strs)
for i in range(len(a)):
if a[i]!=b[i]:
return a[:i]
return a

这里运用了一个知识,min 和 max是求字符的ascii码大小,找出ascii码最小的和最大的,然后进行逐个比较。

Leecode刷题之旅-C语言/python-14.最长公共前缀的更多相关文章

  1. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  2. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  3. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  4. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  5. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  6. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  7. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  8. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

  9. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

随机推荐

  1. java原生文件打包

    一.背景 前端时间,自己做的项目需要打包功能,不想再引外部的jar包 便用java.util.zip做了下该功能 二.适用场景 生成多个word.excel.xml等文件,并要求打包下载的情形 例:项 ...

  2. 【Leetcode】【Easy】Plus One

    Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...

  3. Linux文件压缩和解压缩命令

    Linux文件压缩和解压缩命令: tar 命令(打包并压缩的话,原文件也会默认存在) -c 建立打包档案 -x 解包 -t 查看包里的类容 -r 向包里追加文件 -v 显示打包过程 -f 文件 比如: ...

  4. mysql 5.7版本如何修改密码

    这是官方截图,mysql5.7安装后,会有一个默认密码,保存在mysql.log里面,找的他,并更改 官方文档地址 https://dev.mysql.com/doc/refman/5.7/en/li ...

  5. php之Apache压力测试

    1,测试本机是否已经安装好Apache ①进入自己的Apache目录下面的bin目录,然后执行ab -V.如果返回Apache版本则表示已经装好 2,执行压力测试命令,ab -n 1000(请求总数) ...

  6. csu 1965

    Description You are a boss who has N employers. One day, you have a message and you want to tell it ...

  7. EXCRT

    是个好东西,可以处理在模数不互质的同余方程组 核心就是用扩欧来合并方程 如果我们有两个形如\(x\equiv b_1(mod\ a_1)\) \(x\equiv b_2(mod\ a_2)\)的方程我 ...

  8. 实现新layer的时候易犯的错误

    实现新layer后,如果我还是在原来的build文件夹里面make,好像是不会编译新的层的,所以跑程序会报没有你新添加的这个层.要么make clear重新make,要么就直接./build.sh,这 ...

  9. 【JeeSite】用户管理

    组织机构使用ztree插件,加载数据时使用数据权限过滤(只能访问登录用户的单位及其下属单位), 点击部门加载相应用户. <!-- 数据范围过滤 -->   BaseService.data ...

  10. Android学习笔记_53_Android界面的基本属性

    很好很全面http://www.eoeandroid.com/forum.php?mod=viewthread&tid=46859 布局: 在 android 中我们常用的布局方式有这么几种: ...