Day1 Arrays - My LeetCode Diary
int[] myNum = {10, 20, 30, 40};
//Loop Through an Array
for (int i = 0; i < myNum.length; i++) {
System.out.println(myNum[i]);
}
//OR
for (int i : myNum) {
System.out.println(i);
}
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
System.out.println(myNumbers[1][2]); // Outputs 7
//Loop Through Multi-Dimensional Arrays
for (int i = 0; i < myNumbers.length; ++i) {
for(int j = 0; j < myNumbers[i].length; ++j) {
System.out.println(myNumbers[i][j]);
}
}
Binary Search Notes
Prerequisites
- Sorted Arrays
- No Duplicates
Left close, right close[left, right]
- while (left <= right) use <=
- if (nums[middle] > target) right = middle - 1, because num[mid] will not equal to target number, the right index could be mid - 1.
- O(log n)
class Solution {
public int search(int[] nums, int target) {
if (target < nums[0] || target > nums[nums.length - 1]) {
return -1;
}
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + ((right - left) >> 1);
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
left = mid + 1;
else if (nums[mid] > target)
right = mid - 1;
}
return -1;
}
}
Left close, right open[left,right)
class Solution {
public int search(int[] nums, int target) {
int left = 0, right = nums.length;
while (left < right) {
int mid = left + ((right - left) >> 1);
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
left = mid + 1;
else if (nums[mid] > target)
right = mid;
}
return -1;
}
}
704. Binary Search
Given an array of integers nums
which is sorted in ascending order, and an integer target
, write a function to search target
in nums
. If target
exists, then return its index. Otherwise, return -1
.
You must write an algorithm with O(log n)
runtime complexity.
Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
Example 2:
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1
Constraints:
1 <= nums.length <= 104
104 < nums[i], target < 104
- All the integers in
nums
are unique. nums
is sorted in ascending order.
My answers
class Solution {
public int search(int[] nums, int target) {
for(int i = 0; i < nums.length; i++) {
if(nums[i] == target){
return i;
}
}
return -1;
}
}
//[left,right)
class Solution {
public int search(int[] nums, int target) {
int left = 0, right = nums.length;
while(left < right) {
int mid = left + ((right - left) >> 1);
if(target == nums[mid]){
return mid;
} else if(nums[mid] < target) {
left = mid + 1;
} else if(nums[mid] > target) {
right = mid;
}
}
return -1;
}
}
//[left,right]
class Solution {
public int search(int[] nums, int target) {
int left = 0, right = nums.length - 1;
if (target < nums[0] || target > nums[right]) {
return -1;
}
while(left <= right) {
int mid = left + ((right-left)/2);
if(nums[mid] == target){
return mid;
} else if (nums[mid] > target) {
right = mid - 1;
} else if (nums[mid] < target) {
left = mid + 1;
}
}
return -1;
}
}
Double Pointers
Slow and Fast Pointer:
- Fast Pointer: to look for the new Array’s element, new array is the array that does not contain target value
- Slow Pointer: to index the updating index
27. Remove Element
class Solution {
public int removeElement(int[] nums, int val) {
int size = nums.length;
for(int i = 0; i < size; i++){
if (nums[i] == val){
// found the target value, move remaining element foward
for (int j = i+1; j < size; j++){
nums[j-1] = nums[j];
}
i--; //since moved forward, we need to check first element
size--;
}
}
return size;
}
}
nums = [0,1,2,2,3,0,4,2], val = 2
- When fastIndex goes to nums[4] which nums[fastIndex] == val, slowIndex is still at nums[1]
- Therefore, nums[2] == nums[4]
class Solution {
public int removeElement(int[] nums, int val) {
int slowIndex = 0;
for (int fastIndex = 0; fastIndex < nums.length; fastIndex++) {
if(nums[fastIndex] != val) {
nums[slowIndex] = nums[fastIndex];
slowIndex++;
}
}
return slowIndex;
}
}