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

Commitf336742

Browse files
committed
Added 4 solutions
1 parent2287a5f commitf336742

4 files changed

+123
-0
lines changed

‎Easy/Valid Word Abbrevation.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
classSolution {
2+
publicstaticbooleanvalidWordAbbreviation(Stringword,Stringabbr) {
3+
char[]wordChar =word.toCharArray();
4+
char[]abbrChar =abbr.toCharArray();
5+
6+
inti =0;
7+
intj =0;
8+
9+
while (j <abbrChar.length &&i <wordChar.length) {
10+
if (abbrChar[j] ==wordChar[i]) {
11+
i++;
12+
j++;
13+
continue;
14+
}
15+
16+
if (abbrChar[j] <='0' ||abbrChar[j] >'9') {
17+
returnfalse;
18+
}
19+
20+
intstart =j;
21+
while (j<abbrChar.length &&abbrChar[j] >='0' &&abbrChar[j] <='9') {
22+
++j;
23+
}
24+
25+
intnum =Integer.valueOf(abbr.substring(start,j));
26+
i +=num;
27+
}
28+
29+
returni ==wordChar.length &&j ==abbrChar.length;
30+
}
31+
}

‎Medium/Count Univalue Subtrees.java

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+
intcount =0;
12+
publicintcountUnivalSubtrees(TreeNoderoot) {
13+
updateCount(root);
14+
returncount;
15+
}
16+
17+
privatevoidupdateCount(TreeNoderoot) {
18+
if (root ==null) {
19+
return;
20+
}
21+
22+
if (isUnivalue(root,root.val)) {
23+
count++;
24+
}
25+
26+
updateCount(root.left);
27+
updateCount(root.right);
28+
}
29+
30+
privatebooleanisUnivalue(TreeNoderoot,intval) {
31+
if (root ==null) {
32+
returntrue;
33+
}
34+
if (root.val !=val) {
35+
returnfalse;
36+
}
37+
38+
returnisUnivalue(root.left,val) &&isUnivalue(root.right,val);
39+
}
40+
}

‎Medium/Max Consecutives Ones II.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
classSolution {
2+
publicintfindMaxConsecutiveOnes(int[]nums) {
3+
intslow =0;
4+
intfast =0;
5+
intmax =Integer.MIN_VALUE;
6+
booleanflipped =false;
7+
intzeroIndex =slow;
8+
9+
while (fast <nums.length) {
10+
if (nums[fast] !=1) {
11+
if (!flipped) {
12+
flipped =true;
13+
zeroIndex =fast;
14+
}
15+
else {
16+
max =Math.max(max,fast -slow);
17+
slow =zeroIndex +1;
18+
flipped =false;
19+
fast =zeroIndex;
20+
}
21+
}
22+
23+
fast++;
24+
}
25+
26+
returnMath.max(max,fast-slow);
27+
}
28+
}

‎Medium/Sort Transformed Array.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
classSolution {
2+
publicstaticint[]sortTransformedArray(int[]nums,inta,intb,intc) {
3+
int[]ans =newint[nums.length];
4+
intstart =0;
5+
intend =nums.length-1;
6+
7+
intindex =a >=0 ?end :start;
8+
9+
while (start <=end){
10+
if (a >=0) {
11+
ans[index--] =getQuad(nums[start],a,b,c) >=getQuad(nums[end],a,b,c) ?getQuad(nums[start++],a,b,c) :getQuad(nums[end--],a,b,c);
12+
}
13+
else {
14+
ans[index++] =getQuad(nums[start],a,b,c) >=getQuad(nums[end],a,b,c) ?getQuad(nums[end--],a,b,c) :getQuad(nums[start++],a,b,c);
15+
}
16+
}
17+
18+
returnans;
19+
}
20+
21+
privatestaticintgetQuad(intnum,inta,intb,intc) {
22+
returna *(num *num) +b *num +c;
23+
}
24+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp