Skip to content

Commit 0cf2037

Browse files
committed
Add solutions
1 parent e1cb0a8 commit 0cf2037

File tree

15 files changed

+312
-24
lines changed

15 files changed

+312
-24
lines changed

src/com/andrewbayd/deleteNodeInLinkedList/Solution.java renamed to src/com/andrewbayd/DeleteNodeInLinkedList.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.andrewbayd.deleteNodeInLinkedList;
1+
package com.andrewbayd;
22

33
/*
44
Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly.
@@ -7,7 +7,9 @@
77
https://leetcode.com/problems/delete-node-in-a-linked-list/
88
*/
99

10-
public class Solution {
10+
import com.andrewbayd.datastructures.ListNode;
11+
12+
public class DeleteNodeInLinkedList {
1113
public void deleteNode(ListNode node) {
1214
node.val = node.next.val;
1315
node.next = node.next.next;

src/com/andrewbayd/mergeTwoSortedLists/MergeTwoSortedLists.java renamed to src/com/andrewbayd/MergeTwoSortedLists.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package com.andrewbayd.mergeTwoSortedLists;
1+
package com.andrewbayd;
2+
3+
import com.andrewbayd.datastructures.ListNode;
24

35
public class MergeTwoSortedLists {
46
public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.andrewbayd;
2+
3+
import com.andrewbayd.datastructures.ListNode;
4+
5+
/*
6+
Given the head of a singly linked list, return true if it is a palindrome.
7+
8+
https://leetcode.com/problems/palindrome-linked-list/
9+
*/
10+
11+
public class PalindromeLinkedList {
12+
public boolean isPalindrome(ListNode head) {
13+
if (head == null || head.next == null) {
14+
return true;
15+
}
16+
// Find a middle point of a list using two pointers
17+
ListNode fast = head;
18+
ListNode slow = head;
19+
while (fast != null && fast.next != null) {
20+
fast = fast.next.next;
21+
slow = slow.next;
22+
}
23+
// Reverse first half of a list
24+
ListNode rev = head;
25+
ListNode curr = head.next;
26+
rev.next = null;
27+
while (curr != slow) {
28+
ListNode next = curr.next;
29+
curr.next = rev;
30+
rev = curr;
31+
curr = next;
32+
}
33+
// If list has an odd number of nodes move slow pointer one node forward
34+
if (fast != null) {
35+
slow = slow.next;
36+
}
37+
// Compare lists
38+
while (slow != null) {
39+
if (slow.val != rev.val) {
40+
return false;
41+
}
42+
slow = slow.next;
43+
rev = rev.next;
44+
}
45+
46+
return true;
47+
}
48+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.andrewbayd;
2+
3+
/*
4+
Given the head of a singly linked list, reverse the list, and return the reversed list.
5+
6+
https://leetcode.com/problems/reverse-linked-list/
7+
*/
8+
9+
import com.andrewbayd.datastructures.ListNode;
10+
11+
public class ReverseLinkedList {
12+
public ListNode reverseList(ListNode head) {
13+
if (head == null || head.next == null) {
14+
return head;
15+
}
16+
17+
ListNode prev = head;
18+
ListNode curr = head.next;
19+
prev.next = null;
20+
21+
while (curr != null) {
22+
ListNode next = curr.next;
23+
curr.next = prev;
24+
prev = curr;
25+
curr = next;
26+
}
27+
28+
return prev;
29+
}
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.andrewbayd;
2+
3+
/*
4+
Given a sorted array of distinct integers and a target value, return the index if the target is found.
5+
If not, return the index where it would be if it were inserted in order.
6+
7+
https://leetcode.com/problems/search-insert-position/
8+
*/
9+
10+
public class SearchInsertPosition {
11+
public static int searchInsert(int[] nums, int target) {
12+
int first = 0;
13+
int last = nums.length - 1;
14+
int mid = -1;
15+
while (first <= last) {
16+
mid = first + (last - first) / 2;
17+
if (nums[mid] == target) return mid;
18+
else if (nums[mid] > target) last = mid - 1;
19+
else first = mid + 1;
20+
}
21+
if (nums[mid] < target) return mid + 1;
22+
else return mid;
23+
}
24+
25+
public static void main(String[] args) {
26+
System.out.println(searchInsert(new int[]{1,3}, 2)); //-> 1
27+
System.out.println(searchInsert(new int[]{1,3,5,6}, 5)); //-> 2
28+
System.out.println(searchInsert(new int[]{1,3,5,6}, 2)); //-> 1
29+
System.out.println(searchInsert(new int[]{1,3,5,6}, 7)); //-> 4
30+
}
31+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.andrewbayd;
2+
3+
/*
4+
Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.
5+
6+
https://leetcode.com/problems/search-in-rotated-sorted-array/
7+
*/
8+
9+
public class SearchRotatedSortedArray {
10+
public static int search(int[] nums, int target) {
11+
int first = 0;
12+
int last = nums.length - 1;
13+
14+
while (first <= last) {
15+
int mid = first + (last - first) / 2;
16+
if (nums[mid] == target) {
17+
return mid;
18+
}
19+
if (nums[first] <= nums[mid]) {
20+
if (target >= nums[first] && target < nums[mid]) {
21+
last = mid - 1;
22+
} else {
23+
first = mid + 1;
24+
}
25+
} else {
26+
if (target > nums[mid] && target <= nums[last]) {
27+
first = mid + 1;
28+
} else {
29+
last = mid - 1;
30+
}
31+
}
32+
}
33+
34+
return -1;
35+
}
36+
37+
public static void main(String[] args) {
38+
System.out.println(search(new int[]{4, 5, 6, 7, 0, 1, 2}, 0)); //-> 4
39+
System.out.println(search(new int[]{4, 5, 6, 7, 0, 1, 2}, 3)); //-> -1
40+
}
41+
}

src/com/andrewbayd/TwoSumII.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.andrewbayd;
2+
3+
/*
4+
Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number.
5+
Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length.
6+
Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.
7+
8+
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
9+
*/
10+
11+
import java.util.Arrays;
12+
13+
public class TwoSumII {
14+
public static int[] twoSum(int[] numbers, int target) {
15+
int first = 0;
16+
int last = numbers.length - 1;
17+
while (first < last) {
18+
if (numbers[first] + numbers[last] == target) return new int[]{first + 1, last + 1};
19+
else if (numbers[first] + numbers[last] < target) first++;
20+
else last--;
21+
}
22+
return new int[]{};
23+
}
24+
25+
public static void main(String[] args) {
26+
int[] result1 = twoSum(new int[]{2, 7, 11, 15}, 9);
27+
int[] result2 = twoSum(new int[]{1, 2, 5, 6, 7, 9}, 9);
28+
System.out.println(Arrays.toString(result1)); //-> [1, 2]
29+
System.out.println(Arrays.toString(result2)); //-> [2, 5]
30+
}
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.andrewbayd;
2+
3+
/*
4+
Given a positive integer num, write a function which returns True if num is a perfect square else False.
5+
6+
https://leetcode.com/problems/valid-perfect-square/
7+
*/
8+
9+
public class ValidPerfectSquare {
10+
public static boolean isPerfectSquare(int num) {
11+
long first = 1;
12+
long last = num;
13+
while (first <= last) {
14+
long mid = first + (last - first) / 2;
15+
if (mid * mid == (long)num) return true;
16+
if (mid * mid > (long)num ) last = mid - 1;
17+
else first = mid + 1;
18+
}
19+
return false;
20+
}
21+
22+
public static void main(String[] args) {
23+
System.out.println(isPerfectSquare(16)); //-> true
24+
System.out.println(isPerfectSquare(14)); //-> false
25+
}
26+
}

src/com/andrewbayd/BinarySearchTreeLCA/Solution.java renamed to src/com/andrewbayd/binarySearchTreeLCA/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.andrewbayd.BinarySearchTreeLCA;
1+
package com.andrewbayd.binarySearchTreeLCA;
22

33
/*
44
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.

src/com/andrewbayd/BinarySearchTreeLCA/TreeNode.java renamed to src/com/andrewbayd/binarySearchTreeLCA/TreeNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.andrewbayd.BinarySearchTreeLCA;
1+
package com.andrewbayd.binarySearchTreeLCA;
22

33
public class TreeNode {
44
int val;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.andrewbayd.datastructures;
2+
3+
public class ListNode {
4+
public int val;
5+
public ListNode next;
6+
7+
public ListNode() {
8+
}
9+
10+
public ListNode(int val) {
11+
this.val = val;
12+
}
13+
14+
public ListNode(int val, ListNode next) {
15+
this.val = val;
16+
this.next = next;
17+
}
18+
}

src/com/andrewbayd/deleteNodeInLinkedList/ListNode.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/com/andrewbayd/mergeTwoSortedLists/ListNode.java

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.andrewbayd.symmetricTree;
2+
3+
/*
4+
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
5+
6+
https://leetcode.com/problems/symmetric-tree/
7+
*/
8+
9+
public class Solution {
10+
public boolean isSymmetric(TreeNode root) {
11+
return isSymetricRecursive(root.left, root.right);
12+
}
13+
14+
private boolean isSymetricRecursive(TreeNode first, TreeNode second) {
15+
if (first == null && second == null) {
16+
return true;
17+
}
18+
if (first == null || second == null || first.val != second.val) {
19+
return false;
20+
}
21+
return isSymetricRecursive(first.left, second.right) && isSymetricRecursive(first.right, second.left);
22+
}
23+
24+
/*
25+
Iterative solution
26+
27+
public boolean isSymmetric(TreeNode root) {
28+
if (root == null) {
29+
return true;
30+
}
31+
32+
Queue<TreeNode> queue = new LinkedList<>();
33+
queue.add(root.left);
34+
queue.add(root.right);
35+
36+
while (!queue.isEmpty()) {
37+
TreeNode first = queue.remove();
38+
TreeNode second = queue.remove();
39+
40+
if (first == null && second == null) {
41+
continue;
42+
}
43+
44+
if ((first == null && second != null) || (first != null && second == null) || first.val != second.val) {
45+
return false;
46+
}
47+
48+
queue.add(first.left);
49+
queue.add(second.right);
50+
queue.add(first.right);
51+
queue.add(second.left);
52+
}
53+
54+
return true;
55+
}
56+
*/
57+
58+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.andrewbayd.symmetricTree;
2+
3+
public class TreeNode {
4+
int val;
5+
TreeNode left;
6+
TreeNode right;
7+
8+
TreeNode() {
9+
}
10+
11+
TreeNode(int val) {
12+
this.val = val;
13+
}
14+
15+
TreeNode(int val, TreeNode left, TreeNode right) {
16+
this.val = val;
17+
this.left = left;
18+
this.right = right;
19+
}
20+
}

0 commit comments

Comments
 (0)