leecode刷题(19)-- 最长公共前缀
leecode刷题(19)-- 最长公共前缀
最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 ""。
示例 1:
输入: ["flower","flow","flight"]
输出: "fl"
示例 2:
输入: ["dog","racecar","car"]
输出: ""
解释: 输入不存在公共前缀。
说明:
所有输入只包含小写字母 a-z 。
思路:
这道题我用的是暴力破解的方法,遍历字符串数组,依次比较每个字符,如果都相等,则长度加一再比较,如果不相等,则返回之前的字符。
代码如下:
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs.length == 0) return "";
StringBuilder res = new StringBuilder();
for (int i = 0; i < strs[0].length(); i++) {
int j = 1;
for (; j < strs.length; j++) {
if (strs[j].length() <= i || strs[0].charAt(i) != strs[j].charAt(i)) {
return res.toString();
}
}
if (j == strs.length) {
res.append(strs[0].charAt(i));
}
}
return res.toString();
}
}
几个知识点
1. StringBuilder :
String 类是字符串常量,是不可更改的常量。而 StringBuffer 是字符串变量,它的对象是可以扩充和修改的。
所以我们在这里使用 StringBuffer 可以避免每次添加字符时都要 new 一个新对象。
2. 字符串数组中字符的定位:
字符串数组即形如这样的数组:
String[] res = {"abc", "def", "ghi"}。比如我们找 "abc" 这个字符串,可以直接写:
res[0],但是如果我们要找 "abc" 中的 a,那我们应该怎样写呢?如果写成
res[0][0],在 C++ 中是没有问题的,但是在 java 中会报错:array required, but String found。我们在 java 中应该写成:
res[0].charAt(0)。
官方题解
官方有更好的方法,看了确实很好,帮助很大,这道题的解题思路不唯一:官方题解 。
leecode刷题(19)-- 最长公共前缀的更多相关文章
- C#LeetCode刷题之#14-最长公共前缀(Longest Common Prefix)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3921 访问. 编写一个函数来查找字符串数组中的最长公共前缀. 如 ...
- LeetCode第14题:最长公共前缀
题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow ...
- leecode第十四题(最长公共前缀)
class Solution { public: string longestCommonPrefix(vector<string>& strs) { string res=&qu ...
- Leecode刷题之旅-C语言/python-14.最长公共前缀
/* * @lc app=leetcode.cn id=14 lang=c * * [14] 最长公共前缀 * * https://leetcode-cn.com/problems/longest-c ...
- #leetcode刷题之路14-最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- LeetCode刷题-最长公共前缀(简单)
题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow ...
- 好像leeceode题目我的博客太长了,需要重新建立一个. leecode刷题第二个
376. Wiggle Subsequence 自己没想出来,看了别人的分析. 主要是要分析出升序降序只跟临近的2个决定.虽然直觉上不是这样. 455. 分发饼干 ...
- [LeeCode]14. 最长公共前缀
题目链接:https://leetcode-cn.com/problems/longest-common-prefix/ 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀 ...
- Leetcode题库——14.最长公共前缀
@author: ZZQ @software: PyCharm @file: longestCommonPrefix.py @time: 2018/9/16 17:50 要求:查找字符串数组中的最长公 ...
随机推荐
- 光圈、曝光、ISO
光圈大小对景深的影响: 光圈大小示意图(值越小光圈越大) 光圈.曝光.ISO对图像效果影响
- Selection Tools
[Selection Tools] 1.Marquee Tools. OptionBar其中四年控件涵意如下: 2.Magnetic Lasso Tool,根据属标轨迹自动画点. 3.Magic Wa ...
- tar使用
[tar使用] 1..tar.gz文件 压缩:tar -czvf dstFileName.tar.gz a.txt b.txt …… 解压:tar -xzvf fileName.tar.gz 2..t ...
- 118. Pascal's Triangle (Array)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- 18.4Sum (Map)
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- mybatis开发Dao的Mapper动态代理方式
1. 开发规范Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体跟Dao原始方法中接口实现类的方法相 ...
- [Selenium]对于某些对话框即有可能弹出来,也有可能不弹出来,这种应该怎么处理呢?
界面上如果有一个对话框可能弹出来,也可能不弹出,我们都要认为是正常,应该怎么处理呢? /** * check if release notes dialog present * @author j * ...
- Android应用开发环境的搭建和使用
主要包括Android SDK.Android开发工具:也包括如何使用Android提供的ADB.DDMS.AAPT.DX等工具,掌握这些工具是开发Android应用的基础技能. 1.Android的 ...
- CentOS 7 装好系统一些优化
1.禁用SELINUX vi /etc/sysconfig/selinux 设置为disabled 2.同步时间*/20 * * * * /usr/sbin/ntpdate pool.ntp.org ...
- 马婕 2014MBA专硕考试 词汇每日一练(转)
2013-6-8 1. To ensure its sustained progress in economy, the government has _______ a series of poli ...