Leetcode-java (updated every day) and leetcode-java

Source: Internet
Author: User

Leetcode-java (updated every day) and leetcode-java

NOTE: If java is used, the performance is not optimal. Please refer to the following link for more information: http://www.ming-yue.cn/leetcode-java-solutions /.

First, give an existing leetcode answer. Why do you give the answer directly? because many of the answers are concise and not easy to understand, we suggest you do it yourself. The answer is just for reference at http://www.ninechapter.com/solutions /.

1, bytes.

Idea: Since the question assumes that there are two numbers a and B must be added together to obtain the target value c, then c-a must exist in the array, as a result, the question is how to efficiently check whether an array contains a certain value. Here we find some answers: http://www.diguage.com/archives/112.html. So we sorted it first, and then used the Arrays. binarySearch method. Then, we can modify the number of errors prompted by OJ. The Code is as follows: 254 ms, which is not the same as Chapter 9 and other solutions.

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. Merge (m + n ). There are two ideas: first, merge them into C, and then calculate the median. Second, find the median of AB and use the binary search to find the final result.

I first use the first easy-to-understand method AC. The answer is relatively simple:

// Solution1: merge first, and then calculate 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 subroutine 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}; double result = findMedianSortedArrays (A, B); System. out. println (result );}

Answer 2 is the solution in chapter 9 of the reference. After reading it, write it in silence for a while.

3, bytes.

This is relatively simple. The key point to be clear is that after the duplicate is identified, the next index of the duplicate character continues and should not be omitted.

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()>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 void main(String[] args) {String string = "dvdf";int result = lengthOfLongestSubstring(string);System.out.println(result);}}

4. Waiting for updates

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.