Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

done binarysearch1 #2172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions InfiniteSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Time Complexity :o(logn logm)
// Space Complexity :o(1)
// Did this code successfully run on Leetcode : it runs good in leat code
// Any problem you faced while coding this : no
public int search(ArrayReader reader, int target) {
int low = 0;
int high = 1;
if(reader.get(low) == target) return low;
while(reader.get(high) <= target){
low = high;
high *= 2;
}

while(low <= high){
int mid = (low + high) / 2;
if(reader.get(mid) == target) return mid;
if(reader.get(mid) > target){
high = mid - 1;
}
else{
low = mid + 1;
}
}

return -1;
}
}
41 changes: 41 additions & 0 deletions Matrix2d.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Time Complexity :o(logn)
// Space Complexity :o(1)
// Did this code successfully run on Leetcode : it runs good in leat code
// Any problem you faced while coding this : no
class Solution {

public boolean searchMatrix(int[][] matrix, int target) {

if(matrix == null || matrix.length == 0) return false;

int m = matrix.length; int n = matrix[0].length;

int low = 0; int high = m*n-1;

while(low <= high){

int mid = low + (high- low)/2;

int r = mid/n;

int c = mid%n;

if(matrix[r][c] == target) return true;

else if(matrix[r][c] > target){

high = mid - 1;

} else {

low = mid + 1;

}

}

return false;

}

}
39 changes: 39 additions & 0 deletions RotatedSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Time Complexity :o(logn)
// Space Complexity :o(1)
// Did this code successfully run on Leetcode : it runs good in leat code
// Any problem you faced while coding this : no

class Solution {
public int search(int[] nums, int target) {
int n=nums.length;
if(n==0|| nums==null){
return -1;
}
{
int low= 0; int high= n-1;
while(low<high){
int mid= low+(high-low)/2;
if(nums[mid] == target){
return mid;
} else if(nums[low]<=nums[mid]){
//left sorted

if(nums[low]<=target&&nums[mid]>target){
high=mid-1;
} else {
low=mid+1;
}
}else {
if(nums[mid]<target&&target<=nums[high]){
low=mid+1;
}else{
high=mid-1;
}
}

}
if(nums[low]==target)return low;
return -1;
}
}
}