Skip to content

Commit e1cb0a8

Browse files
committed
Add solutions
1 parent 31d1ec9 commit e1cb0a8

File tree

11 files changed

+380
-0
lines changed

11 files changed

+380
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.andrewbayd;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/*
8+
You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n,
9+
representing the number of elements in nums1 and nums2 respectively.
10+
Merge nums1 and nums2 into a single array sorted in non-decreasing order.
11+
12+
https://leetcode.com/problems/merge-sorted-array/
13+
*/
14+
15+
public class MergeSortedArrays {
16+
public static void merge(int[] nums1, int m, int[] nums2, int n) {
17+
if (nums2.length == 0) return;
18+
19+
List<Integer> temp = new ArrayList<>();
20+
21+
int first = 0;
22+
int second = 0;
23+
24+
for (int i = 0; i < n + m; i++) {
25+
if (first < m && (second == n || nums1[first] <= nums2[second])) {
26+
temp.add(nums1[first++]);
27+
} else {
28+
temp.add(nums2[second++]);
29+
}
30+
}
31+
32+
for (int i = 0; i < n + m; i++) {
33+
nums1[i] = temp.get(i);
34+
}
35+
}
36+
37+
public static void main(String[] args) {
38+
int[] nums1 = {1, 2, 3, 0, 0, 0};
39+
int[] nums2 = {2, 5, 6};
40+
merge(nums1, 3, nums2, 3);
41+
System.out.println(Arrays.toString(nums1)); //-> [1, 2, 2, 3, 5, 6]
42+
}
43+
}

src/com/andrewbayd/PlusOne.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.andrewbayd;
2+
3+
import java.util.Arrays;
4+
5+
/*
6+
You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer.
7+
Increment the large integer by one and return the resulting array of digits.
8+
9+
https://leetcode.com/problems/plus-one/
10+
*/
11+
12+
public class PlusOne {
13+
public static int[] plusOne(int[] digits) {
14+
if (digits[digits.length - 1] < 9) {
15+
digits[digits.length - 1]++;
16+
return digits;
17+
}
18+
19+
int add = 0;
20+
for (int i = digits.length - 1; i >= 0; i--) {
21+
if (i == digits.length - 1) {
22+
digits[i]++;
23+
}
24+
if (add == 1) {
25+
digits[i]++;
26+
add--;
27+
}
28+
if (digits[i] >= 10) {
29+
digits[i] = digits[i] % 10;
30+
add++;
31+
}
32+
}
33+
34+
if (add == 0) {
35+
return digits;
36+
}
37+
38+
int[] result = new int[digits.length + 1];
39+
result[0] = 1;
40+
for (int i = 1; i < result.length - 1; i++) {
41+
result[i] = digits[i - 1];
42+
}
43+
44+
return result;
45+
}
46+
47+
public static void main(String[] args) {
48+
System.out.println(Arrays.toString(plusOne(new int[]{1, 2, 4}))); //-> [1, 2, 5]
49+
System.out.println(Arrays.toString(plusOne(new int[]{2, 6, 9}))); //-> [2, 7, 0]
50+
System.out.println(Arrays.toString(plusOne(new int[]{9, 9}))); //-> [1, 0, 0]
51+
}
52+
}

src/com/andrewbayd/ReverseString.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.andrewbayd;
2+
3+
import java.util.Arrays;
4+
5+
/*
6+
Write a function that reverses a string. The input string is given as an array of characters s.
7+
You must do this by modifying the input array in-place with O(1) extra memory.
8+
9+
https://leetcode.com/problems/reverse-string/
10+
*/
11+
12+
public class ReverseString {
13+
public static void reverseString(char[] s) {
14+
for (int i = 0; i < s.length / 2; i++) {
15+
char temp = s[i];
16+
s[i] = s[s.length - 1 - i];
17+
s[s.length - 1 - i] = temp;
18+
}
19+
}
20+
21+
public static void main(String[] args) {
22+
char[] string = {'h', 'e', 'l', 'l', 'o'};
23+
reverseString(string);
24+
System.out.println(Arrays.toString(string)); //-> ["o","l","l","e","h"]
25+
}
26+
}

src/com/andrewbayd/RotateImage.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.andrewbayd;
2+
3+
/*
4+
You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).
5+
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly.
6+
DO NOT allocate another 2D matrix and do the rotation.
7+
8+
https://leetcode.com/problems/rotate-image/
9+
*/
10+
11+
import java.util.Arrays;
12+
13+
public class RotateImage {
14+
// rotate = transpose + reflect
15+
public static void rotate(int[][] matrix) {
16+
int n = matrix.length;
17+
// base case
18+
if (n <= 1) {
19+
return;
20+
}
21+
//transpose
22+
for (int i = 0; i < n; i++) {
23+
for (int j = i + 1; j < n; j++) {
24+
int temp = matrix[i][j];
25+
matrix[i][j] = matrix[j][i];
26+
matrix[j][i] = temp;
27+
}
28+
}
29+
//reflect
30+
for (int i = 0; i < n; i++) {
31+
for (int j = 0; j < n / 2; j++) {
32+
int temp = matrix[i][j];
33+
matrix[i][j] = matrix[i][n-1-j];
34+
matrix[i][n-1-j] = temp;
35+
}
36+
}
37+
}
38+
39+
public static void main(String[] args) {
40+
int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}};
41+
rotate(matrix);
42+
Arrays.stream(matrix).map(Arrays::toString).forEach(System.out::println);
43+
}
44+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.andrewbayd;
2+
3+
import java.util.Arrays;
4+
5+
/*
6+
Given an m x n integer matrix, if an element is 0, set its entire row and column to 0's.
7+
You must do it in place.
8+
9+
https://leetcode.com/problems/set-matrix-zeroes/
10+
*/
11+
12+
public class SetMatrixZeroes {
13+
public static void setZeroes(int[][] matrix) {
14+
int n = matrix.length;
15+
int m = matrix[0].length;
16+
boolean isFirstRow = false;
17+
boolean isFirstColumn = false;
18+
19+
for (int i = 0; i < n; i++) {
20+
for (int j = 0; j < m; j++) {
21+
if (matrix[i][j] == 0) {
22+
if (i == 0 && !isFirstRow) {
23+
isFirstRow = true;
24+
}
25+
if (j == 0 && !isFirstColumn) {
26+
isFirstColumn = true;
27+
}
28+
matrix[0][j] = 0;
29+
matrix[i][0] = 0;
30+
}
31+
}
32+
}
33+
34+
for (int i = 1; i < n; i++) {
35+
for (int j = 1; j < m; j++) {
36+
if (matrix[0][j] == 0 || matrix[i][0] == 0) {
37+
matrix[i][j] = 0;
38+
}
39+
}
40+
}
41+
42+
if (isFirstRow) {
43+
for (int j = 0; j < m; j++) {
44+
matrix[0][j] = 0;
45+
}
46+
}
47+
48+
if (isFirstColumn) {
49+
for (int i = 0; i < n; i++) {
50+
matrix[i][0] = 0;
51+
}
52+
}
53+
}
54+
55+
public static void main(String[] args) {
56+
int[][] matrix = {{1,2,3,4},{5,0,7,8}, {0,10,11,12},{13,14,15,0}};
57+
setZeroes(matrix);
58+
Arrays.stream(matrix).map(Arrays::toString).forEach(System.out::println);
59+
}
60+
}

src/com/andrewbayd/SpiralMatrix.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.andrewbayd;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/*
7+
Given an m x n matrix, return all elements of the matrix in spiral order.
8+
9+
https://leetcode.com/problems/spiral-matrix/
10+
*/
11+
12+
public class SpiralMatrix {
13+
public static List<Integer> spiralOrder(int[][] matrix) {
14+
List<Integer> elements = new ArrayList<>();
15+
16+
int topRow = 0;
17+
int bottomRow = matrix.length - 1;
18+
int leftColumn = 0;
19+
int rightColumn = matrix[0].length - 1;
20+
21+
while (topRow <= bottomRow && leftColumn <= rightColumn) {
22+
for (int i = leftColumn; i <= rightColumn; i++) {
23+
elements.add(matrix[topRow][i]);
24+
}
25+
topRow++;
26+
27+
for (int i = topRow; i <= bottomRow; i++) {
28+
elements.add(matrix[i][rightColumn]);
29+
}
30+
rightColumn--;
31+
32+
if (topRow <= bottomRow) {
33+
for (int i = rightColumn; i >= leftColumn; i--) {
34+
elements.add(matrix[bottomRow][i]);
35+
}
36+
}
37+
bottomRow--;
38+
39+
if (leftColumn <= rightColumn) {
40+
for (int i = bottomRow; i >= topRow; i--) {
41+
elements.add(matrix[i][leftColumn]);
42+
}
43+
}
44+
leftColumn++;
45+
}
46+
47+
return elements;
48+
}
49+
50+
public static void main(String[] args) {
51+
int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
52+
System.out.println(spiralOrder(matrix)); //-> [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]
53+
}
54+
}

src/com/andrewbayd/StrStr.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.andrewbayd;
2+
3+
/*
4+
Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
5+
6+
https://leetcode.com/problems/implement-strstr/
7+
*/
8+
9+
public class StrStr {
10+
public int strStr(String haystack, String needle) {
11+
if (needle.length() == 0) return 0;
12+
if (needle.length() > haystack.length()) return -1;
13+
14+
for (int i = 0; i < haystack.length(); i++) {
15+
if (i + needle.length() <= haystack.length() && haystack.startsWith(needle, i)) return i;
16+
}
17+
18+
return -1;
19+
}
20+
21+
public static void main(String[] args) {
22+
StrStr str = new StrStr();
23+
System.out.println(str.strStr("hello", "ll")); //->2
24+
System.out.println(str.strStr("a", "a")); //->0
25+
System.out.println(str.strStr("mississippi", "issip")); //->4
26+
System.out.println(str.strStr("aa", "aaa")); //->-1
27+
System.out.println(str.strStr("aaa", "")); //->0
28+
}
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.andrewbayd.binaryTreeInOrderTraversal;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/*
7+
Given the root of a binary tree, return the inorder traversal of its nodes' values.
8+
9+
https://leetcode.com/problems/binary-tree-inorder-traversal/
10+
*/
11+
12+
public class Solution {
13+
public List<Integer> inorderTraversal(TreeNode root) {
14+
List<Integer> result = new ArrayList<>();
15+
traversalRecursive(root, result);
16+
return result;
17+
}
18+
19+
private void traversalRecursive(TreeNode root, List<Integer> result) {
20+
if (root == null) {
21+
return;
22+
}
23+
traversalRecursive(root.left, result);
24+
result.add(root.val);
25+
traversalRecursive(root.right, result);
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.andrewbayd.binaryTreeInOrderTraversal;
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+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.andrewbayd.deleteNodeInLinkedList;
2+
3+
public class ListNode {
4+
int val;
5+
ListNode next;
6+
7+
ListNode(int x) {
8+
val = x;
9+
}
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.andrewbayd.deleteNodeInLinkedList;
2+
3+
/*
4+
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.
5+
It is guaranteed that the node to be deleted is not a tail node in the list.
6+
7+
https://leetcode.com/problems/delete-node-in-a-linked-list/
8+
*/
9+
10+
public class Solution {
11+
public void deleteNode(ListNode node) {
12+
node.val = node.next.val;
13+
node.next = node.next.next;
14+
}
15+
}

0 commit comments

Comments
 (0)