LeetCode算法入门- Longest Common Prefix -day13
LeetCode算法入门- Longest Common Prefix -day13
- 题目描述:
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string “”.
Example 1:
Input: [“flower”,“flow”,“flight”]
Output: “fl”
Example 2:
Input: [“dog”,“racecar”,“car”]
Output: “”
Explanation: There is no common prefix among the input strings.
Note:
All given inputs are in lowercase letters a-z.
思路分析:
题目的意思是给定一个字符串数组,找出数组中所有字符串最长的公共前缀。
解题思路:可以将数组中的第一个元素strs[0]当做最大的公共前缀,然后来一一判断其他数组中是否有此公共前缀,如果没有,就将strs[0]中的最后一个元素去掉继续判断。。。code如下:
class Solution {public String longestCommonPrefix(String[] strs) {int len = strs.length;if(len == 0)return "";String prefixStr = strs[0];for(int i = 1; i < len; i++){//下面这个方法很关键,判断prefixStr是不是在strs[i]中下标为0的位置while(strs[i].indexOf(prefixStr) != 0){//将prefixStr最后面一个字符去掉prefixStr = prefixStr.substring(0, prefixStr.length() - 1);//如果为空,说明匹配失败if(prefixStr.length() == 0)return "";}}return prefixStr;}
}
LeetCode算法入门- Longest Common Prefix -day13相关推荐
- LeetCode算法入门- Longest Valid Parentheses -day12
LeetCode算法入门- Longest Valid Parentheses -day12 Given a string containing just the characters '(' and ...
- LeetCode算法入门- Longest Palindromic Substring-day5
LeetCode算法入门- Longest Palindromic Substring-day5 Longest Palindromic Substring Given a string s, fin ...
- LeetCode算法入门- Longest Substring Without Repeating Characters-day4
LeetCode算法入门- Longest Substring Without Repeating Characters-day4 Longest Substring Without Repeatin ...
- LeetCode - Easy - 14. Longest Common Prefix
Topic String Description https://leetcode.com/problems/longest-common-prefix/ Write a function to fi ...
- 【LeetCode】15.Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 从字符数组的第一项往后挨个 ...
- LeetCode in Python-14. Longest Common Prefix 最长公共前缀
Longest Common Prefix 最长公共前缀 题目 解法1.逐位比较 解法2.利用集合性质 解法3.zip+sets 解法4.借助os模块自带函数... 出处 题目 解法1.逐位比较 cl ...
- 【LeetCode算法题库】Day5:Roman to Integer Longest Common Prefix 3Sum
[Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
- LeetCode: 14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 大意就是,写一个函数可以找 ...
最新文章
- 计算机音乐情深深雨蒙蒙,情深深雨蒙蒙 MIDI File Download :: MidiShow
- 四十三、文件存储空间管理
- 延长端粒续命有风险,科学家警告:端粒过长反而容易患癌
- linux 编程博客,Linux系统编程博客参考
- 【渝粤题库】陕西师范大学500001 理论力学 作业(专升本)
- Linux: I/O多路转接之poll(有图有代码有真相!!!)
- Spring Boot中使用Redis数据库
- java项目打成war包,使用ide打包部署在tomcat中
- Spring Security(三十六):12. Spring MVC Test Integration
- Using OpenCV Java with Eclipse
- Mybatis3.4.x技术内幕(十七):Mybatis之动态Sql设计原本(上)
- java vm 参数及设置(转载)
- 移动端html富文本编辑器,PC/移动端的富文本编辑器wangEditor的使用
- SketchUp LayOut 剪贴簿制作技巧
- linux 桌面什么图标好看,Xenlism WildFire:Linux桌面下的一款漂亮图标主题
- 计算机二级office易错知识点,重要!!!计算机二级office易错点汇总
- 线性判别分析(Linear Discriminant Analysis)
- Python:1019 数字黑洞
- MongoDB中shell基本使用
- cocos3.X拖动精灵移动
热门文章
- Java LineNumberInputStream available()方法与示例
- 面试官:使用SpringBoot如何开发邮件发送系统?
- SqlServer时间戳与普通格式的转换
- MFC:2个重载中没有一个可以转换所有参数类型
- 【Jetson-Nano】2.Tensorflow和Pytorch的安装
- Pyalgotrade量化交易回测框架
- ASP.NET MVC 上传图片到项目目录中的文件夹并显示
- 计算机专业410分能上哪些大学,2021年高考410分能报什么学校
- python代码没有反应_没有任何编程经验者不要被Python简明手册误导。
- 行列式的计算机应用论文结论,【计算机应用论文】建筑耗能计算机模拟分析(共3654字)...