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

Commitffe284e

Browse files
committed
Added 4 solutions & modified 1 solution
1 parent8839319 commitffe284e

5 files changed

+132
-18
lines changed

‎Easy/Defanging an IP Address.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
classSolution {
2+
publicStringdefangIPaddr(Stringaddress) {
3+
StringBuildersb =newStringBuilder();
4+
for (charc :address.toCharArray()) {
5+
if (c =='.') {
6+
sb.append("[.]");
7+
}
8+
else {
9+
sb.append(c);
10+
}
11+
}
12+
13+
returnsb.toString();
14+
}
15+
}

‎Medium/Corporate Flight Bookings.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
classSolution {
2+
publicint[]corpFlightBookings(int[][]bookings,intn) {
3+
int[]ans =newint[n];
4+
5+
for (int[]booking :bookings) {
6+
inttickets =booking[2];
7+
intfrom =booking[0];
8+
intto =booking[1];
9+
10+
ans[from -1] +=tickets;
11+
if (to <n) {
12+
ans[to] -=tickets;
13+
}
14+
}
15+
16+
intcurrSum =0;
17+
for (inti =0;i <n;i++) {
18+
currSum +=ans[i];
19+
ans[i] =currSum;
20+
}
21+
22+
returnans;
23+
}
24+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
List<TreeNode>forest;
12+
publicList<TreeNode>delNodes(TreeNoderoot,int[]to_delete) {
13+
forest =newArrayList<>();
14+
Set<Integer>toDelete =newHashSet<>();
15+
for (intval :to_delete) {
16+
toDelete.add(val);
17+
}
18+
19+
helper(root,toDelete);
20+
21+
if (!toDelete.contains(root.val)) {
22+
forest.add(root);
23+
}
24+
25+
returnforest;
26+
}
27+
28+
privateTreeNodehelper(TreeNoderoot,Set<Integer>toDelete) {
29+
if (root ==null) {
30+
returnnull;
31+
}
32+
33+
root.left =helper(root.left,toDelete);
34+
root.right =helper(root.right,toDelete);
35+
36+
if (toDelete.contains(root.val)) {
37+
if (root.left !=null) {
38+
forest.add(root.left);
39+
}
40+
41+
if (root.right !=null) {
42+
forest.add(root.right);
43+
}
44+
45+
returnnull;
46+
}
47+
48+
returnroot;
49+
}
50+
}

‎Medium/Random Pick Index.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
classSolution {
2+
Map<Integer,List<Integer>>map =newHashMap();
3+
Randomrand;
4+
publicSolution(int[]nums) {
5+
rand =newRandom();
6+
for (inti =0;i <nums.length;i++) {
7+
map.computeIfAbsent(nums[i],k ->newArrayList<>()).add(i);
8+
}
9+
}
10+
11+
publicintpick(inttarget) {
12+
List<Integer>indexes =map.get(target);
13+
returnindexes.get(rand.nextInt(indexes.size()));
14+
}
15+
}
16+
17+
/**
18+
* Your Solution object will be instantiated and called as such:
19+
* Solution obj = new Solution(nums);
20+
* int param_1 = obj.pick(target);
21+
*/

‎Medium/Random Pick With Weight.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
classSolution {
22

3-
List<Integer>prefixSum;
4-
inttotal;
5-
Randomrand;
3+
int[]accumulatedSum;
4+
Randomrandom;
65
publicSolution(int[]w) {
7-
prefixSum =newArrayList<>();
8-
total =0;
9-
for (inti=0;i<w.length;i++) {
10-
total +=w[i];
11-
prefixSum.add(total);
6+
random =newRandom();
7+
accumulatedSum =newint[w.length];
8+
intcurrSum =0;
9+
for (inti =0;i <w.length;i++) {
10+
currSum +=w[i];
11+
accumulatedSum[i] =currSum;
1212
}
13-
rand =newRandom();
1413
}
1514

1615
publicintpickIndex() {
17-
inttarget =rand.nextInt(total);
18-
inti =0;
19-
intj =prefixSum.size() -1;
20-
while (i !=j) {
21-
intmid = (i+j)/2;
22-
if (target >=prefixSum.get(mid)) {
23-
i =mid +1;
16+
inttarget =random.nextInt(accumulatedSum[accumulatedSum.length -1]) +1;
17+
returnbinarySearch(accumulatedSum,0,accumulatedSum.length -1,target);
18+
}
19+
20+
privateintbinarySearch(int[]arr,intleft,intright,inttarget) {
21+
while (left <=right) {
22+
intmid = (left +right) /2;
23+
if (arr[mid] ==target) {
24+
returnmid;
25+
}
26+
elseif (arr[mid] >target) {
27+
right =mid -1;
2428
}
2529
else {
26-
j =mid;
30+
left =mid +1;
2731
}
2832
}
2933

30-
returnj;
34+
returnleft;
3135
}
3236
}
3337

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp