当前位置:网站首页>Leetcode:14. Longest common prefix [thinking + sorting]
Leetcode:14. Longest common prefix [thinking + sorting]
2022-07-20 10:04:00 【Starry sky and bright moon】
Title Description
Write a function to find the longest common prefix in the string array .
If no common prefix exists , Returns an empty string “”.
Input and output
Input :strs = [“flower”,“flow”,“flight”]
Output :“fl”
Input :strs = [“dog”,“racecar”,“car”]
Output :""
explain : Input does not have a common prefix .
Tips
- 1 <= strs.length <= 200
- 0 <= strs[i].length <= 200
- strs[i] It's only made up of lowercase letters
Ideas
If we can sort strings , Then we only need to judge the public prefix of the first string and the last string , Because the longest common prefix depends on the first string and the last string , And the middle string will not affect .
AC Code (C++)
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
sort(strs.begin(), strs.end());
string first = strs.front();
string end = strs.back();
string res = "";
for (int i = 0; i < first.size() && i < end.size(); ++i) {
if (first[i] != end[i]) {
break;
}
res += first[i];
}
return res;
}
};
AC Code (Java)
class Solution {
public String longestCommonPrefix(String[] strs) {
Arrays.sort(strs);
String first = strs[0];
String end = strs[strs.length - 1];
String res = "";
for (int i = 0; i < first.length() && i < end.length(); ++i) {
if (first.charAt(i) != end.charAt(i)) {
break;
}
res += first.charAt(i);
}
return res;
}
//Array.sort(strs) Is the default sort , You can choose a sorting interval .
}
AC Code (Python)
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
strs.sort()
first, end = strs[0], strs[-1]
i, res = 0, ""
while i < len(first) and i < len(end):
if first[i] != end[i]:
break
else:
res += first[i]
i += 1
return res
//strs.sort() Sort the original list , The original table has changed , and sorted(strs) The function returns an object .
边栏推荐
- 基于定时器捕获功能的红外解码程序(NEC协议)
- 基于循环卷积的一维小波变换程序验证(C语言)
- FPGA decoder + decoder (including code)
- 乐山师范程序设计大赛2020-E: 分石头【01背包】
- 代码审计之oasys系统
- (data and electricity) summary of various triggers - FPGA octet (1)
- 使用小技巧(一)
- Leshan normal programming competition 2020-b: design web page [prime number]
- Quanzhi t507 realizes the whole process of SPI to can
- 提升开发效率的 Chrome 开发者工具快捷键参考
猜你喜欢
Openstack uses dashboard to perform image operations, manage instances, and other operations
Code audit system
Depth evaluation of Ruixin micro rk3568 development board
离散数据(数组)的过零位置搜索
FPGA skimming P3: 4-bit numeric comparator circuit, 4bit carry ahead adder circuit, priority encoder circuit, priority encoder
方向信号的表达——复指数信号
关于浮点数的剪不断理还乱
2022-7-11 第八小组 顾宇佳 学习笔记(Js)
线性卷积、循环卷积、周期卷积的定义、计算方法及三者之间的关系
Verilog HDL language summary (full)
随机推荐
基于定时器捕获功能的红外解码程序(NEC协议)
Pearson correlation coefficient and code implementation (C language +matlab)
FPGA majority voter (including code)
Add directory navigation to personal blog website articles
i. Mx8mp development board porting USBWiFi rtl8192eu driver
[markdown] about markdown, I want to say this~
ESM测向误差对定位误差的影响分析
详细讲解JS中的加法(+)运算,基本数据类型相加,引用数据类型相加底层的运算规则,[]+{},{}+[]
Least square linear fitting and its code implementation (C language)
FPGA network port implementation and detailed explanation (3)
FPGA skimming p2: multifunctional data processor, calculate the difference between two numbers, use generate For statement simplifies the code, uses sub modules to realize the size comparison of three
How to set the oil on the through hole cover when exporting the Gerber file of PCB
"Embedded intelligence" constantly empowers medical devices
Depth evaluation of Ruixin micro rk3568 development board
7-1 懂的都懂
Detailed explanation of tunnel and agent usage in SSH protocol
Leetcode:13.罗马数字转整数【键值对映射】
Ktor 2.0?半香不香的尴尬
PCL基本操作大全
Leetcode:14. 最长公共前缀【思维+排序】