From e6f602930378641378e15073a6a52f7a0c17726c Mon Sep 17 00:00:00 2001 From: Sonjyoti Rabha Date: Sun, 21 May 2023 23:46:54 +0530 Subject: [PATCH 1/4] Added Searching in a Sorted Rotated Array --- .../SearchSortedRotatedArray.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 leetcode/binary-search/SearchSortedRotatedArray.java diff --git a/leetcode/binary-search/SearchSortedRotatedArray.java b/leetcode/binary-search/SearchSortedRotatedArray.java new file mode 100644 index 00000000..28248ad9 --- /dev/null +++ b/leetcode/binary-search/SearchSortedRotatedArray.java @@ -0,0 +1,52 @@ +// Given an array after the possible rotation and an integer target, return the index of target if it is in the array, or -1 if it is not in the array. + +// eg Input: arr = [4,5,6,7,0,1,2], target = 0, Output: 4 + +class SearchRotatedSortedArray { + public int search(int[] arr, int target) { + int pivot = findPivot(arr); + if (pivot == -1) { + return binarySearch(arr, target, 0, arr.length - 1); + } + if (arr[pivot] == target) { + return pivot; + } else if (target >= arr[0]) { + return binarySearch(arr, target, 0, pivot - 1); + } + return binarySearch(arr, target, pivot + 1, arr.length - 1); + } + + int binarySearch(int[] arr, int target, int start, int end) { + while (start <= end) { + int mid = start + (end - start) / 2; + if (arr[mid] == target) { + return mid; + } else if (arr[mid] > target) { + end = mid - 1; + } else { + start = mid + 1; + } + } + return -1; + } + + int findPivot(int[] nums) { + int start = 0; + int end = nums.length - 1; + while (start <= end) { + int mid = start + (end - start) / 2; + if (mid < end && nums[mid] > nums[mid + 1]) { + return mid; + } + if (mid > start && nums[mid] < nums[mid - 1]) { + return mid - 1; + } + if (nums[mid] < nums[start]) { + end = mid - 1; + } else { + start = mid + 1; + } + } + return -1; + } +} \ No newline at end of file From 9123d5591376535f8f4cdb4f17010b49d66b5662 Mon Sep 17 00:00:00 2001 From: Sonjyoti Rabha Date: Wed, 24 May 2023 00:20:24 +0530 Subject: [PATCH 2/4] Added Remove Duplicates from a Sorted List --- .../SearchSortedRotatedArray.java | 2 + .../RemoveDuplicatesFromSortedList.java | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 leetcode/linked-list/RemoveDuplicatesFromSortedList.java diff --git a/leetcode/binary-search/SearchSortedRotatedArray.java b/leetcode/binary-search/SearchSortedRotatedArray.java index 28248ad9..5d3266c6 100644 --- a/leetcode/binary-search/SearchSortedRotatedArray.java +++ b/leetcode/binary-search/SearchSortedRotatedArray.java @@ -2,6 +2,8 @@ // eg Input: arr = [4,5,6,7,0,1,2], target = 0, Output: 4 + + class SearchRotatedSortedArray { public int search(int[] arr, int target) { int pivot = findPivot(arr); diff --git a/leetcode/linked-list/RemoveDuplicatesFromSortedList.java b/leetcode/linked-list/RemoveDuplicatesFromSortedList.java new file mode 100644 index 00000000..8dda7b4c --- /dev/null +++ b/leetcode/linked-list/RemoveDuplicatesFromSortedList.java @@ -0,0 +1,37 @@ +/** + * Leetcode 83. Remove duplicates from a sorted List + * Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. + * example: Input: head = [1,1,2] Output: [1,2] + */ + + + + +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ + +class Solution{ + public ListNode deleteDuplicates(ListNode head){ + if(head == null){ + return head; + } + ListNode temp = head; + while(temp.next != null){ + if(temp.val == temp.next.val){ + temp.next = temp.next.next; + } + else{ + temp = temp.next; + } + } + return head; + } +} \ No newline at end of file From 797871e99e1654569d6b789557b05572e349d5ba Mon Sep 17 00:00:00 2001 From: Sonjyoti Rabha <85861164+sonjyoti@users.noreply.github.com> Date: Wed, 24 May 2023 07:19:19 +0530 Subject: [PATCH 3/4] Update RemoveDuplicatesFromSortedList.java --- .../RemoveDuplicatesFromSortedList.java | 37 ------------------- 1 file changed, 37 deletions(-) diff --git a/leetcode/linked-list/RemoveDuplicatesFromSortedList.java b/leetcode/linked-list/RemoveDuplicatesFromSortedList.java index 8dda7b4c..e69de29b 100644 --- a/leetcode/linked-list/RemoveDuplicatesFromSortedList.java +++ b/leetcode/linked-list/RemoveDuplicatesFromSortedList.java @@ -1,37 +0,0 @@ -/** - * Leetcode 83. Remove duplicates from a sorted List - * Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. - * example: Input: head = [1,1,2] Output: [1,2] - */ - - - - -/** - * Definition for singly-linked list. - * public class ListNode { - * int val; - * ListNode next; - * ListNode() {} - * ListNode(int val) { this.val = val; } - * ListNode(int val, ListNode next) { this.val = val; this.next = next; } - * } - */ - -class Solution{ - public ListNode deleteDuplicates(ListNode head){ - if(head == null){ - return head; - } - ListNode temp = head; - while(temp.next != null){ - if(temp.val == temp.next.val){ - temp.next = temp.next.next; - } - else{ - temp = temp.next; - } - } - return head; - } -} \ No newline at end of file From 8e699652d0dc27822072622b86c8cf6624c90da3 Mon Sep 17 00:00:00 2001 From: Sonjyoti Rabha <85861164+sonjyoti@users.noreply.github.com> Date: Wed, 24 May 2023 07:24:57 +0530 Subject: [PATCH 4/4] Update RemoveDuplicatesFromSortedList.java --- .../RemoveDuplicatesFromSortedList.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/leetcode/linked-list/RemoveDuplicatesFromSortedList.java b/leetcode/linked-list/RemoveDuplicatesFromSortedList.java index e69de29b..e0affa43 100644 --- a/leetcode/linked-list/RemoveDuplicatesFromSortedList.java +++ b/leetcode/linked-list/RemoveDuplicatesFromSortedList.java @@ -0,0 +1,37 @@ +/** + * Leetcode 83. Remove duplicates from a sorted List + * Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. + * example: Input: head = [1,1,2] Output: [1,2] + */ + + + + + /** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ + + class Solution{ + public ListNode deleteDuplicates(ListNode head){ + if(head == null){ + return head; + } + ListNode temp = head; + while(temp.next != null){ + if(temp.val == temp.next.val){ + temp.next = temp.next.next; + } + else{ + temp = temp.next; + } + } + return head; + } + } \ No newline at end of file