Skip to content

Commit

Permalink
Reorganize folder structure
Browse files Browse the repository at this point in the history
Files are now organized into folders named for their respective
approach.

Any files that were created for studying will not have a number prefix
as they did not originate from leetcode/CTCI.
  • Loading branch information
Sean Prashad authored and Sean Prashad committed Jul 23, 2019
1 parent 27bc0c2 commit fbbe6c6
Show file tree
Hide file tree
Showing 92 changed files with 213 additions and 3,034 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
*.class
.vscode/
14 changes: 0 additions & 14 deletions .vscode/launch.json

This file was deleted.

Empty file.
10 changes: 0 additions & 10 deletions Pascale_Triangle_118.java → Arrays/118_Pascale_Triangle.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,4 @@ public static List<List<Integer>> generate(int numRows) {

return result;
}

public static void main(String[] args) {
int n = 5;
List<List<Integer>> result = generate(n);

for (List<Integer> l : result) {
System.out.println(l);
}
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,4 @@ public static List<Integer> getRow(int rowIndex) {

return row;
}

public static void main(String[] args) {
int n = 3;
List<Integer> result = getRow(n);

for (Integer l : result) {
System.out.print(l + ", ");
}
return;
}
}
19 changes: 19 additions & 0 deletions Arrays/169_Majority_Element.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.HashMap;
import java.util.Map;

public class Majority_Element_169 {
public static int majorityElement(int[] nums) {
Map<Integer, Integer> hm = new HashMap<>();
int n = nums.length;
int result = 0;

for (int i = 0; i < n; i++) {
hm.put(nums[i], hm.getOrDefault(nums[i], 0) + 1);
if (hm.get(nums[i]) > n / 2) {
result = nums[i];
}
}

return result;
}
}
19 changes: 3 additions & 16 deletions TwoSum.java → Arrays/1_TwoSum.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import java.util.HashMap;

public class TwoSum {
public class TwoSum_1 {
public static int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> hm = new HashMap<>();
int[] indices = new int[2];
Expand All @@ -10,25 +10,12 @@ public static int[] twoSum(int[] nums, int target) {
if (hm.containsKey(remainder)) {
indices[0] = hm.get(remainder);
indices[1] = i;
break;
return indices;
}

hm.put(nums[i], i);
}

return indices;
}

public static void main(String[] args) {
int[] nums = new int[] { -1, -2, 2, 6, 9, -10, -3 };
int target = 0;

int[] result = twoSum(nums, target);

System.out.println("The indicies of the two values that add up to " + target + " are: ");
for (Integer i : result) {
System.out.println(i);
}
return;
return new int[] {};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,11 @@ public static int[] productExceptSelf(int[] nums) {

productSoFar = 1;

int[] result2 = new int[nums.length];
for (int i = nums.length - 1; i >= 0; i--) {
result2[i] = productSoFar;
result[i] *= productSoFar;
productSoFar *= nums[i];
}

return result;
}

public static void main(String[] args) {
// int[] nums = new int[] { 3, 1, 2, 5, 6, 4 };
int[] nums = new int[] { 1, 2, 3, 4 };

int[] result = productExceptSelf(nums);

for (int i = 0; i < result.length; i++) {
System.out.print(result[i]);
if (i != result.length - 1) {
System.out.print(", ");
}
}

return;
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,10 @@ public static int[] intersection(int[] nums1, int[] nums2) {

int[] result = new int[intersection.size()];
int idx = 0;
for (int i : intersection) {
result[idx++] = i;
for (int commonVal : intersection) {
result[idx++] = commonVal;
}

return result;
}

public static void main(String[] args) {
int[] arr1 = new int[] { 1, 2, 2, 1 };
int[] arr2 = new int[] { 2, 2 };

int[] result1 = intersection(arr1, arr2);
for (int i : result1) {
System.out.print(i + ", ");
}

System.out.println();

int[] arr3 = new int[] { 4, 9, 5 };
int[] arr4 = new int[] { 9, 4, 9, 8, 4 };

int[] result2 = intersection(arr3, arr4);
for (int i : result2) {
System.out.print(i + ", ");
}

return;
}
}
31 changes: 31 additions & 0 deletions Arrays/350_Intersection_of_Two_Arrays_II.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Intersection_of_Two_Arrays_II_350 {
public static int[] intersect(int[] nums1, int[] nums2) {
Map<Integer, Integer> hm = new HashMap<>();
List<Integer> intersection = new ArrayList<>();

for (int i = 0; i < nums1.length; i++) {
hm.put(nums1[i], hm.getOrDefault(nums1[i], 0) + 1);
}

for (int i = 0; i < nums2.length; i++) {
int freq = hm.getOrDefault(nums2[i], 0);
if (freq > 0) {
intersection.add(nums2[i]);
hm.put(nums2[i], --freq);
}
}

int[] result = new int[intersection.size()];
int idx = 0;
for (int commonVal : intersection) {
result[idx++] = commonVal;
}

return result;
}
}
Loading

0 comments on commit fbbe6c6

Please sign in to comment.