- Notifications
You must be signed in to change notification settings - Fork2.4k
Added Java 617,7,146#267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
9b39842 Added Java 617 Merge Two Binary Trees
miladra6b31bf3 Added Java 7 Reverse Integer
miladra80550f6 Added Java 139 Word Break
miladraf5a855e added 146 LRU Cache
miladra000c09a Add java LRU Cache, Min stack, Happy number, Palindrone linked list
miladra79dbc7f add 417 pacific atlantic ware flow
miladra0703c2e Merge branch 'main' into main
neetcode-ghFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
25 changes: 25 additions & 0 deletionsjava/139-Word- Break.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| class Solution { | ||
| public boolean wordBreak(String s, List<String> wordDict) { | ||
| boolean[] dp = new boolean[s.length() + 1]; | ||
| Set<String> workDictSet = new HashSet<>(wordDict); | ||
| for(int i=0; i<dp.length ; i++){ | ||
| dp[i] = false; | ||
| } | ||
| dp[s.length()] = true; | ||
| for(int i= s.length() -1 ; i>=0; i--){ | ||
| for(String word : wordDict){ | ||
| if( ((i + word.length()) <= s.length()) | ||
| && (s.substring(i , i + word.length()).startsWith(word))){ | ||
| dp[i] = dp[i + word.length()]; | ||
| } | ||
| if(dp[i]){ | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| return dp[0]; | ||
| } | ||
| } |
91 changes: 91 additions & 0 deletionsjava/146-LRU-Cache.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| class LRUCache { | ||
| private Map<Integer,Node> cache; | ||
| private int capacity; | ||
| private Node left; | ||
| private Node right; | ||
| public LRUCache(int capacity) { | ||
| this.capacity = capacity; | ||
| cache = new HashMap<>(); | ||
| //left = LRU , right = most recent | ||
| this.left = new Node(0,0); | ||
| this.right = new Node(0,0); | ||
| this.left.next = this.right; | ||
| this.right.prev = this.left; | ||
| } | ||
| public int get(int key) { | ||
| if(cache.containsKey(key)){ | ||
| remove(cache.get(key)); | ||
| insert(cache.get(key)); | ||
| return cache.get(key).val; | ||
| } else{ | ||
| return -1; | ||
| } | ||
| } | ||
| public void put(int key, int value) { | ||
| if(cache.containsKey(key)){ | ||
| remove(cache.get(key)); | ||
| } | ||
| cache.put(key , new Node(key , value)); | ||
| insert(cache.get(key)); | ||
| if(cache.size() > capacity){ | ||
| // remove from the list and delte the LRU from the hashmap | ||
| Node lru = this.left.next; | ||
| remove(lru); | ||
| cache.remove(lru.key); | ||
| } | ||
| } | ||
| // remove node from list | ||
| public void remove(Node node) { | ||
| Node prev = node.prev; | ||
| Node next = node.next; | ||
| prev.next = next; | ||
| next.prev = prev; | ||
| } | ||
| // insert node at right | ||
| public void insert(Node node) { | ||
| Node prev = this.right.prev; | ||
| Node next = this.right; | ||
| prev.next = node; | ||
| next.prev = node; | ||
| node.next =next; | ||
| node.prev =prev; | ||
| } | ||
| private class Node{ | ||
| private int key; | ||
| private int val; | ||
| Node next; | ||
| Node prev; | ||
| public Node(int key , int val){ | ||
| this.key = key; | ||
| this.val = val; | ||
| } | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletionsjava/155-Min-Stack.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| class MinStack { | ||
| private Stack<Integer> stack; | ||
| private Stack<Integer> minStack; | ||
| public MinStack() { | ||
| stack = new Stack<>(); | ||
| minStack = new Stack<>(); | ||
| } | ||
| public void push(int val) { | ||
| stack.push(val); | ||
| // The min stack may be empty, so we need to check it | ||
| val = Math.min(val, minStack.isEmpty() ? val : minStack.peek()); | ||
| minStack.push(val); | ||
| } | ||
| public void pop() { | ||
| stack.pop(); | ||
| minStack.pop(); | ||
| } | ||
| public int top() { | ||
| return stack.peek(); | ||
| } | ||
| public int getMin() { | ||
| return minStack.peek(); | ||
| } | ||
| } | ||
| /** | ||
| * Your MinStack object will be instantiated and called as such: | ||
| * MinStack obj = new MinStack(); | ||
| * obj.push(val); | ||
| * obj.pop(); | ||
| * int param_3 = obj.top(); | ||
| * int param_4 = obj.getMin(); | ||
| */ |
41 changes: 41 additions & 0 deletionsjava/202-Happy-Number.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| class Solution { | ||
| public boolean isHappy(int n) { | ||
| if (n == 1 || n == -1) { | ||
| return true; | ||
| } | ||
| Set<Integer> visit = new HashSet<Integer>(); | ||
| // compute square until getting duplicate value | ||
| while (!visit.contains(n)) { | ||
| visit.add(n); | ||
| // using helper function to compute the sum of squares | ||
| n = sumOfSquare(n); | ||
| if (n == 1) | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| public int sumOfSquare(int n) { | ||
| int output = 0; | ||
| while (n != 0) { | ||
| int digit = n % 10; | ||
| digit = digit * digit; | ||
| output += digit; | ||
| n = n / 10; | ||
| } | ||
| return output; | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletionsjava/234-Palindrome-Linked-List.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| class Solution { | ||
| public boolean isPalindrome(ListNode head) { | ||
| ListNode fast = head; | ||
| ListNode slow = head; | ||
| while (fast != null && fast.next != null) { | ||
| // fast pointer shifted twice | ||
| fast = fast.next; | ||
| fast = fast.next; | ||
| // slow pointer shifted only once | ||
| slow = slow.next; | ||
| } | ||
| // reverse second half | ||
| ListNode prev = null; | ||
| while (slow != null) { | ||
| ListNode tmp = slow.next; | ||
| slow.next = prev; | ||
| prev = slow; | ||
| slow = tmp; | ||
| } | ||
| ListNode left = head; | ||
| ListNode right = prev; | ||
| while (right != null) { | ||
| if (left.val != right.val) { | ||
| return false; | ||
| } | ||
| left = left.next; | ||
| right = right.next; | ||
| } | ||
| return true; | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletionsjava/617-Merge-Two-Binary-Trees.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| class Solution { | ||
| public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { | ||
| if(root1 == null && root2 == null) return null; | ||
| int val1 = root1 != null ? root1.val : 0; | ||
| int val2 = root2 != null ? root2.val : 0; | ||
| TreeNode root = new TreeNode(val1+val2); | ||
| // merge left side of trees if they are not null | ||
| root.left = mergeTrees((root1 != null && root1.left != null) ? root1.left : null , (root2 != null && root2.left != null) ? root2.left : null); | ||
| // merge righ side of trees if they are not null | ||
| root.right = mergeTrees((root1 != null && root1.right != null) ? root1.right : null , (root2 !=null && root2.right != null) ? root2.right: null ); | ||
| return root; | ||
| } | ||
| } | ||
26 changes: 26 additions & 0 deletionsjava/7-Reverse-Integer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| class Solution { | ||
| public int reverse(int x) { | ||
| int min = Integer.MIN_VALUE; | ||
| int max = Integer.MAX_VALUE; | ||
| int res = 0; | ||
| while(x!=0){ | ||
| int digit = x % 10; | ||
| x = x / 10; | ||
| if((res> max / 10) || (res == max / 10 && digit >= max % 10)) | ||
| return 0; | ||
| if((res < min / 10) || (res== min / 10 && digit <= min % 10)) | ||
| return 0; | ||
| res = (res * 10) + digit; | ||
| } | ||
| return res; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.