Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Naughton Jr authored and Kevin Naughton Jr committed Mar 22, 2018
1 parent 614c349 commit e9a0525
Show file tree
Hide file tree
Showing 310 changed files with 2,055 additions and 4,707 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added Company/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
* ListNode(int x) { val = x; }
* }
*/
public class Solution {

public class AddTwoNumbers {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

ListNode current1 = l1;
ListNode current2 = l2;

Expand Down
14 changes: 4 additions & 10 deletions Company/Airbnb/convertSortedArrayToBinarySearchTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@
* }
*/
public class Solution {

public TreeNode sortedArrayToBST(int[] nums) {

if(nums.length == 0) return null;
if(nums.length == 0) {
return null;
}

TreeNode root = helper(nums, 0, nums.length - 1);

return root;

}

private TreeNode helper(int[] nums, int start, int end) {

if(start <= end) {

int mid = (start + end) / 2;

TreeNode current = new TreeNode(nums[mid]);
Expand All @@ -33,11 +30,8 @@ private TreeNode helper(int[] nums, int start, int end) {
current.right = helper(nums, mid + 1, end);

return current;

}

return null;

}

}
}
23 changes: 10 additions & 13 deletions Company/Airbnb/houseRobber.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,29 @@

// Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

public class Solution {

public class HouseRobber {
public int rob(int[] nums) {

if(nums.length == 0) return 0;
if(nums.length == 1) return nums[0];
if(nums.length == 0) {
return 0;
}

if(nums.length == 1) {
return nums[0];
}

int[] dp = new int[nums.length];

dp[0] = nums[0];
dp[1] = nums[0] > nums[1] ? nums[0] : nums[1];

for(int i = 2; i < nums.length; i++) {

dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);

}

for(int i = 0; i < dp.length; i++) {

System.out.print(dp[i] + " ");

}

return dp[dp.length - 1];

}

}
}
35 changes: 20 additions & 15 deletions Company/Airbnb/mergeKSortedLists.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,44 @@
* ListNode(int x) { val = x; }
* }
*/
public class Solution {

public class MergeKSortedLists {
public ListNode mergeKLists(ListNode[] lists) {

if (lists==null||lists.length==0) return null;
if (lists==null||lists.length==0) {
return null;
}

PriorityQueue<ListNode> queue= new PriorityQueue<ListNode>(lists.length,new Comparator<ListNode>(){
@Override
public int compare(ListNode o1,ListNode o2){
if (o1.val<o2.val)
if (o1.val<o2.val) {
return -1;
else if (o1.val==o2.val)
} else if (o1.val==o2.val) {
return 0;
else
} else {
return 1;
}
}
});

ListNode dummy = new ListNode(0);
ListNode tail=dummy;

for (ListNode node:lists)
if (node!=null)
for (ListNode node:lists) {
if (node!=null) {
queue.add(node);

while (!queue.isEmpty()){
}
}

while (!queue.isEmpty()) {
tail.next=queue.poll();
tail=tail.next;
if (tail.next!=null)

if (tail.next!=null) {
queue.add(tail.next);
}

}
return dummy.next;
}

}
}
}
34 changes: 6 additions & 28 deletions Company/Airbnb/regularExpressionMatching.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,41 @@
// isMatch("ab", ".*") → true
// isMatch("aab", "c*a*b") → true

public class Solution {

public class RegularExpressionMatching {
public boolean isMatch(String s, String p) {

if(s == null || p == null) return false;
if(s == null || p == null) {
return false;
}

boolean[][] dp = new boolean[s.length() + 1][p.length() + 1];
dp[0][0] = true;

for(int i = 0; i < p.length(); i++) {

if(p.charAt(i) == '*' && dp[0][i - 1]) {

dp[0][i + 1] = true;

}

}

for(int i = 0; i < s.length(); i++) {

for(int j = 0; j < p.length(); j++) {

if(p.charAt(j) == '.') {

dp[i + 1][j + 1] = dp[i][j];

}

if(p.charAt(j) == s.charAt(i)) {

dp[i + 1][j + 1] = dp[i][j];

}

if(p.charAt(j) == '*') {

if(p.charAt(j - 1) != s.charAt(i) && p.charAt(j - 1) != '.') {

dp[i + 1][j + 1] = dp[i + 1][j - 1];

}

else {

} else {
dp[i + 1][j + 1] = (dp[i + 1][j] || dp[i][j + 1] || dp[i + 1][j - 1]);

}

}

}

}

return dp[s.length()][p.length()];

}

}
}
13 changes: 3 additions & 10 deletions Company/Airbnb/twoSum.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,23 @@
// Because nums[0] + nums[1] = 2 + 7 = 9,
// return [0, 1].

public class Solution {

public class TwoSum {
public int[] twoSum(int[] nums, int target) {

int[] result = new int[2];

HashMap<Integer, Integer> map = new HashMap<>();

for(int i = 0; i < nums.length; i++) {

if(map.containsKey(target - nums[i])) {

result[1] = i;
result[0] = map.get(target - nums[i]);

return result;

}

map.put(nums[i], i);

}

return result;

}

}
}
36 changes: 5 additions & 31 deletions Company/Airbnb/validParentheses.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,28 @@

// The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

public class Solution {

public class ValidParentheses {
public boolean isValid(String s) {

if(s.length() % 2 == 1) {

return false;

}

Stack<Character> stack = new Stack<Character>();

for(int i = 0; i < s.length(); i++) {

if(s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{') {

stack.push(s.charAt(i));

}

else if(s.charAt(i) == ')' && !stack.isEmpty() && stack.peek() == '(') {

} else if(s.charAt(i) == ')' && !stack.isEmpty() && stack.peek() == '(') {
stack.pop();

}

else if(s.charAt(i) == ']' && !stack.isEmpty() && stack.peek() == '[') {

} else if(s.charAt(i) == ']' && !stack.isEmpty() && stack.peek() == '[') {
stack.pop();

}

else if(s.charAt(i) == '}' && !stack.isEmpty() && stack.peek() == '{') {

} else if(s.charAt(i) == '}' && !stack.isEmpty() && stack.peek() == '{') {
stack.pop();

}

else {

} else {
return false;

}

}

return stack.isEmpty();

}

}
35 changes: 10 additions & 25 deletions Company/Amazon/3Sum.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,23 @@
// [-1, -1, 2]
// ]

public class Solution {

public class 3Sum {
public List<List<Integer>> threeSum(int[] nums) {

List<List<Integer>> result = new ArrayList<>();

Arrays.sort(nums);

for(int i = 0; i < nums.length - 2; i++) {

if(i > 0 && nums[i] == nums[i - 1]) {

continue;

}

int j = i + 1;
int k = nums.length - 1;
int target = -nums[i];

while(j < k) {

if(nums[j] + nums[k] == target) {

ArrayList<Integer> temp = new ArrayList<Integer>();

temp.add(nums[i]);
Expand All @@ -45,29 +38,21 @@ public List<List<Integer>> threeSum(int[] nums) {
j++;
k--;

while(j < k && nums[j] == nums[j - 1]) j++;
while(j < k && nums[k] == nums[k + 1]) k--;

}

else if(nums[j] + nums[k] > target) {
while(j < k && nums[j] == nums[j - 1]) {
j++;
}

while(j < k && nums[k] == nums[k + 1]) {
k--;
}
} else if(nums[j] + nums[k] > target) {
k--;

}

else {

} else {
j++;

}

}

}

return result;

}

}
}
Loading

0 comments on commit e9a0525

Please sign in to comment.