码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode -- Day 20 & Day 21

时间:2015-07-30 11:04:48      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

Binary search in sorted Array

Question 1

Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Refer to this link below:
http://www.cnblogs.com/springfor/p/3861890.html
 1 public class Solution {
 2     public double findMedianSortedArrays(int[] nums1, int[] nums2) {
 3         if (nums1 == null || nums2 == null)
 4             return 0;
 5         int m = nums1.length;
 6         int n = nums2.length;
 7         int k = (m+n)/2;
 8         
 9         if ((m+n) % 2 == 0){
10             double first = findMedianSortedArrays(nums1,0,m-1,nums2,0,n-1,k);
11             double second = findMedianSortedArrays(nums1,0,m-1,nums2,0,n-1,k+1);
12             return (first+second)/2;
13         }
14         else
15             return findMedianSortedArrays(nums1,0,m-1,nums2,0,n-1,k+1);
16     }
17     
18     public double findMedianSortedArrays(int[] A, int sA, int eA, int[] B, int sB, int eB, int k){
19         int m = eA - sA + 1;
20         int n = eB - sB + 1;
21         if (m > n)
22             return findMedianSortedArrays(B, sB, eB, A, sA, eA, k);
23         if (m == 0)
24             return B[k-1];
25         if (k == 1)
26             return Math.min(A[sA], B[sB]);
27         int partA = Math.min(k/2, m);
28         int partB = k - partA;
29         if (A[sA + partA-1] < B[sB + partB - 1])
30             return findMedianSortedArrays(A, sA + partA, eA, B, sB, eB, k-partA);
31         else if (A[sA + partA-1] > B[sB + partB - 1])
32             return findMedianSortedArrays(A, sA, eA, B, sB+partB, eB, k-partB);
33         else
34             return A[sA + partA - 1];
35     }
36 }

 

Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

 

  • Integers in each row are sorted from left to right.
  • The first integer of each row is greater than the last integer of the previous row.

 

For example,

Consider the following matrix:

[
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]

Given target = 3, return true.


row and colum‘s product -1 is the total number of this values. And a trick here is mid/colum is the row and mid%colum is the column.

public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length < 1)
            return false;
        
        int left = 0;
        int right = matrix.length * matrix[0].length - 1;
        
        while (left <= right){
            int mid = (left + right)/2;
            int value = matrix[mid/matrix[0].length][mid % matrix[0].length];
            if (value == target){
                return true;
            }
            else if (value < target){
                left = mid + 1;
            }
            else {
                right = mid - 1;
            }
        }
        return false;
    }

This is my initial solution, using two loops, more complex but more directly.

 1 public boolean searchMatrix(int[][] matrix, int target) {
 2         if (matrix == null)
 3             return false;
 4             
 5         if (matrix[0][0] > target)
 6             return false;
 7         int row_mid;
 8         int col_mid;
 9         int row_start = 0;
10         int col_start = 0;
11         int row_end = matrix.length-1;
12         int col_end = matrix[0].length-1;
13         
14         while (row_start <= row_end){
15             row_mid = (row_start + row_end)/2;
16             // matrix[row_mid][] is the one we are interested in
17             if (matrix[row_mid][0] == target){
18                 return true;
19             }
20             else if (matrix[row_mid][0] > target){
21                 row_end = row_mid - 1; 
22             }
23             else {
24                 row_start = row_mid + 1;
25             }
26         }
27         
28         row_mid = row_end;
29         if (row_mid < 0)
30             return false;
31             
32         while (col_start <= col_end){
33             col_mid = (col_start + col_end)/2;
34             int value = matrix[row_mid][col_mid];
35             if (value == target){
36                 return true;
37             }
38             else if (value < target){
39                 col_start = col_mid + 1;
40             }
41             else{
42                 col_end = col_mid - 1;
43             }
44         }
45             
46         return false;
47     }

 

 

 

 

 

Leetcode -- Day 20 & Day 21

标签:

原文地址:http://www.cnblogs.com/timoBlog/p/4688332.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!