Skip to content

Commit

Permalink
added to Array
Browse files Browse the repository at this point in the history
  • Loading branch information
kdn251 committed Feb 16, 2017
1 parent cf0c2cc commit 0648aee
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Array/subsetsII.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Given a collection of integers that might contain duplicates, nums, return all possible subsets.
// Given a collection of integers that might contain duplicates, nums, return all possible subsets.

// Note: The solution set must not contain duplicate subsets.

Expand Down
30 changes: 30 additions & 0 deletions BinarySearch/closestBinarySearchTreeValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.

// Note:
// Given target value is a floating point.
// You are guaranteed to have only one unique value in the BST that is closest to the target.

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int closestValue(TreeNode root, double target) {

int value = root.val;
TreeNode child = root.val < target ? root.right : root.left;

if(child == null) return value;

int childValue = closestValue(child, target);

return Math.abs(value - target) < Math.abs(childValue - target) ? value : childValue;

}

}
29 changes: 29 additions & 0 deletions BinarySearch/firstBadVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

// Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.

// You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */

public class Solution extends VersionControl {

public int firstBadVersion(int n) {

int start = 1;
int end = n;

while(start < end) {

int mid = start + (end - start) / 2;
if(!isBadVersion(mid)) start = mid + 1;
else end = mid;

}

return start;

}

}
46 changes: 46 additions & 0 deletions BinarySearch/guessNumberHigherOrLower.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// We are playing the Guess Game. The game is as follows:

// I pick a number from 1 to n. You have to guess which number I picked.

// Every time you guess wrong, I'll tell you whether the number is higher or lower.

// You call a pre-defined API guess(int num) which returns 3 possible results (-1, 1, or 0):

// -1 : My number is lower
// 1 : My number is higher
// 0 : Congrats! You got it!
// Example:
// n = 10, I pick 6.

// Return 6.

/* The guess API is defined in the parent class GuessGame.
@param num, your guess
@return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num); */

public class Solution extends GuessGame {

public int guessNumber(int n) {

return binarySearch(1, n);

}

private int binarySearch(int start, int end) {

if(start > end) return -1;

if(guess(start) == 0) return start;
if(guess(end) == 0) return end;

int mid = start + (end - start) / 2;

if(guess(mid) == 0) return mid;

else if(guess(mid) == 1) return binarySearch(mid + 1, end);
else return binarySearch(start, mid - 1);

}

}
30 changes: 30 additions & 0 deletions BinarySearch/pow(x,n).java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Implement pow(x, n).

public class Solution {

public double myPow(double x, int n) {

if(n == 0) {

return 1;

}

if(Double.isInfinite(x)) {

return 0;

}

if(n < 0) {

n = -n;
x = 1 / x;

}

return n % 2 == 0 ? myPow(x * x, n / 2) : x * myPow(x * x, n / 2);

}

}
28 changes: 28 additions & 0 deletions BinarySearch/sqrt(x).java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Implement int sqrt(int x).

// Compute and return the square root of x.

public class Solution {

public int mySqrt(int x) {

if(x == 0) return 0;

int left = 1;
int right = x;

while(left <= right) {

int mid = left + (right - left) / 2;

if(mid == x / mid) return mid;
else if(mid > x / mid) right = mid - 1;
else if(mid < x / mid) left = mid + 1;

}

return right;

}

}

0 comments on commit 0648aee

Please sign in to comment.