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

Commit465985b

Browse files
authored
Merge pull requestneetcode-gh#267 from miladra/main
Added Java 617,7,146
2 parentsb2f257e +abfcc55 commit465985b

File tree

7 files changed

+291
-0
lines changed

7 files changed

+291
-0
lines changed

‎java/139-Word- Break.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
classSolution {
2+
publicbooleanwordBreak(Strings,List<String>wordDict) {
3+
boolean[]dp =newboolean[s.length() +1];
4+
Set<String>workDictSet =newHashSet<>(wordDict);
5+
6+
for(inti=0;i<dp.length ;i++){
7+
dp[i] =false;
8+
}
9+
10+
dp[s.length()] =true;
11+
12+
for(inti=s.length() -1 ;i>=0;i--){
13+
for(Stringword :wordDict){
14+
if( ((i +word.length()) <=s.length())
15+
&& (s.substring(i ,i +word.length()).startsWith(word))){
16+
dp[i] =dp[i +word.length()];
17+
}
18+
if(dp[i]){
19+
break;
20+
}
21+
}
22+
}
23+
returndp[0];
24+
}
25+
}

‎java/146-LRU-Cache.java‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
classLRUCache {
2+
3+
privateMap<Integer,Node>cache;
4+
privateintcapacity;
5+
6+
privateNodeleft;
7+
privateNoderight;
8+
9+
publicLRUCache(intcapacity) {
10+
this.capacity =capacity;
11+
cache =newHashMap<>();
12+
13+
//left = LRU , right = most recent
14+
this.left =newNode(0,0);
15+
this.right =newNode(0,0);
16+
this.left.next =this.right;
17+
this.right.prev =this.left;
18+
19+
}
20+
21+
publicintget(intkey) {
22+
23+
if(cache.containsKey(key)){
24+
remove(cache.get(key));
25+
insert(cache.get(key));
26+
returncache.get(key).val;
27+
28+
}else{
29+
return -1;
30+
}
31+
32+
}
33+
34+
publicvoidput(intkey,intvalue) {
35+
36+
if(cache.containsKey(key)){
37+
remove(cache.get(key));
38+
}
39+
cache.put(key ,newNode(key ,value));
40+
insert(cache.get(key));
41+
42+
if(cache.size() >capacity){
43+
// remove from the list and delte the LRU from the hashmap
44+
Nodelru =this.left.next;
45+
remove(lru);
46+
cache.remove(lru.key);
47+
48+
}
49+
50+
51+
}
52+
53+
// remove node from list
54+
publicvoidremove(Nodenode) {
55+
56+
Nodeprev =node.prev;
57+
Nodenext =node.next;
58+
59+
prev.next =next;
60+
next.prev =prev;
61+
}
62+
63+
// insert node at right
64+
publicvoidinsert(Nodenode) {
65+
66+
Nodeprev =this.right.prev;
67+
Nodenext =this.right;
68+
69+
70+
prev.next =node;
71+
next.prev =node;
72+
73+
node.next =next;
74+
node.prev =prev;
75+
76+
77+
78+
}
79+
privateclassNode{
80+
privateintkey;
81+
privateintval;
82+
83+
Nodenext;
84+
Nodeprev;
85+
86+
publicNode(intkey ,intval){
87+
this.key =key;
88+
this.val =val;
89+
}
90+
}
91+
}

‎java/155-Min-Stack.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
classMinStack {
2+
3+
privateStack<Integer>stack;
4+
privateStack<Integer>minStack;
5+
6+
publicMinStack() {
7+
stack =newStack<>();
8+
minStack =newStack<>();
9+
10+
}
11+
12+
publicvoidpush(intval) {
13+
stack.push(val);
14+
15+
// The min stack may be empty, so we need to check it
16+
val =Math.min(val,minStack.isEmpty() ?val :minStack.peek());
17+
minStack.push(val);
18+
19+
}
20+
21+
publicvoidpop() {
22+
stack.pop();
23+
minStack.pop();
24+
25+
}
26+
27+
publicinttop() {
28+
returnstack.peek();
29+
}
30+
31+
publicintgetMin() {
32+
returnminStack.peek();
33+
34+
}
35+
}
36+
37+
/**
38+
* Your MinStack object will be instantiated and called as such:
39+
* MinStack obj = new MinStack();
40+
* obj.push(val);
41+
* obj.pop();
42+
* int param_3 = obj.top();
43+
* int param_4 = obj.getMin();
44+
*/

‎java/202-Happy-Number.java‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
classSolution {
2+
publicbooleanisHappy(intn) {
3+
4+
if (n ==1 ||n == -1) {
5+
returntrue;
6+
}
7+
8+
Set<Integer>visit =newHashSet<Integer>();
9+
10+
// compute square until getting duplicate value
11+
while (!visit.contains(n)) {
12+
visit.add(n);
13+
// using helper function to compute the sum of squares
14+
n =sumOfSquare(n);
15+
16+
if (n ==1)
17+
returntrue;
18+
19+
}
20+
21+
returnfalse;
22+
23+
}
24+
25+
publicintsumOfSquare(intn) {
26+
27+
intoutput =0;
28+
29+
while (n !=0) {
30+
intdigit =n %10;
31+
digit =digit *digit;
32+
output +=digit;
33+
n =n /10;
34+
35+
}
36+
37+
returnoutput;
38+
39+
}
40+
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
classSolution {
2+
publicbooleanisPalindrome(ListNodehead) {
3+
4+
ListNodefast =head;
5+
ListNodeslow =head;
6+
7+
while (fast !=null &&fast.next !=null) {
8+
9+
// fast pointer shifted twice
10+
fast =fast.next;
11+
fast =fast.next;
12+
13+
// slow pointer shifted only once
14+
slow =slow.next;
15+
16+
}
17+
18+
// reverse second half
19+
ListNodeprev =null;
20+
while (slow !=null) {
21+
ListNodetmp =slow.next;
22+
slow.next =prev;
23+
prev =slow;
24+
slow =tmp;
25+
}
26+
27+
ListNodeleft =head;
28+
ListNoderight =prev;
29+
30+
while (right !=null) {
31+
if (left.val !=right.val) {
32+
returnfalse;
33+
}
34+
35+
left =left.next;
36+
right =right.next;
37+
38+
}
39+
40+
returntrue;
41+
42+
}
43+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
classSolution {
2+
publicTreeNodemergeTrees(TreeNoderoot1,TreeNoderoot2) {
3+
4+
if(root1 ==null &&root2 ==null)returnnull;
5+
6+
intval1 =root1 !=null ?root1.val :0;
7+
intval2 =root2 !=null ?root2.val :0;
8+
9+
TreeNoderoot =newTreeNode(val1+val2);
10+
11+
// merge left side of trees if they are not null
12+
root.left =mergeTrees((root1 !=null &&root1.left !=null) ?root1.left :null , (root2 !=null &&root2.left !=null) ?root2.left :null);
13+
14+
// merge righ side of trees if they are not null
15+
root.right =mergeTrees((root1 !=null &&root1.right !=null) ?root1.right :null , (root2 !=null &&root2.right !=null) ?root2.right:null );
16+
17+
returnroot;
18+
}
19+
}
20+
21+

‎java/7-Reverse-Integer.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
classSolution {
2+
publicintreverse(intx) {
3+
4+
intmin =Integer.MIN_VALUE;
5+
intmax =Integer.MAX_VALUE;
6+
7+
8+
intres =0;
9+
while(x!=0){
10+
intdigit =x %10;
11+
x =x /10;
12+
13+
if((res>max /10) || (res ==max /10 &&digit >=max %10))
14+
return0;
15+
16+
if((res <min /10) || (res==min /10 &&digit <=min %10))
17+
return0;
18+
19+
20+
res = (res *10) +digit;
21+
}
22+
23+
returnres;
24+
25+
}
26+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp