<> 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 .

©2019-2020 Toolsou All rights reserved,
Java Swing JList: List box components Accurate mobile phone number capture , Big data of operators The difference between memory overflow and memory leak , Causes and Solutions Website mobile phone number capture method stay Vue Use in Web Worker3 species Python data structure ,13 Creation methods , This is the conclusion , Great ! What's the difference in server prices ? Basic definition and use of function ‘ To be continued SQL Server Database Glossary Chrome OS, For programmers and Windows What does it mean ? Internet Marketing