新博文地址:[leetcode]Longest Common Prefix
http://oj.leetcode.com/problems/longest-common-prefix/
Write a function to find the longest common prefix string amongst an array of strings.
其实木有必要求出数组最短字符串的长度的,加一个判断即可
public String longestCommonPrefix(String[] strs){ if(strs == null || strs.length == 0){ return ""; } StringBuilder sb = new StringBuilder(); //------------get shortestLength of strs thus, longest prefix int shortestLength = Integer.MAX_VALUE; for(String tem : strs){ if(tem.length()< shortestLength){ shortestLength = tem.length(); } } breakable: for(int i = 0; i < shortestLength; i++){ char tem = strs[0].charAt(i); for(int j = 1; j < strs.length; j++){ if(strs[j].charAt(i)!=tem){ break breakable; } } sb.append(tem+""); } return sb.toString(); }
相关推荐
LeetCode Longest Common Prefix解决方案
Common Prefix 最长公共前缀 20 Valid Parentheses 有效的括号 26 Remove Duplicates from Sorted Array 删除排序数组中的重复项 32 Longest Valid Parentheses 最长有效括号 33 Search in Rotated Sorted Array ...
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
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中文版 LeetCode # Title Chinese Tag Solution ...Common Prefix 最长公共前缀 string 16 3Sum Closest 最接近的三数之和 two pointers,array 21 Merge Two Sorted Lists 合并两个有序链表 lin
# [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...
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
leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...
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....
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
longest common prefix , 简单 valid number, hard, 用有限自动机 integer to roman ,easy , 模拟 roman to integer ,easy , 模拟 count and say , easy , 模拟 Anagrams 字符串处理,map Simplify Path,字符串处理,...
leetcode卡 LeetCode LeetCode题解 目录 字符串问题 ID Title C++ 难度 ...Common Prefix :star: 0038 Count and Say :star: 0043 Multiply Strings :star: :star: 大数相乘 0044 Wild Card Matchi
leetcode Python 001 leetcode的算法问题 这是我的解决方案,用 cpp 、 java 和 python 编写 #LeetCode 解决的问题: 001. Two Sum 002. Add Two ...Longest ...Longest ...Longest Common Prefix 019. R
lru cache leetcode #算法解题报告 主要记录我每天做的题目,包括leetcode, ...Common Prefix 15.3Sum 16.3Sum Closest 19.Remove Nth Node From End of List 20.Valid Parentheses 21.Merge Two Sorted L
14、longest Common Prefix 思路1:把每个位置上对应的字符放在set([])里,如果长度为1就加在res上,否则break ps:可以通过**startswith()**判断字符串是否以某几个字符开头 思路2:没天理之os.path.commonprefix...
Prefix(最长公共前缀) 编写一个函数来查找字符串数组中的最长公共前缀,如果不存在公共前缀,返回空字符串 ""。 Valid Parentheses(有效的括号) 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串...
判断链表是否为回文链表 leetcode Leetcode-解决方案 Leetcode问题的思考和解决方案 #Reverse Integer & Palindrome Number ...#longest common prefix 最长的公共前缀长度不会超过列表中最短的字符
Longest Common Prefix 运行时间:40 毫秒内存使用:13.9 MB 20. Valid Parentheses 运行时间:40 毫秒内存使用:13.8 MB 22. Generate Parentheses 运行时间:164 毫秒内存使用:22.5 MB 26. Remove Duplicates ...
罗马数字转整数Easy14Longest Common Prefix153SumMedium163Sum ClosestMedium1717. 电话号码的字母组合Mediumjava184SumMedium19Remove Nth Node From End of ListJavaMediumJava20Valid ParenthesesJavaEasyJav
leetcode 答案programming-language-learning Javacript 复习基本DOM操作 C ...Longest Common Prefix 记得要判断strsSize = 0 && 1 的情况即可刚刚一直没判断会出现segment fault Python PHP Java