GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Initial check in · lelea2/Algorithm@542cc01

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • java/coding
    • 3Sum.java
    • kthElementOf2SortedArray.java
    • validParenthenses.java
  • util/func
    • pow.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎java/coding/3Sum.java‎

Copy file name to clipboardExpand all lines: java/coding/3Sum.java
+5-7Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
55
if (num.length < 3) { //If given array is less than 3 number, just returned right away
66
return result;
77
}
8-
// sort array
8+
// sort array (assume that array being sorted)
99
Arrays.sort(num);
1010
for (int i = 0; i < num.length - 2; i++) {
1111
// //avoid duplicate solutions
1212
if (i == 0 || num[i] > num[i - 1]) {
1313

1414
int negate = -num[i];
15-
1615
int start = i + 1;
1716
int end = num.length - 1;
18-
1917
while (start < end) {
2018
//case 1
2119
if (num[start] + num[end] == negate) {
@@ -28,11 +26,12 @@ public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
2826
start++;
2927
end--;
3028
//avoid duplicate solutions
31-
while (start < end && num[end] == num[end + 1])
29+
while (start < end && num[end] == num[end + 1]) {
3230
end--;
33-
34-
while (start < end && num[start] == num[start - 1])
31+
}
32+
while (start < end && num[start] == num[start - 1]) {
3533
start++;
34+
}
3635
//case 2
3736
} else if (num[start] + num[end] < negate) {
3837
start++;
@@ -41,7 +40,6 @@ public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
4140
end--;
4241
}
4342
}
44-
4543
}
4644
}
4745

Collapse file

‎java/coding/kthElementOf2SortedArray.java‎

Copy file name to clipboardExpand all lines: java/coding/kthElementOf2SortedArray.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ public static int findKth(int A[], int B[], int k, int aStart, int aEnd, int bSt
4646
aMid = aMid + aStart;
4747
bMid = bMid + bStart;
4848

49-
if (A[aMid] > B[bMid]) {
49+
if (A[aMid] > B[bMid]) { //aEnd, bStart
5050
k = k - (bMid - bStart + 1);
5151
aEnd = aMid;
5252
bStart = bMid + 1;
53-
} else {
53+
} else { //(A[mid] <= B[mid]) => bEnd, aStart
5454
k = k - (aMid - aStart + 1);
5555
bEnd = bMid;
5656
aStart = aMid + 1;
Collapse file

‎java/coding/validParenthenses.java‎

Copy file name to clipboard
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
3+
*The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
4+
*/
5+
6+
//Idea: this typical problem could be solve with stacking logic
7+
//Create a hash map contains corresponding character
8+
//if value contains key, do array push
9+
//if value contains hashmap value, do array pop
10+
//at the end, if array/stack is empty, meant your parenthensis is balanced.
11+
//
12+
//
13+
public static boolean isValid(String s) {
14+
HashMap<Character, Character> map = new HashMap<Character, Character>();
15+
map.put('(', ')');
16+
map.put('[', ']');
17+
map.put('{', '}');
18+
19+
Stack<Character> stack = new Stack<Character>();
20+
21+
for (int i = 0; i < s.length(); i++) {
22+
char curr = s.charAt(i);
23+
24+
if (map.keySet().contains(curr)) {
25+
stack.push(curr);
26+
} else if (map.values().contains(curr)) {
27+
if (!stack.empty() && map.get(stack.peek()) == curr) {
28+
stack.pop();
29+
} else {
30+
return false;
31+
}
32+
}
33+
}
34+
35+
return stack.empty();
36+
}
Collapse file

‎util/func/pow.js‎

Copy file name to clipboard
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
//Pow function in javascript
3+
//
4+
function pow(a, b) {
5+
if (b === 0) {
6+
return 0;
7+
}
8+
if (b === 1) {
9+
return 1;
10+
}
11+
var c = pow(a/2);
12+
if (b % 2 === 0) {
13+
return c * c;
14+
} else {
15+
return c * c * a;
16+
}
17+
}

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.