LeetCode-Java (一)

前言

每天五道LeetCode,算是为了明年的春招做点准备吧。在此分享出来和大家一起学习,为了方法名不重复,我在每个方法后面写上了题号吧。如果有什么疑问可以在下面评论,我看到了会及时回复的。
好吧,今天是第一天。

Remove Duplicates from Sorted Array 1

Given a sorted array, remove the duplicates in place such that each element appear only
once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example, Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].

/**
     * 2.1.1 Remove Duplicates from Sorted Array 1
     */
    public static int removeDuplicates211(int[] A) {
        int N = A.length;
        if (N == 0) {
            return 0;
        }
        int index = 0;
        for (int i = 1; i < N; i++) {
            if (A[index] != A[i]) {
                A[++index] = A[i];
            }
        }
        return index + 1;
    }

    public static void removeDuplicates211Test() {
        int[] A = {1, 1, 2};
        int index = removeDuplicates1(A);
        System.out.println("index: " + index);
    }

Remove Duplicates from Sorted Array 2

Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice?
For example, Given sorted array A = [1,1,1,2,2,3],
Your function should return length = 5, and A is now [1,1,2,2,3]


    /**
     * 2.1.2 Remove Duplicates from Sorted Array 2
     */
    public static int removeDuplicates212(int[] A) {
        int N = A.length;
        if (N == 0) {
            return 0;
        }
        int index = 0;
        int count = 0;
        for (int i = 1; i < N; i++) {
            if (A[index] == A[i]) {
                count++;
                if (count < 2) {
                    A[++index] = A[i];
                }
            } else {
                A[++index] = A[i];
                count = 0;
            }
        }
        return index + 1;
    }

    public static void removeDuplicates212Test() {
        int[] A = {1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4};
        int index = removeDuplicates2(A);
        System.out.println("index: " + index);
    }

Search in Rotated Sorted Array

Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.

   /**
     * 2.1.3 Search in Rotated Sorted Array
     */
    public static int search213(int[] A, int target) {
        int N = A.length;
        if (N == 0) {
            return N;
        }
        int first = 0;
        int last = N-1;
        int mid = 0;
        while (first <= last) {
            mid = (first + last) / 2;
            System.out.println("mid " +mid);
            if (A[mid] == target) {
                return mid;
            }
            if (A[first] <= A[mid]) {
                if (A[first] <= target && target < A[mid]) {
                    last = mid - 1;
                } else {
                    first = mid + 1;
                }
            } else {
                if (A[mid] < target && target <= A[last]) {
                    first = mid+1;
                } else {
                    last = mid - 1;
                }
            }
        }
        return -1;
    }

    public static void search213Test() {
        int[] A = {4,5,6,7,8,9,0,1,2};
        int index = search213(A,2);
        System.out.println("index : " +index);
    }

Search in Rotated Sorted Array 2

Follow up for ”Search in Rotated Sorted Array”: What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.

    /**
     * 2.1.4 Search in Rotated Sorted Array 2
     */
    public static int search214(int[] A, int target){
        int first = 0;
        int last = A.length-1;
        int mid = 0;
        while (first <= last) {
            mid = (first + last)/2;
            if (A[mid] == target) {
                return mid;
            }
            if (A[first] < A[mid]) {
                if (A[first] <= target && target < A[mid]) {
                    last = mid - 1;
                } else {
                    first = mid + 1;
                }
            } else if (A[first] == A[mid]) {
                first++;
            } else {
                if ( A[mid] < target && target <= A[last]) {
                    first = mid +1;
                } else {
                    last = mid -1;
                }
            }

        }
        return -1;
    }
    public static void search214Test(){
        int[] A = {1,3,1,1,1};
        int index = search214(A,1);
        System.out.println("index : "+index);
    }

Median of Two Sorted Array

ere are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted
arrays. e overall run time complexity should be O(log(m + n)).
    //下面这种方法虽然可以得到正确结果,但是不满足O(log(m + n))条件
    /**
     * 2.1.5 Median of Two Sorted Array
     */
    public static int findMedianSortedArrays(int[] A, int[] B){
        if (A == null||B == null) {
            return -1;
        }
        int K = (A.length+B.length)/2-1;
        int median = 0;
        int m = 0;
        int n = 0;
        int prev_median =0;
        while ((m+n) <= K) {
            prev_median = median;
            if (A[m] < B[n]) {
                m++;
                median = B[n];
            } else if (A[m] > B[n]) {
                n++;
                median = A[m];
            } else {
                m++;
                n++;
                median = A[m];
            }
        }
        if ( K %2 != 0) {
            return (prev_median + median)/2;
        } else {
            return median;
        }
    }
    public static void findMedianSortedArraysTest(){
        int[] A = {1,2,3,4,7,9};
        int[] B = {1,3,6,7,8};
        int median = findMedianSortedArrays(A,B);
        System.out.println("median : "+median);
    }
    //要想满足O(log(m + n))条件,可以尝试使用二分法去做
    /**
     * 2.1.5 Median of Two Sorted Arrays
     *
     */
    public static int findMedianSortedArrays(int[] A, int[] B){
        int m = A.length;
        int n = B.length;
        int length = m+n;
        if (length %2 != 0) {   //奇数
            return findKth(A,0,m-1,B,0,n-1,length/2+1);
        } else {    //偶数
            return findKth(A,0,m-1,B,0,n-1,length/2)
                    + findKth(A,0,m-1,B,0,n-1,length/2+1);
        }
    }
    public static int findKth(int[] A, int aStart, int aEnd, int[] B, int bStart, int bEnd, int k){
        int m = aEnd-aStart+1;
        int n = bEnd-bStart+1;
        if (m>n){
            return findKth(B,bStart,bEnd,A,aStart,aEnd,k);
        }
        if (m ==0) {
            return B[k-1];
        }
        if (k == 1) {
            return Math.min(A[aStart],B[bStart]);
        }
        int aPart = Math.min(k/2,m);
        int bPart = k-aPart;
        if (A[aStart+aPart-1] > B[bStart+bPart-1]) {
            return findKth(A,aStart,aEnd,B,bStart+bPart,bEnd, k-bPart);
        } else if (A[aStart+aPart-1] < B[bStart+bPart-1]) {
            return findKth(A, aStart + aPart, aEnd, B, bStart, bEnd, k-aPart);
        }else {
            return  A[aStart+aPart-1];
        }
    }
    public static void findMedianSortedArraysTest(){
        int[] A = {1,2,3,5,7};
        int[] B = {1,3,6,7,7,8};
        int median = findMedianSortedArrays(A,B);
        System.out.println("median : "+median);
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值