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

Commitcd8cc38

Browse files
committed
2 parents33f27d2 +8daca7d commitcd8cc38

File tree

36 files changed

+782
-33
lines changed

36 files changed

+782
-33
lines changed

‎README.md

Lines changed: 8 additions & 8 deletions
Large diffs are not rendered by default.

‎cpp/0394-decode-string.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
classSolution {
2+
public:
3+
stringdecodeString(string s) {
4+
stack<string> stack;
5+
string result;
6+
7+
for (int i =0; i < s.length(); i++) {
8+
if (s[i] !=']') {
9+
stack.push(string(1, s[i]));
10+
}else {
11+
string substr;
12+
while (!stack.empty() && stack.top() !="[") {
13+
substr = stack.top() + substr;
14+
stack.pop();
15+
}
16+
stack.pop();
17+
18+
string k;
19+
while (!stack.empty() &&isdigit(stack.top()[0])) {
20+
k = stack.top() + k;
21+
stack.pop();
22+
}
23+
intkInt =stoi(k);
24+
25+
string temp;
26+
for (int j =0; j <kInt; j++) {
27+
temp += substr;
28+
}
29+
stack.push(temp);
30+
}
31+
}
32+
33+
while (!stack.empty()) {
34+
result = stack.top() + result;
35+
stack.pop();
36+
}
37+
38+
return result;
39+
}
40+
};

‎java/0226-invert-binary-tree.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ public TreeNode invertTree(TreeNode root) {
44
if (root ==null)returnnull;
55
TreeNodenode =newTreeNode(root.val);
66
node.right =invertTree(root.left);
7-
node.val =root.val;
87
node.left =invertTree(root.right);
98
returnnode;
109
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
classNumMatrix {
2+
int[][]preSum;
3+
4+
publicNumMatrix(int[][]matrix) {
5+
intm =matrix.length;
6+
intn =matrix[0].length;
7+
preSum =newint[m +1][n +1];
8+
9+
for (inti =0;i <m;i++) {
10+
for (intj =0;j <n;j++) {
11+
preSum[i +1][j +1] =preSum[i +1][j] +preSum[i][j +1] -preSum[i][j] +matrix[i][j];
12+
}
13+
}
14+
}
15+
16+
publicintsumRegion(introw1,intcol1,introw2,intcol2) {
17+
returnpreSum[row2 +1][col2 +1] -preSum[row2 +1][col1] -preSum[row1][col2 +1] +preSum[row1][col1];
18+
}
19+
}
20+
21+
/**
22+
* Your NumMatrix object will be instantiated and called as such:
23+
* NumMatrix obj = new NumMatrix(matrix);
24+
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
25+
*/

‎java/0661-image-smoother.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
classSolution {
2+
publicint[][]imageSmoother(int[][]img) {
3+
intROWS =img.length,COLS =img[0].length;
4+
int[][]res =newint[ROWS][COLS];
5+
6+
for(intr =0;r <ROWS;r++){
7+
for(intc =0;c <COLS;c++){
8+
inttotal =0,cnt =0;
9+
for(inti =r -1;i <r +2;i++){
10+
for(intj =c -1;j <c +2;j++){
11+
if(i <0 ||i ==ROWS ||j <0 ||j ==COLS)
12+
continue;
13+
total +=img[i][j];
14+
cnt +=1;
15+
}
16+
}
17+
res[r][c] =total /cnt;
18+
}
19+
}
20+
returnres;
21+
}
22+
}
23+
24+
// Optimal Solution
25+
26+
classSolution {
27+
publicint[][]imageSmoother(int[][]img) {
28+
intROWS =img.length,COLS =img[0].length;
29+
30+
for(intr =0;r <ROWS;r++){
31+
for(intc =0;c <COLS;c++){
32+
inttotal =0,cnt =0;
33+
34+
for(inti =r -1;i <r +2;i++){
35+
for(intj =c -1;j <c +2;j++){
36+
if(i <0 ||i ==ROWS ||j <0 ||j ==COLS)
37+
continue;
38+
total +=img[i][j] %256;
39+
cnt +=1;
40+
}
41+
}
42+
img[r][c] =img[r][c] ^ (total /cnt) <<8;
43+
}
44+
}
45+
for(intr =0;r <ROWS;r++){
46+
for(intc =0;c <COLS;c++){
47+
img[r][c] =img[r][c] >>8;
48+
}
49+
}
50+
returnimg;
51+
}
52+
}

‎java/0704-binary-search.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ public int search(int[] nums, int target) {
55
intj =nums.length -1;
66

77
while (i <=j) {
8-
intmid = (i +j) /2;
8+
// mid is calculated this way to prevent integer overflow.
9+
// See: https://blog.research.google/2006/06/extra-extra-read-all-about-it-nearly.html
10+
intmid =i + (j -i) /2;
911

1012
if (nums[mid] ==target)returnmid;elseif (
1113
nums[mid] <target

‎java/0904-fruit-into-baskets.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
classSolution {
2+
publicinttotalFruit(int[]fruits) {
3+
Map<Integer,Integer>fruitTypeToItsCount =newHashMap<>();
4+
intmaxTotalFruits =0;
5+
intl =0;
6+
intr =0;
7+
8+
while (r <fruits.length) {
9+
fruitTypeToItsCount.put(fruits[r],fruitTypeToItsCount.getOrDefault(fruits[r],0) +1);
10+
11+
while (fruitTypeToItsCount.size() >2) {
12+
fruitTypeToItsCount.put(fruits[l],fruitTypeToItsCount.get(fruits[l]) -1);
13+
if (fruitTypeToItsCount.get(fruits[l]) ==0) {
14+
fruitTypeToItsCount.remove(fruits[l]);
15+
}
16+
l++;
17+
}
18+
19+
maxTotalFruits =Math.max(maxTotalFruits,r -l +1);
20+
r++;
21+
}
22+
23+
returnmaxTotalFruits;
24+
}
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
classSolution {
2+
publicbooleanvalidateStackSequences(int[]pushed,int[]popped) {
3+
Stack<Integer>stack =newStack<>();
4+
5+
inti =0;
6+
for (intvalue :pushed) {
7+
stack.push(value);
8+
9+
while (i <popped.length && !stack.isEmpty() &&stack.peek() ==popped[i]) {
10+
stack.pop();
11+
i++;
12+
}
13+
}
14+
15+
returnstack.isEmpty();
16+
}
17+
}
Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,47 @@
1+
/* Top Down Method
2+
-----------------------------------*/
13
classSolution {
2-
3-
intmod =1000000007;
4+
intMOD = (int)1e9 +7;
5+
Map<String,Integer>memo;
46

57
publicintnumRollsToTarget(intn,intk,inttarget) {
6-
if (target >n *k ||target <n)return0;
7-
if (n ==1)returntarget <n ?0 :1;
8-
int[][]dp =newint[n +1][target +1];
9-
returnhelper(n,k,target,dp);
8+
memo =newHashMap<>();
9+
returncount(n,k,target);
10+
}
11+
12+
privateintcount(intn,intk,inttarget){
13+
StringcurrState =n +"," +target;
14+
if(n ==0)
15+
return (target ==0)?1:0;
16+
if(memo.containsKey(currState))
17+
returnmemo.get(currState);
18+
19+
intres =0;
20+
for(intval =1;val <k +1;val++)
21+
res = (res +count(n -1,k,target -val)) %MOD;
22+
memo.put(currState,res);
23+
returnres;
1024
}
25+
}
26+
27+
/* Bottom Up Method
28+
-----------------------------------------*/
29+
classSolution {
30+
publicintnumRollsToTarget(intn,intk,inttarget) {
31+
int[]dp =newint[target +1];
32+
dp[0] =1;
33+
intMOD = (int)1e9 +7;
34+
35+
for(intdice =0;dice <n;dice++){
36+
int[]next_dp =newint[target +1];
1137

12-
publicinthelper(intn,intk,inttarget,int[][]dp) {
13-
if (target >n *k ||target <n)return0;
14-
if (target ==0 &&n ==0)return1;
15-
if (dp[n][target] !=0)returndp[n][target];
16-
intsum =0;
17-
for (inti =1;i <=k;i++) {
18-
sum = (sum +helper(n -1,k,target -i,dp)) %mod;
38+
for(intval =1;val <k +1;val++){
39+
for(inttotal =val;total <target +1;total++){
40+
next_dp[total] = (next_dp[total] +dp[total -val]) %MOD;
41+
}
42+
}
43+
dp =next_dp;
1944
}
20-
returndp[n][target] =sum;
45+
returndp[target];
2146
}
2247
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
classSolution {
2+
publicintmaxScore(Strings) {
3+
intzero =0,one =0;
4+
intres =0;
5+
6+
for(charc:s.toCharArray()){
7+
if(c =='1')
8+
one +=1;
9+
}
10+
for(inti =0;i <s.length()-1;i++){
11+
charc =s.charAt(i);
12+
if(c =='0')
13+
zero +=1;
14+
else
15+
one -=1;
16+
res =Math.max(res,zero +one);
17+
}
18+
returnres;
19+
}
20+
}

‎java/1436-destination-city.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
classSolution {
2+
publicStringdestCity(List<List<String>>paths) {
3+
Set<String>set =newHashSet<>();
4+
for(List<String>path:paths){
5+
Stringcity =path.get(0);
6+
set.add(city);
7+
}
8+
9+
for(List<String>path:paths){
10+
Stringcity =path.get(1);
11+
if(!set.contains(city))
12+
returncity;
13+
}
14+
return"";
15+
}
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
classSolution {
2+
publicintmaxProduct(int[]nums) {
3+
Arrays.sort(nums);
4+
5+
return (nums[nums.length-1]-1)*(nums[nums.length-2] -1);
6+
}
7+
}

‎java/1496-path-crossing.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
classSolution {
2+
publicbooleanisPathCrossing(Stringpath) {
3+
Set<String>set =newHashSet<>();
4+
set.add("[0, 0]");
5+
int[]pos = {0,0};
6+
7+
for(charc:path.toCharArray()){
8+
if(c =='N'){
9+
pos[1] +=1;
10+
}
11+
elseif(c =='S')
12+
pos[1] -=1;
13+
elseif(c =='E')
14+
pos[0] +=1;
15+
else
16+
pos[0] -=1;
17+
if(set.contains(Arrays.toString(pos)))
18+
returntrue;
19+
set.add(Arrays.toString(pos));
20+
}
21+
returnfalse;
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*-------------------------------
2+
Time Complexity: O(n)
3+
Space Complexity: O(1)
4+
--------------------------------*/
5+
classSolution {
6+
publicintminCost(Stringcolors,int[]neededTime) {
7+
intres =0,l =0;
8+
9+
for(intr =1;r <colors.length();r++){
10+
if(colors.charAt(l) ==colors.charAt(r)){
11+
if(neededTime[l] <neededTime[r]){
12+
res +=neededTime[l];
13+
l =r;
14+
}
15+
else
16+
res +=neededTime[r];
17+
}
18+
else
19+
l =r;
20+
}
21+
returnres;
22+
}
23+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
classSolution {
2+
publicintnumSpecial(int[][]mat) {
3+
intr =mat.length;
4+
intc =mat[0].length;
5+
int[]rowOnes =newint[r];
6+
int[]colOnes =newint[c];
7+
8+
for(inti =0;i <r;i++){
9+
for(intj =0;j <c;j++){
10+
if(mat[i][j] ==1){
11+
rowOnes[i]++;
12+
colOnes[j]++;
13+
}
14+
}
15+
}
16+
17+
intres =0;
18+
for(inti =0;i <r;i++){
19+
for(intj =0;j <c;j++){
20+
if(mat[i][j] ==1 &&rowOnes[i] ==1 &&colOnes[j] ==1){
21+
res++;
22+
}
23+
}
24+
}
25+
returnres;
26+
}
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*--------------------------------
2+
Time Complexity: O(nlog(n))
3+
Space Complexity: O(1)
4+
---------------------------------*/
5+
classSolution {
6+
publicintmaxWidthOfVerticalArea(int[][]points) {
7+
Arrays.sort(points, (p1,p2) ->p1[0] -p2[0]);
8+
9+
intres =0;
10+
for(inti =1;i <points.length;i++){
11+
res =Math.max(res,points[i][0] -points[i-1][0]);
12+
}
13+
returnres;
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
classSolution {
2+
publicintminOperations(Strings) {
3+
intcount =0;
4+
5+
for(inti =0;i <s.length();i++){
6+
if(i %2 ==1)
7+
count += (s.charAt(i) =='0')?1:0;
8+
else
9+
count += (s.charAt(i) =='1')?1:0;
10+
}
11+
returnMath.min(count,s.length() -count);
12+
}
13+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp