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 要求:查找字符串数组中的最长公 ...
随机推荐
- TabIndex 属性 Tabindex="-1" 与Tabindex="0"、任意数字 (收录)
TabIndex 属性 Tabindex="-1" 与Tabindex="0".任意数字 html中的tabIndex属性可以设置键盘中的TAB键在控件中的移动 ...
- Go Methods and Interfaces
[Go Methods and Interfaces] 1.Go does not have classes. However, you can define methods on struct ty ...
- 04-SSH综合案例:环境搭建之jar包引入
刚才已经把表关系的分析已经分析完了,现在呢就先不去创建这个表,写到哪儿的时候再去创建这个表. 1.4 SSH环境搭建: 1.4.1 第一步:创建一个web项目. 1.4.2 第二步:导入相应jar包. ...
- Python3 hex() 函数
Python3 hex() 函数 Python3 内置函数 描述 hex() 函数用于将一个指定数字转换为 16 进制数. 语法 hex 语法: hex(x) 参数说明: x -- 一个整数 返回值 ...
- 13.Roman to Integer (HashTable)
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- 27.Remove Element(Array)
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- Stop单个Coroutine
public Coroutine StartCoroutine(string methodName, object value = null); Description Starts a corout ...
- c语言静态断言
在php中可以通过xdebug来显示详细的错误信息,可以细化到哪个文件哪行代码引起的报错.在C语言里面也可以通过静态断言(assert)来使得调试代码更加方便.关于断言,可以作为一种很强大的调试方式或 ...
- 通过curl模拟多线程抓取网页(curl_multi_*)
curl请求多个url,以前都是使用循环来处理.最近发现可以通过curl_multi_*系列函数来模拟多线程.比对一下,发现如果请求的url只有几个,2种方案耗时差不多,但是url比较多,差距就非常明 ...
- [C#] Delegate, Multicase delegate, Event
声明:这篇博客翻译自:https://www.codeproject.com/Articles/1061085/Delegates-Multicast-delegates-and-Events-in- ...