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

Commitad9e9cd

Browse files
committed
Added 5 solutions
1 parentf336742 commitad9e9cd

5 files changed

+233
-0
lines changed

‎Easy/Max Stack.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
classMaxStack {
2+
3+
/** initialize your data structure here. */
4+
PriorityQueue<Integer>pr;
5+
List<Integer>list;
6+
publicMaxStack() {
7+
pr =newPriorityQueue<>((a,b) ->b-a);
8+
list =newArrayList<>();
9+
}
10+
11+
publicvoidpush(intx) {
12+
pr.add(x);
13+
list.add(x);
14+
}
15+
16+
publicintpop() {
17+
intres =list.get(list.size()-1);
18+
list.remove(list.size()-1);
19+
pr.remove(res);
20+
returnres;
21+
}
22+
23+
publicinttop() {
24+
returnlist.get(list.size()-1);
25+
}
26+
27+
publicintpeekMax() {
28+
returnpr.peek();
29+
}
30+
31+
publicintpopMax() {
32+
intres =pr.poll();
33+
inti =list.size()-1;
34+
35+
while (i >=0) {
36+
if (list.get(i) ==res) {
37+
break;
38+
}
39+
40+
i--;
41+
}
42+
43+
list.remove(i);
44+
45+
returnres;
46+
}
47+
}
48+
49+
/**
50+
* Your MaxStack object will be instantiated and called as such:
51+
* MaxStack obj = new MaxStack();
52+
* obj.push(x);
53+
* int param_2 = obj.pop();
54+
* int param_3 = obj.top();
55+
* int param_4 = obj.peekMax();
56+
* int param_5 = obj.popMax();
57+
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
classSolution {
11+
publicintlongestConsecutive(TreeNoderoot) {
12+
if (root ==null) {
13+
return0;
14+
}
15+
16+
int[]ans = {1};
17+
helper(root,ans,null,1);
18+
19+
returnans[0];
20+
}
21+
22+
privatevoidhelper(TreeNoderoot,int[]ans,TreeNodeparent,intcount) {
23+
if (root ==null) {
24+
return;
25+
}
26+
27+
if (parent !=null) {
28+
if (root.val ==parent.val +1) {
29+
count++;
30+
ans[0] =Math.max(ans[0],count);
31+
}
32+
else {
33+
count =1;
34+
}
35+
}
36+
37+
helper(root.left,ans,root,count);
38+
helper(root.right,ans,root,count);
39+
}
40+
}

‎Medium/Coin Change 2.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
classSolution {
2+
publicstaticintchange(intamount,int[]coins) {
3+
int[]combinations =newint[amount+1];
4+
combinations[0] =1;
5+
6+
for (intcoin :coins) {
7+
for (inti=coin;i<amount+1;i++) {
8+
combinations[i] +=combinations[i-coin];
9+
}
10+
}
11+
12+
returncombinations[amount];
13+
}
14+
}

‎Medium/Design Phone Directory.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
classPhoneDirectory {
2+
3+
/** Initialize your data structure here
4+
@param maxNumbers - The maximum numbers that can be stored in the phone directory. */
5+
int[]directory;
6+
intpos;
7+
publicPhoneDirectory(intmaxNumbers) {
8+
directory =newint[maxNumbers];
9+
for (inti=0;i<maxNumbers;i++) {
10+
directory[i] = (i+1)%maxNumbers;
11+
}
12+
pos =0;
13+
}
14+
15+
/** Provide a number which is not assigned to anyone.
16+
@return - Return an available number. Return -1 if none is available. */
17+
publicintget() {
18+
if (directory[pos] == -1) {
19+
return -1;
20+
}
21+
22+
intval =pos;
23+
pos =directory[pos];
24+
directory[val] = -1;
25+
returnval;
26+
}
27+
28+
/** Check if a number is available or not. */
29+
publicbooleancheck(intnumber) {
30+
returndirectory[number] != -1;
31+
}
32+
33+
/** Recycle or release a number. */
34+
publicvoidrelease(intnumber) {
35+
if (directory[number] != -1) {
36+
return;
37+
}
38+
directory[number] =pos;
39+
pos =number;
40+
}
41+
}
42+
43+
/**
44+
* Your PhoneDirectory object will be instantiated and called as such:
45+
* PhoneDirectory obj = new PhoneDirectory(maxNumbers);
46+
* int param_1 = obj.get();
47+
* boolean param_2 = obj.check(number);
48+
* obj.release(number);
49+
*/
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
classTrie {
2+
3+
Nodetrie;
4+
/** Initialize your data structure here. */
5+
publicTrie() {
6+
trie =newNode("");
7+
}
8+
9+
/** Inserts a word into the trie. */
10+
publicvoidinsert(Stringword) {
11+
Nodecurr =trie;
12+
for(inti=0;i<word.length();i++) {
13+
if (!curr.childrens.containsKey(word.charAt(i))) {
14+
curr.childrens.put(word.charAt(i),newNode(word.substring(0,i+1)));
15+
}
16+
17+
curr =curr.childrens.get(word.charAt(i));
18+
19+
if (i ==word.length() -1) {
20+
curr.isWord =true;
21+
}
22+
}
23+
}
24+
25+
/** Returns if the word is in the trie. */
26+
publicbooleansearch(Stringword) {
27+
Nodecurr =trie;
28+
for (inti=0;i<word.length();i++) {
29+
if (curr.childrens.containsKey(word.charAt(i))) {
30+
curr =curr.childrens.get(word.charAt(i));
31+
}
32+
else {
33+
returnfalse;
34+
}
35+
}
36+
37+
returncurr.isWord;
38+
}
39+
40+
/** Returns if there is any word in the trie that starts with the given prefix. */
41+
publicbooleanstartsWith(Stringprefix) {
42+
Nodecurr =trie;
43+
44+
for (inti=0;i<prefix.length();i++) {
45+
if (curr.childrens.containsKey(prefix.charAt(i))) {
46+
curr =curr.childrens.get(prefix.charAt(i));
47+
}
48+
else {
49+
returnfalse;
50+
}
51+
}
52+
53+
returntrue;
54+
}
55+
56+
privatestaticfinalclassNode {
57+
Stringprefix;
58+
Map<Character,Node>childrens;
59+
booleanisWord;
60+
61+
publicNode(Stringprefix) {
62+
this.prefix =prefix;
63+
this.childrens =newHashMap<>();
64+
}
65+
}
66+
}
67+
/**
68+
* Your Trie object will be instantiated and called as such:
69+
* Trie obj = new Trie();
70+
* obj.insert(word);
71+
* boolean param_2 = obj.search(word);
72+
* boolean param_3 = obj.startsWith(prefix);
73+
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp