Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
neetcode-gh merged 7 commits intoneetcode-gh:mainfrommiladra:main
Jul 2, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletionsjava/139-Word- Break.java
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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;

}
}

[8]ページ先頭

©2009-2025 Movatter.jp