【LeetCode】14.Longest Common Prefix(Easy)解题报告
2017-12-19 10:04
435 查看
【LeetCode】14.Longest Common Prefix(Easy)解题报告
tags:String
题目地址:https://leetcode.com/problems/longest-common-prefix/description/
题目描述:
Write a function to find the longest common prefix string amongst an array of strings.
找出字符串序列中重复的字符串。提交了好几次,因为越界问题,要提前取到最小长度就可以了。还有不要忘异常判断。
Solutions:
Date:2017年12月19日
tags:String
题目地址:https://leetcode.com/problems/longest-common-prefix/description/
题目描述:
Write a function to find the longest common prefix string amongst an array of strings.
找出字符串序列中重复的字符串。提交了好几次,因为越界问题,要提前取到最小长度就可以了。还有不要忘异常判断。
Solutions:
class Solution { public String longestCommonPrefix(String[] strs) { if(strs.length==0){ return ""; } int len=strs[0].length(); for(int i=0;i<strs.length;i++){ len=Math.min(strs[i].length(),len); } for(int i=0;i<strs.length-1;i++){ for(int j=0;j<len;j++){ if(strs[i].charAt(j)!=strs[i+1].charAt(j)){ len=j; } } } return strs[0].substring(0,len); } }
Date:2017年12月19日
相关文章推荐
- 【LeetCode】235.Lowest Common Ancestor of a Binary Search Tree(Easy)解题报告
- [LeetCode 解题报告]014. Longest Common Prefix
- LeetCode: Longest Common Prefix 解题报告
- LeetCode解题报告--Longest Common Prefix
- [LeetCode] Longest Common Prefix 解题报告
- [LeetCode] Longest Common Prefix 解题报告
- Leetcode Longest Common Prefix 解题报告
- [LeetCode] Longest Common Prefix 解题报告
- leetCode 14.Longest Common Prefix(最长公共前缀) 解题思路和方法
- 【LeetCode】Longest Common Prefix 解题报告
- Leetcode 14. Longest Common Prefix (Easy) (cpp)
- 【LeetCode】448.Find All Numbers Disappeared in an Array(Easy)解题报告
- 【LeetCode】404.Sum of Left Leaves(Easy)解题报告
- 【LeetCode】108.Convert Sorted Array to Binary Search Tree(Easy)解题报告
- LeetCode解题报告 70. Climbing Stairs [easy]
- 【LeetCode】189.Rotate Array(Easy)解题报告
- 【LeetCode】Lowest Common Ancestor of a Binary Search Tree 解题报告
- [Leetcode] 236. Lowest Common Ancestor of a Binary Tree 解题报告
- [leetcode] 236. Lowest Common Ancestor of a Binary Tree 解题报告
- 【LeetCode】236.Lowest Common Ancestor of a Binary Tree(Medium)解题报告