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

Commitf9d23dc

Browse files
committed
Modified 3 solutions
1 parentfb7069d commitf9d23dc

File tree

3 files changed

+61
-68
lines changed

3 files changed

+61
-68
lines changed

‎Easy/Excel Sheet Column Title.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
publicclassSolution {
2-
publicStringconvertToTitle(intn) {
3-
StringBuildersb =newStringBuilder("");
4-
intmod =0;
5-
while (n >0) {
6-
mod = (n -1) %26;
7-
sb.append(String.valueOf((char) (65 +mod)));
8-
n = (n-mod)/26;
9-
}
10-
returnsb.reverse().toString();
1+
classSolution {
2+
publicStringconvertToTitle(intn) {
3+
StringBuildersb =newStringBuilder();
4+
while (n >0) {
5+
n--;
6+
sb.append((char) ('A' +n %26));
7+
n /=26;
118
}
12-
}
9+
returnsb.reverse().toString();
10+
}
11+
}

‎Easy/Non-decreasing Array.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
classSolution {
2-
publicbooleancheckPossibility(int[]a) {
3-
intc =0;
4-
for (inti =1;i <a.length;i++) {
5-
if (a[i] <a[i -1]) {
6-
if (c++ >0)returnfalse;
7-
if (i -2 <0 ||a[i -2] <=a[i])a[i -1] =a[i];
8-
elsea[i] =a[i -1];
9-
}
2+
publicbooleancheckPossibility(int[]nums) {
3+
IntegerprobIdx =null;
4+
for (inti =0;i <nums.length -1;i++) {
5+
if (nums[i] >nums[i +1]) {
6+
if (probIdx !=null) {
7+
returnfalse;
108
}
11-
returntrue;
9+
probIdx =i;
10+
}
1211
}
12+
return (
13+
probIdx ==null ||
14+
probIdx ==0 ||
15+
probIdx == (nums.length -2) ||
16+
nums[probIdx -1] <=nums[probIdx +1] ||
17+
nums[probIdx] <=nums[probIdx +2]
18+
);
19+
}
1320
}

‎Medium/Insert Delete GetRandom O(1).java

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,42 @@
11
classRandomizedSet {
22

3-
/** Initialize your data structure here. */
4-
Map<Integer,Integer>map;
5-
List<Integer>list;
6-
intlastIndex;
7-
publicRandomizedSet() {
8-
map =newHashMap<>();
9-
list =newArrayList<>();
10-
lastIndex =0;
3+
/** Initialize your data structure here. */
4+
Map<Integer,Integer>map;
5+
List<Integer>list;
6+
publicRandomizedSet() {
7+
map =newHashMap<>();
8+
list =newArrayList<>();
9+
}
10+
11+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
12+
publicbooleaninsert(intval) {
13+
if (map.containsKey(val)) {
14+
returnfalse;
1115
}
12-
13-
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
14-
publicbooleaninsert(intval) {
15-
if (map.containsKey(val)) {
16-
returnfalse;
17-
}
18-
19-
list.add(val);
20-
map.put(val,lastIndex);
21-
lastIndex++;
22-
23-
returntrue;
24-
}
25-
26-
/** Removes a value from the set. Returns true if the set contained the specified element. */
27-
publicbooleanremove(intval) {
28-
if (!map.containsKey(val)) {
29-
returnfalse;
30-
}
31-
32-
map.put(list.get(lastIndex-1),map.get(val));
33-
swap(map.get(val),lastIndex-1);
34-
list.remove(lastIndex-1);
35-
map.remove(val);
36-
lastIndex--;
37-
38-
returntrue;
39-
}
40-
41-
privatevoidswap(intid1,intid2) {
42-
inttemp =list.get(id1);
43-
list.set(id1,list.get(id2));
44-
list.set(id2,temp);
45-
}
46-
47-
/** Get a random element from the set. */
48-
publicintgetRandom() {
49-
Randomrand =newRandom();
50-
intidx =rand.nextInt(((lastIndex-1) -0) +1) +0;
51-
returnlist.get(idx);
16+
list.add(val);
17+
map.put(val,list.size() -1);
18+
returntrue;
19+
}
20+
21+
/** Removes a value from the set. Returns true if the set contained the specified element. */
22+
publicbooleanremove(intval) {
23+
if (!map.containsKey(val)) {
24+
returnfalse;
5225
}
26+
intidx =map.get(val);
27+
intlastIdx =list.size() -1;
28+
list.set(idx,list.get(lastIdx));
29+
map.put(list.get(lastIdx),idx);
30+
map.remove(val);
31+
list.remove(lastIdx);
32+
returntrue;
33+
}
34+
35+
/** Get a random element from the set. */
36+
publicintgetRandom() {
37+
intidx =newRandom().nextInt(list.size());
38+
returnlist.get(idx);
39+
}
5340
}
5441

5542
/**

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp