【LeetCode从零单排】No14.LongestCommonPrefix
来源:互联网 发布:淘宝规则的划分 编辑:程序博客网 时间:2024/04/29 04:31
题目
Write a function to find the longest common prefix string amongst an array of strings.
代码
public class Solution { public String longestCommonPrefix(String[] strs) { if(strs.length==0) return ""; if(strs.length==1) return strs[0]; String commonPrefix=""; boolean end=true; char strChar[]=strs[0].toCharArray(); ok: for (int j = 0;j<strChar.length;j++){ for(int i=1;i<strs.length;i++){ char tempChar[]=strs[i].toCharArray(); int tempChar_length=tempChar.length; if(j<tempChar_length){ if(tempChar[j]==strChar[j]){ } else{ break ok; } } else{ break ok; } } commonPrefix+=strChar[j]; } return commonPrefix; }}
代码下载:https://github.com/jimenbian/GarvinLeetCode
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/
0 0
- 【LeetCode从零单排】No14.LongestCommonPrefix
- 【LeetCode从零单排】No20.ValidParentheses
- 【LeetCode从零单排】No19.RemoveNthNodeFromEndofList
- 【LeetCode从零单排】No21.MergeTwoSortedLists
- 【LeetCode从零单排】No27.Remove Element
- 【LeetCode从零单排】No28Implement strStr()
- 【LeetCode从零单排】No36Valid Sudoku
- 【LeetCode从零单排】No38.CountAndSay
- 【LeetCode从零单排】No67.AddBinary
- 【LeetCode从零单排】No70.ClimbingStairs
- 【LeetCode从零单排】No112Path Sum
- 【LeetCode从零单排】No118Pascal's Triangle
- 【LeetCode从零单排】No189.Rotate Array
- 【LeetCode从零单排】No15 3Sum
- 【LeetCode从零单排】No221.Maximal Square
- 【LeetCode从零单排】No22.Generate Parentheses
- 从零单排1
- 从零单排2
- 素数判定总结
- 真正實現Windows XP、WIN7、WIN8下一鍵備份/還原註冊表
- vs2008 dll动态连接库不能生成.lib文件
- Leetcode_Palindrome Number
- android内存管理(三)
- 【LeetCode从零单排】No14.LongestCommonPrefix
- Redis的pipeline
- 在单独的js文件中开发Jquery的问题
- WAMPServer服务控制面板介绍...
- 别不信!App三年内将被HTML5顶替彻底消失?
- sizeWithFont:constrainedToSize:转boundingRectWithSize:options:
- Windows新装eclipse提示SDK要求高版本ADT的解决办法
- hdu 1013 Digital Roots 用一个大水题来纪念我进入杭电前一万名
- C string buffer的实现