<> Title Description

It shouldn't be hard to follow intuition , Is it traversal , Then compare them one by one , Don't talk much , Just start writing code , Then debug the boundary conditions .

<> First attempt

My idea is : Two floors for loop , The outer layer controls the character of each string , How many strings is memory control . But here comes the question , The outer layer controls the number of characters , The boundary is not easy to determine , The shortest string in the array of characters , But in that case , I thought I needed to go through it one more time , One more traversal , Just to find the shortest string , I think it's wasteful .

I'll assume that the first string in the string array is the shortest , If only it was , If not , I'll use exception handling , Once an exception is caught , That means the border has been reached . Return the string that needs to be returned directly .
public String longestCommonPrefix(String[] strs) { if (strs == null || strs.
length== 0 ) return ""; int len; if ((len = strs.length) ==1) return strs[0];
StringBuilder re= new StringBuilder(); if ("".equals(strs[0])) return ""; for (
int i = 0; i < strs[0].length(); i++) { try { char temp = strs[0].charAt(i); for
(int j = 1; j < len; j++) { if ("".equals(strs[j])) return ""; if (strs[j].
charAt(i) != temp) { return re.toString(); } } re.append(temp); } catch (
Exception e) { return re.toString(); } } return re.toString(); }
The main logic is what I said above , The other is the treatment of some boundary conditions , It's not hard .

<> Submit results

Hoho , I didn't expect that the effect was good . But I don't know how to use exception handling mechanism to avoid one more loop operation , Is it a violation .

<> Big guy idea

It's a little easier to scan horizontally and vertically , It's a comparison of traversal . What divide and conquer , Is it too hard to score two , Logic takes a long time , Forget it, forget it .

Technology
©2019-2020 Toolsou All rights reserved,
c Language implementation 《 Student management system 》 No hole is the future of mobile phone ? There are still many problems to be solved Junior , A little sense , Just for mutual encouragement How to use Vue Used in Echarts Visual Library The 11th Blue Bridge Cup Java The second provincial competition B Group part Python- be based on OpenCV Contour fill for flooding algorithm hole filling 【C#】 The realization of student achievement information management system List Common interview questions in the collection and simple ideas China Mobile Science Popularization : Why do mobile networks call “ Cellular mobile network ”【Golang Basic series 10 】Go language On conditional sentences if