Skip to content

Commit 9511120

Browse files
committed
Re-practice the basic algos
1 parent 1b4cedd commit 9511120

8 files changed

+403
-1
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package problems.leetcode;
2+
3+
public class ArithmeticEval {
4+
5+
// Given a string of an arithmetic expression, composed of numbers,
6+
// '+' and '*', calculate the result -
7+
//
8+
// 2 + 4 * 5 * 7
9+
//
10+
// 1 + 2 + 5
11+
//
12+
// 2 + 4 * 5 + 7
13+
14+
static int eval(String exp) {
15+
int result = 0, current = 1;
16+
for (String token : exp.split(" ")) {
17+
if (token.isBlank()) {
18+
continue;
19+
} else if (token.equals("+")) {
20+
result += current;
21+
current = 1;
22+
} else if (!token.equals("*")) {
23+
current *= Integer.parseInt(token);
24+
}
25+
}
26+
27+
return result + current;
28+
}
29+
30+
public static void main(String[] args) {
31+
String exp1 = "2 + 4 * 5 + 7";
32+
String exp2 = "1 + 2 + 3";
33+
String exp3 = "1 + 2";
34+
String exp4 = "2 * 4";
35+
String exp5 = "24";
36+
System.out.println(eval(exp1));
37+
System.out.println(eval(exp2));
38+
System.out.println(eval(exp3));
39+
System.out.println(eval(exp4));
40+
System.out.println(eval(exp5));
41+
}
42+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package problems.leetcode;
2+
3+
public class BinarySearch {
4+
5+
private static int binarySearch(int[] vals, int val) {
6+
int l = 0, h = vals.length - 1;
7+
8+
while (l <= h) {
9+
int m = (l + h) / 2;
10+
if (vals[m] == val) {
11+
return m;
12+
} else if (vals[m] < val) {
13+
l = m + 1;
14+
} else {
15+
h = m - 1;
16+
}
17+
}
18+
19+
return -1;
20+
}
21+
22+
public static void main(String[] args) {
23+
System.out.println(binarySearch(new int[] { 1, 2, 3, 4, 5 }, 3));
24+
System.out.println(binarySearch(new int[] { 1, 2, 3, 4, 5 }, 5));
25+
System.out.println(binarySearch(new int[] { 1, 2, 3, 4, 5 }, 1));
26+
System.out.println(binarySearch(new int[] { 1, 2, 3, 4, 5 }, 6));
27+
System.out.println(binarySearch(new int[] { 1, 2, 3, 4, 5 }, -5));
28+
}
29+
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package problems.leetcode;
2+
3+
import java.util.Arrays;
4+
5+
public class InsertionSort {
6+
7+
// runtime: O(N^2)
8+
// space: O(1)
9+
public static void sort(int[] nums) {
10+
if (nums == null || nums.length < 1) {
11+
return;
12+
}
13+
14+
for (int i = 1; i < nums.length; i++) {
15+
for (int j = i; j > 0 && nums[j] < nums[j - 1]; j--) {
16+
int num = nums[j];
17+
nums[j] = nums[j - 1];
18+
nums[j - 1] = num;
19+
}
20+
}
21+
22+
System.out.println(Arrays.toString(nums));
23+
}
24+
25+
public static void main(String[] args) {
26+
sort(new int[] { 1, 2, 3, 4, 5 });
27+
sort(new int[] { 5, 4, 3, 2, 1 });
28+
sort(new int[] { 1, 2, 3, 4, 5, 4, 3, 2, 1 });
29+
}
30+
31+
}

src/problems/leetcode/MergeSort.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package problems.leetcode;
2+
3+
import java.util.Arrays;
4+
5+
public class MergeSort {
6+
7+
private static void merge(int[] nums, int lo, int mid, int hi, int[] aux) {
8+
// Merge nums[lo..mid] with nums[mid+1..hi].
9+
int i = lo, j = mid + 1;
10+
// Copy nums[lo..hi] to nums[lo..hi].
11+
for (int k = lo; k <= hi; k++) {
12+
aux[k] = nums[k];
13+
}
14+
15+
// Merge back to a[lo..hi].
16+
for (int k = lo; k <= hi; k++) {
17+
if (i > mid) {
18+
nums[k] = aux[j++];
19+
} else if (j > hi) {
20+
nums[k] = aux[i++];
21+
} else if (aux[j] < aux[i]) {
22+
nums[k] = aux[j++];
23+
} else {
24+
nums[k] = aux[i++];
25+
}
26+
}
27+
}
28+
29+
private static void sort(int[] nums, int lo, int hi, int[] aux) {
30+
// sort nums[lo..hi]
31+
32+
if (hi <= lo) {
33+
return;
34+
}
35+
36+
int mid = (lo + hi) / 2;
37+
sort(nums, lo, mid, aux);
38+
sort(nums, mid + 1, hi, aux);
39+
merge(nums, lo, mid, hi, aux);
40+
}
41+
42+
// runtime: O(NlgN)
43+
// space: O(N)
44+
public static void sort(int[] nums) {
45+
if (nums == null || nums.length < 1) {
46+
return;
47+
}
48+
49+
sort(nums, 0, nums.length - 1, new int[nums.length]);
50+
System.out.println(Arrays.toString(nums));
51+
}
52+
53+
public static void main(String[] args) {
54+
sort(new int[] { 1, 2, 3, 4, 5 });
55+
sort(new int[] { 5, 4, 3, 2, 1 });
56+
sort(new int[] { 1, 2, 3, 4, 5, 4, 3, 2, 1 });
57+
}
58+
59+
}

src/problems/leetcode/ReverseLinkedList.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,25 @@ private static class ListNode {
1212
ListNode(int x) {
1313
val = x;
1414
}
15+
16+
@Override
17+
public String toString() {
18+
return next != null ? (val + " -> " + next.toString()) : String.valueOf(val);
19+
}
1520
}
1621

17-
public ListNode reverseList(ListNode head) {
22+
// 1 - > 2 -> 3 -> 4
23+
24+
// head = 1
25+
// curr = head
26+
// prev = null
27+
28+
// next = curr.next
29+
// curr.next = prev
30+
// prev = curr
31+
// curr = next
32+
33+
public static ListNode reverseList(ListNode head) {
1834
ListNode prev = null;
1935
ListNode current = head;
2036
while (current != null) {
@@ -26,4 +42,13 @@ public ListNode reverseList(ListNode head) {
2642

2743
return prev;
2844
}
45+
46+
public static void main(String[] args) {
47+
ListNode head = new ListNode(1);
48+
head.next = new ListNode(2);
49+
head.next.next = new ListNode(3);
50+
System.out.println(head);
51+
head = reverseList(head);
52+
System.out.println(head);
53+
}
2954
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package problems.leetcode;
2+
3+
import java.util.Arrays;
4+
5+
public class SelectionSort {
6+
7+
// space complexity: O(1)
8+
// runtime complexity: O(N^2)
9+
public static void sort(int[] nums) {
10+
if (nums == null || nums.length < 1) {
11+
return;
12+
}
13+
14+
for (int i = 0; i < nums.length; i++) {
15+
int k = i;
16+
for (int j = i; j < nums.length; j++) {
17+
if (nums[k] > nums[j]) {
18+
k = j;
19+
}
20+
}
21+
int num = nums[i];
22+
nums[i] = nums[k];
23+
nums[k] = num;
24+
}
25+
26+
System.out.println(Arrays.toString(nums));
27+
}
28+
29+
public static void main(String[] args) {
30+
sort(new int[] { 1, 2, 3, 4, 5 });
31+
sort(new int[] { 5, 4, 3, 2, 1 });
32+
sort(new int[] { 1, 2, 3, 4, 5, 4, 3, 2, 1 });
33+
}
34+
35+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package problems.leetcode;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
import java.util.Stack;
6+
7+
public class TwoStackEval {
8+
9+
10+
private static void eval(Stack<String> ops, Stack<Double> vals) {
11+
if (vals.size() < 2 || ops.isEmpty()) {
12+
throw new IllegalArgumentException("invalid expression");
13+
}
14+
15+
double val2 = vals.pop();
16+
double val1 = vals.pop();
17+
String op = ops.pop();
18+
19+
double result;
20+
if (op.equals("+")) {
21+
result = val1 + val2;
22+
} else if (op.equals("-")) {
23+
result = val1 - val2;
24+
} else if (op.equals("*")) {
25+
result = val1 * val2;
26+
} else if (op.equals("/")) {
27+
result = val1 / val2;
28+
} else {
29+
throw new IllegalArgumentException("invalid op: " + op);
30+
}
31+
vals.push(result);
32+
}
33+
34+
public static double eval(String str) {
35+
36+
Stack<String> ops = new Stack<>();
37+
Stack<Double> vals = new Stack<>();
38+
39+
for (String token : str.split(" ")) {
40+
if (token.isBlank()) {
41+
continue;
42+
}
43+
44+
if (token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/")) {
45+
ops.push(token);
46+
} else if (token.equals("(")) {
47+
// ignore (
48+
continue;
49+
} else if (token.equals(")")) {
50+
eval(ops, vals);
51+
} else {
52+
vals.push(Double.parseDouble(token));
53+
}
54+
}
55+
56+
while (vals.size() > 1) {
57+
eval(ops, vals);
58+
}
59+
60+
return !vals.empty() ? vals.pop() : 0;
61+
}
62+
63+
public static void main(String[] args) {
64+
String exp1 = "1 + 2 + 3";
65+
String exp2 = "4 + ( 2 - 3 )";
66+
String exp3 = "( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )";
67+
System.out.println(eval(exp1));
68+
System.out.println(eval(exp2));
69+
System.out.println(eval(exp3));
70+
}
71+
72+
73+
}

0 commit comments

Comments
 (0)