Leetcode-java (updated daily)

Source: Internet
Author: User
Tags repetition

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.

Import Java.util.arrays;public class Solution1 {public static int[] Twosum (int[] numbers, int target) {int[] num = n    Umbers.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,in        Dex1 = 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 = Sum (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 the median public static double findmediansortedarrays (int a[], int b[]) {int[] c= mergesortsub (A, 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[] arr1,int[] 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.

The code is as follows:

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 () >maxle Ngth) {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 void Main (string[] args) {string string = "DVDF"; int result = Lengthoflongestsubstring (string); SYSTEM.OUT.PRINTLN (result);}}

4, Waiting for updates

Leetcode-java (updated daily)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.