Description: Choose Java, heavy in the experience, performance is not optimal. Welcome reprint: http://www.ming-yue.cn/leetcode-java-solutions/.
First give a leetcode already have the answer, why come up directly to answer, because this many answers written are very concise, not too easy to understand, or suggest to do their own first, the answer is only reference http://www.ninechapter.com/solutions/.
1,https://leetcode.com/problems/two-sum/, the main idea is to give an unordered array and a target value, assuming that the array has and only two numbers are added and the target value is equal, by index from small to large output these two numbers index, starting from 1 index.
Train of thought: Since the topic assumes that there are two numbers, a, and a must add to the target value C, then there must be c-a in the array, so the problem turns into how to efficiently check whether an array contains a value, here to find some answers http://www.diguage.com/archives/112.html. Then use the first sort, then use the Arrays.binarysearch method, and then according to the OJ hint of some errors, modify a few to be good. The specific code is as follows, 254ms, and nine chapters and other answers are not quite the same.
import java.util.arrays;public class solution1 { public static int[] Twosum (Int[] numbers, int target) { int[] num = numbers.clone (); arrays.sort (num); int size = num.length; int[] answers = new int[2]; for (int i=0;i<size;i++) { if ( Arrays.binarysearch (Num, target-num[i]) >0) { int count=0,index1 = 0,index2=0; for (int j=0;j<size;j++) { if (numbers[j]==num[i]| | Numbers[j]==target-num[i]) { count++; if (count==2) { index2=j; answers[0] = (INDEX1<INDEX2?INDEX1:INDEX2) +1; answers[1] = (index1> INDEX2?INDEX1:INDEX2) +1; break; } else { index1=j;} } } } } return answers; } public Static void main (String[] args) {int[] test = {- 3,4,3,90};int target = 0;int[] result = {0,0};result = twosum (Test, Target); System.out.println (result[0]+ "," +result[1]);}}
2,https://leetcode.com/problems/median-of-two-sorted-arrays/, the main idea is that there are two ordered array ab, the length of MN, to find the median of these two arrays, log (m+n). Two kinds of thinking, one is to merge into C, then the median number, the second is to find the median of AB, using two-point search to find the final result, the concept of http://zh.wikipedia.org/wiki/%E4%B8%AD%E4%BD%8D%E6%95%B8.
I first in the first easy to understand the way AC, the answer is relatively simple:
Solution1: Merge First, then find the median public static double findmediansortedarrays (int a[], int b[]) { int[] c= mergesortsub (A, &NBSP;B); double result=0; int n=C.length; if (n%2==0) { double m1=0.5*n-1; double m2= 0.5*n; result = 0.5* (C[(int) m1]+C[(int) m2 ]); }else {result = c[(int) Math.round (0.5* n-1)];} return result; } private static Int[] mergesortsub (INT[]&NBSP;ARR1,INT[]&NBSP;ARR2) {//merge sort sub-program if (arr1.length==0) {return arr2;} if (arr2.length==0) {return arr1;} int[] result = new int[arr1.length+arr2.length];int i = 0;int j = 0;int k = 0;while (True) {if (Arr1[i] < arr2[j]) {result[k] = arr1[i]; if (++i>arr1.length-1) {break;}} Else{result[k] = arr2[j];if (++j>arr2.length-1) {break;}} k++;} for (; i<arr1.length;i++) {result[++k] = arr1[i];} for (; j<arr2.length;j++) {result[++k] = arr2[j];} Return result;} Public static void main (String[] args) {int a[]={1,2,3,4};int b[]={5,6,7,8} ;d Ouble result = findmediansortedarrays (A, b); SYSTEM.OUT.PRINTLN (result);}
Answer 2 is a reference to the nine chapters in the solution, read and then after a period of time to write dictation.
3,https://leetcode.com/problems/longest-substring-without-repeating-characters/, the main idea is to find a string of non-repetition of the eldest son string.
This is relatively simple, the key point is to determine the repetition, from the next index of repeated characters continue to start, do not miss.
public class solution3 { public static int Lengthoflongestsubstring (string s) { int len = s.length (); if (len==0) { return 0; } String string = null; string substring = null; int maxLength = 0; for (int i=0;i<len;i++) { substring = string.valueof (S.charat (i)); if (i==0) { string = substring; substring = null; }else {if (!string.contains (subString)) {substring = string+substring;string = Substring;if (String.Length () >maxlength) {maxlength = string.length ();} Substring = null;} Else {int index = string.indexof (subString); Substring = string.substring (index+1 ) +substring;string = substring;if (String.Length () >maxlength) {Maxlength = string.length ( );} Substring = null;}} }return Maxlength; } public static&nbsP;void main (String[] args) {String string = "DVDF";int result = Lengthoflongestsubstring (string); SYSTEM.OUT.PRINTLN (result);}}
4, Waiting for updates
Leetcode-java (updated daily)