问题描述:
Write a function to find the longest common prefix string amongst an array of strings.
原问题链接:https://leetcode.com/problems/longest-common-prefix/
问题分析:
这个问题相对来说比较好解决。对于一组string来说,我们可以以第一个为基准,然后从它的第一个字符开始去和后面的比较,如果后面所有的string都包含有这个则比较下一个,直到已经达到后面string长度或者后面发现有不匹配的了。
public class Solution { public String longestCommonPrefix(String[] strs) { if(strs.length < 1) return ""; StringBuilder builder = new StringBuilder(""); for(int i = 0; i < strs[0].length(); i++) { char c = strs[0].charAt(i); for(int j = 1; j < strs.length; j++) { if(i >= strs[j].length() || strs[j].charAt(i) != c) { return builder.toString(); } } builder.append(c); } return builder.toString(); } }
这个问题里容易忽略的就是前后字符串的长度可能不一致,另外针对string列表长度也要注意一些特殊情况。
相关推荐
LeetCode Longest Common Prefix解决方案
Prefix 15 Three Sum 16 Three Sum Closest 20 Valid Parentheses 26 Remove Duplicates from Sorted Array 48 Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73
leetcode卡 LeetCode LeetCode题解 目录 字符串问题 ID Title C++ 难度 ...Common Prefix :star: 0038 Count and Say :star: 0043 Multiply Strings :star: :star: 大数相乘 0044 Wild Card Matchi
Common Prefix 15. 3Sum 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 25. Reverse Nodes in k-Group 26. Remove Duplicates from Sorted Array 27. Remove Element 28. Implement ...
LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7....
longest common prefix , 简单 valid number, hard, 用有限自动机 integer to roman ,easy , 模拟 roman to integer ,easy , 模拟 count and say , easy , 模拟 Anagrams 字符串处理,map Simplify Path,字符串处理,...
leetcode中文版 LeetCode # Title Chinese Tag Solution ...Common Prefix 最长公共前缀 string 16 3Sum Closest 最接近的三数之和 two pointers,array 21 Merge Two Sorted Lists 合并两个有序链表 lin
leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...
leetcode 2 sum c leetcode 力扣(Leetcode)编程题,JavaScript版本。...Prefix 简单 15 3Sum 中等 16 3Sum Closest 中等 17 Letter Combinations of a Phone Number DFS 中等 18 4Sum 中等 19 Remo
java lru leetcode Leetcode 问题的解决方案 问题 解决方案 0001_Two_Sum 0002_Add_Two_Numbers ...0014_Longest_Common_Prefix 0015_3总和 0016_3Sum_Closest 0017_Letter_Combinations_of_a_Phone_N
罗马数字转整数Easy14Longest Common Prefix153SumMedium163Sum ClosestMedium1717. 电话号码的字母组合Mediumjava184SumMedium19Remove Nth Node From End of ListJavaMediumJava20Valid ParenthesesJavaEasyJav
014_Longest_Common_Prefix 5 月 12 日。 2021年 Python 006 020_Valid_括号 5 月 12 日。 2021年 Python 007 021_Merge_Two_Sorted_Lists 2021 年 6 月 10 日 C# 008 125_Valid_Palindrome 2021 年 6 月 12 日 C#...
leetcode二维数组搜索leetcode 对于 Leetcode 和其他算法问题 文件描述 2_add_two_numbers.c : 两个数相加 3_solution.c : 无重复字符的最长子串 ..../14_longest_common_prefix.c : 最长公共前缀 ./19_Remo
# [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...
leetcode ...Prefix 015 3Sum 016 3Sum Closest 017 Letter Combinations of a Phone Number 018 4Sum 020 Valid Parentheses 022 Generate Parentheses 028 Implement strStr() 031 Next Permutat
判断链表是否为回文链表 leetcode Leetcode-解决方案 Leetcode问题的思考和解决方案 #Reverse Integer & Palindrome Number ...#longest common prefix 最长的公共前缀长度不会超过列表中最短的字符
14、longest Common Prefix 思路1:把每个位置上对应的字符放在set([])里,如果长度为1就加在res上,否则break ps:可以通过**startswith()**判断字符串是否以某几个字符开头 思路2:没天理之os.path.commonprefix...
这是我在的刷题集,详情参见__注释__。 未完待续!!! 已完成 27. Remove Element 14. Longest Common Prefix 9. Palindrome Number 7. Reverse Integer 1. Two Sum License The MIT License (MIT)
leetcode Python 001 leetcode的算法问题 这是我的解决方案,用 cpp 、 java 和 python 编写 #LeetCode 解决的问题: 001. Two Sum 002. Add Two ...Longest ...Longest ...Longest Common Prefix 019. R
Common Prefix 最长公共前缀 20 Valid Parentheses 有效的括号 26 Remove Duplicates from Sorted Array 删除排序数组中的重复项 32 Longest Valid Parentheses 最长有效括号 33 Search in Rotated Sorted Array ...