编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 ""

示例 1:

输入: ["flower","flow","flight"]
输出: "fl"

示例 2:

输入: ["dog","racecar","car"]
输出: ""
解释: 输入不存在公共前缀。

说明:

所有输入只包含小写字母 a-z 。

题解:

用两层循环,第一层循环选定字符串str[0],用substring获得str[0]的各个分字符串,在第二层循环中与其他字符串的substring对比,若不同则返回前一个子串:

class Solution {public String longestCommonPrefix(String[] strs) {int amount = strs.length;if (amount==0) return "";int len = strs[0].length();for(int i=1;i<amount;i++){if(strs[i].length()<len)len=strs[i].length();} String str="";for(int i=1;i<=len;i++){str=strs[0].substring(0,i);for(int j=1;j<amount;j++){if(!str.equals(strs[j].substring(0,i)))return str.substring(0,i-1);}}return str;}
}

注意:1.substring中string的s为小写。

2.对字符串个方法的掌握还是太差了,别人的解法中看到了用str1.indexOf(String str2)求解的,该方法若存在返回[0,str1.length()-str2.length()]的值,不存在返回-1,所以只需从str1.substring(0,str.length())到str1.substring(0,0)判断indexOf的返回值是否为0,若为0则返回字符串,否则str1=str1.substring(0,str1.length()-1)

转载于:https://www.cnblogs.com/annofyf/p/9382358.html

LeetCode刷题记录_最长公共前缀相关推荐

  1. python【力扣LeetCode算法题库】14-最长公共前缀(列表解压)

    最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","fl ...

  2. 算法记录 牛客网 leetcode刷题记录

    算法记录 & 牛客网 & leetcode刷题记录 解题思路 STL容器 常用算法模板 堆排序 插入排序 快速排序 BFS层序遍历 二叉树 JZ55 二叉树的深度 BST(binary ...

  3. LeetCode刷题记录7——824. Goat Latin(easy)

    LeetCode刷题记录7--824. Goat Latin(easy) 目录 LeetCode刷题记录7--824. Goat Latin(easy) 题目 语言 思路 后记 题目 题目需要将一个输 ...

  4. LeetCode刷题记录4——67. Add Binary(easy)

    LeetCode刷题记录4--67. Add Binary(easy) 目录 LeetCode刷题记录4--67. Add Binary(easy) 题目 语言 思路 后记 题目 今天这题是与字符串相 ...

  5. LeetCode刷题记录2——217. Contains Duplicate(easy)

    LeetCode刷题记录2--217. Contains Duplicate(easy) 目录 LeetCode刷题记录2--217. Contains Duplicate(easy) 题目 语言 思 ...

  6. LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)

    LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...

  7. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  8. LeetCode刷题记录13——705. Design HashSet(easy)

    LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...

  9. LeetCode刷题记录12——232. Implement Queue using Stacks(easy)

    LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...

最新文章

  1. 从零开始学习Sencha Touch MVC应用之八
  2. 多级联动下拉菜单插件:jquery.cxselect.js
  3. java的jdk和jre_Java的JDK和JRE
  4. Android系统从驱动到上层服务再到应用的两种服务架构方式
  5. 新书推荐 |《5G安全技术与标准》
  6. xmarin.android导航栏,Xamarin.Forms中心标题和透明导航栏 - Android
  7. JDK集合框架结构分析(二)
  8. 常见电容器图片_工业机器视觉的常见应用与施努卡VisionMax视觉系统介绍
  9. lin通讯从节点同步间隔场_LIN总线入门
  10. kotlin-android-extensions扩展的导入
  11. 博科光纤交换机默认密码更改
  12. GitHub 中国区前 100 名到底是什么样的人?
  13. 佐切的第一天学习分享
  14. linux 符号执行,[原创]符号执行Symcc与模糊测试AFL结合实践
  15. python中nums[:]和nums
  16. ubuntu16.04编译高翔的ORBSLAM2_with_pointcloud_map,并保存点云图
  17. python11.网络通信过程
  18. LVGL打印LOG日志
  19. JAVA继承案例--计算圆柱体体积
  20. 全球气温数据集(.nc数据的打开方法)

热门文章

  1. 收藏 | 深度学习19个损失函数汇总
  2. 三维视觉前沿进展年度报告
  3. 呕心沥血干完K-Means聚类——深度AI科普团队
  4. 机器学习(十五)隐马尔科夫模型-未完待续
  5. c语言while跳不出来,这个while循环终止了却跳不出来为什么
  6. MYSQL8 关键字(官方)
  7. Navicat远程服务器2013-Lost connection to MYSQL server at 'reading for initial communication packet' 公钥
  8. pandas 空字符串与na区别_关于python:Pandas用空白/空字符串替换NaN
  9. anaconda使用github代码_使用这几款插件,能让你在GitHub看代码的效率翻倍
  10. 2019上半年系统集成项目管理工程师下午案例分析真题与答案解析