Directory:
- Summarize
- Thinking and realization
- Some of my own notes about Java basics
1. Summary
- The habit now is to consider the correct case first and then consider the boundary condition
- Write a comment
- Leetcode Code Submission Method: it is necessary to implement one of its member functions according to the interface given by class solution. This function can be called in main to test.
- When writing loops, consider the condition of "jumping out of the loop"
2. Ideas and realization
1> Write a function to find the longest common prefix string amongst an array of strings.
Finds the longest common prefix string from a string array. Input: string array strs[]; output: Longest, common prefix string commonprefix.
e.g.
2> selects one of the strs[] as the commonprefix and then compares it to other Str. So which STR to choose as the initial commonprefix? The first use of strs[0].
There is a problem, when we compare to ABC in strs[], we will have to truncate Commonprefix (ABCEFGH) to a large part (EFGH), which means that the comparison (EFGH) with the previous strs is not valid.
So which STR should be chosen as the initial commonprefix?
Required output: The longest, common prefix string, then it must be included by all Str: It is also the shortest substring of str.
So, the shortest initial value of STR---Commonprefix.
3> now have to compare the Commonprefix, then began to compare Bai. At first I was so cyclic strs[] compared:
for (I < strs.length) {2 for (J < Commonprefix.length ()) {3 // some statements4 } 5}
Dual for Loop, O (T) = O (length of shortest str x strs[] array length)
1 Public classSolution {2 PublicString Longestcommonprefix (string[] strs) {3//Note the boundary condition: the input is empty4if(Strs.length = = 0) { 5return""; 6 } 7 8 String Commonp Refix =NewString (); 9 Commonprefix = Strs[0]; 10intMinleng = Strs[0].length (); 11intMinlengindex = 0; 12//Save the least-length str to Commonprefix13 for(inti = 1; i < strs.length; i++) { 14if(Minleng <strs[i].length ()) { 15Continue; 16}Else { Minleng =strs[i].length (); Commonprefix =Strs[i]; Minlengindex =i; 20 } 21st } 22 23//cyclic remaining str, compared to commonprefix, with common prefixes stored to commonprefix24 for(inti = 0; i < strs.length; i++) { 25if(i = =Minlengindex) { 26Continue; 27 } 28 29//for each STR starting with the first character, take a substring comparison30 for(intj = 0; J < Commonprefix.length (); J + +) { System.out.println (Commonprefix + ":" 32 + strs[i].substring (0, J + 1)); 33if(commonprefix.substring (0, J + 1). Equals (Strs[i].substring (0, J + 1))) { 34//if same, move one character to the right and take the prefix substring to continue the comparison35Continue; 36}Else { 37//if different, intercept the same prefix substring instead of commonprefix; jump out of this str and compare the next str38if(j = = 0) { Commonprefix = commonprefix.substring (0, 1); 40}Else { Commonprefix = commonprefix.substring (0, J); 42 } 43 Break; 44 } 45 } 46 } 47returnCommonprefix; 48 }49}
View Code
Then, time Limit exceed.
4> topic AC Not, of course, things are not finished. How to simplify this double cycle? Next I refer to this code (click to see the original text).
The above has been analyzed, commonprefix must be the shortest substring of str, we only need to loop the shortest str(that is, the initial value of Commonprefix), the loop variable is its length, and strs[] in turn, do not match , the Commonprefix end character is truncated (the length becomes shorter). So what happens when we jump out of the loop ?
One is that the Commonprefix is completely truncated (length 0, no matching prefix substring);
The second is that it has cycled through strs[].
notice! 1. Cyclic variable; 2. Conditions for jumping out of a loop
1 /**2 * Longest Common Prefix3 * Write a function to find the longest common prefix string amongst an array of strings.4 * 2015/6/145 * */6 7 Public classLongestcommonprefix {8 Public StaticString Longestcommonprefix (string[] strs) {9 //Note the boundary condition: the input is emptyTen if(Strs.length = = 0) { One return""; A } - -String Commonprefix =NewString (); theCommonprefix = Strs[0]; - intMinleng = Strs[0].length (); - //Save the least-length str to Commonprefix - for(inti = 1; i < strs.length; i++) { + if(Minleng >strs[i].length ()) { -Minleng =strs[i].length (); +Commonprefix =Strs[i]; A } at } - - intEndIndex =commonprefix.length (); - inti = 0; - //jump out of the loop condition: Commonprefix is completely truncated, or cycle through play strs[] - while(EndIndex > 0 && i <strs.length) { in if(commonprefix.substring (0, EndIndex). Equals ( -Strs[i].substring (0, EndIndex))) { to //if matched, the next str is compared +i++; -}Else { the //does not match, then Commonprefix truncates the end character and continues to compare the same str *endindex--; $ }Panax Notoginseng } - the //Commonprefix is completely truncated, there is no matching prefix substring + if(EndIndex = = 0) { A return""; the } + - //take the substring, commonprefix is not really cut off $ returnCommonprefix.substring (0, EndIndex); $ } - - Public Static voidMain (string[] args) { thestring[] STRs =NewString[] {"Abac", "ABA", "ABC" }; -System.out.println ("--" +Longestcommonprefix (STRs));Wuyi } the}
View Code
O (T) = O (max (strs[] array length, shortest str length))
Accepted.
3. Some of my own notes, about Java basics
- Main (); class method
- Java determines whether strings are equal
- Three-dimensional expression
[Leetcode] Longest Common Prefix