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

Commit1ff38ef

Browse files
committed
Added 7 solutions
1 parent8ce0747 commit1ff38ef

7 files changed

+252
-0
lines changed

‎Easy/Flip Game.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
classSolution {
2+
publicstaticList<String>generatePossibleNextMoves(Strings) {
3+
List<String>ans =newArrayList<>();
4+
5+
if (s.length() <2) {
6+
returnans;
7+
}
8+
9+
if (s.equals("--")) {
10+
returnans;
11+
}
12+
13+
inti =0;
14+
while (i <s.length()-1) {
15+
if (s.charAt(i) ==s.charAt(i+1) &&s.charAt(i) =='+') {
16+
StringBuildersb =newStringBuilder();
17+
sb.append(s.substring(0,i));
18+
sb.append('-');
19+
sb.append('-');
20+
sb.append(s.substring(i+2));
21+
22+
ans.add(sb.toString());
23+
}
24+
25+
i++;
26+
}
27+
28+
returnans;
29+
}
30+
}

‎Easy/Logger Rate Limiter.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
classLogger {
2+
3+
/** Initialize your data structure here. */
4+
Map<String,Integer>map;
5+
publicLogger() {
6+
map =newHashMap<>();
7+
}
8+
9+
/** Returns true if the message should be printed in the given timestamp, otherwise returns false.
10+
If this method returns false, the message will not be printed.
11+
The timestamp is in seconds granularity. */
12+
publicbooleanshouldPrintMessage(inttimestamp,Stringmessage) {
13+
14+
if (map.containsKey(message) &&timestamp -map.get(message) <10) {
15+
returnfalse;
16+
}
17+
18+
map.put(message,timestamp);
19+
20+
returntrue;
21+
}
22+
}
23+
24+
/**
25+
* Your Logger object will be instantiated and called as such:
26+
* Logger obj = new Logger();
27+
* boolean param_1 = obj.shouldPrintMessage(timestamp,message);
28+
*/

‎Easy/Meeting Rooms.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for an interval.
3+
* public class Interval {
4+
* int start;
5+
* int end;
6+
* Interval() { start = 0; end = 0; }
7+
* Interval(int s, int e) { start = s; end = e; }
8+
* }
9+
*/
10+
classSolution {
11+
publicstaticbooleancanAttendMeetings(Interval[]intervals) {
12+
Arrays.sort(intervals,newComparator<Interval>() {
13+
@Override
14+
publicintcompare(Intervalo1,Intervalo2) {
15+
returno1.start -o2.start;
16+
}
17+
});
18+
19+
inti=0;
20+
while (i <intervals.length -1) {
21+
if (intervals[i].end >intervals[i+1].start) {
22+
returnfalse;
23+
}
24+
25+
i++;
26+
}
27+
28+
returntrue;
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
classMovingAverage {
2+
3+
/** Initialize your data structure here. */
4+
Queue<Integer>queue;
5+
intcapacity;
6+
intsum =0;
7+
publicMovingAverage(intsize) {
8+
capacity =size;
9+
queue =newLinkedList<>();
10+
}
11+
12+
publicdoublenext(intval) {
13+
if (queue.size() ==capacity) {
14+
sum -=queue.remove();
15+
}
16+
17+
queue.add(val);
18+
sum +=val;
19+
20+
doubleavg = (double)sum /queue.size();
21+
22+
returnavg;
23+
}
24+
}
25+
/**
26+
* Your MovingAverage object will be instantiated and called as such:
27+
* MovingAverage obj = new MovingAverage(size);
28+
* double param_1 = obj.next(val);
29+
*/

‎Easy/Palindrome Permutation.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
classSolution {
2+
publicstaticbooleancanPermutePalindrome(Strings) {
3+
Map<Character,Integer>map =newHashMap<>();
4+
char[]chars =s.toCharArray();
5+
6+
for (charc :chars) {
7+
map.put(c,map.getOrDefault(c,0) +1);
8+
}
9+
10+
intoddVal =0;
11+
12+
for (Map.Entry<Character,Integer>entry :map.entrySet()) {
13+
if (entry.getValue()%2 !=0) {
14+
oddVal++;
15+
}
16+
17+
if (oddVal ==2) {
18+
returnfalse;
19+
}
20+
}
21+
22+
returntrue;
23+
}
24+
}

‎Medium/Design Hit Counter.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
classHitCounter {
2+
3+
/** Initialize your data structure here. */
4+
Queue<Integer>queue;
5+
publicHitCounter() {
6+
queue =newLinkedList<>();
7+
}
8+
9+
/** Record a hit.
10+
@param timestamp - The current timestamp (in seconds granularity). */
11+
publicvoidhit(inttimestamp) {
12+
queue.add(timestamp);
13+
}
14+
15+
/** Return the number of hits in the past 5 minutes.
16+
@param timestamp - The current timestamp (in seconds granularity). */
17+
publicintgetHits(inttimestamp) {
18+
while (!queue.isEmpty() &&timestamp -queue.peek() >=300) {
19+
queue.remove();
20+
}
21+
22+
returnqueue.size();
23+
}
24+
}
25+
26+
/**
27+
* Your HitCounter object will be instantiated and called as such:
28+
* HitCounter obj = new HitCounter();
29+
* obj.hit(timestamp);
30+
* int param_2 = obj.getHits(timestamp);
31+
*/

‎Medium/Meeting Rooms II.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Definition for an interval.
3+
* public class Interval {
4+
* int start;
5+
* int end;
6+
* Interval() { start = 0; end = 0; }
7+
* Interval(int s, int e) { start = s; end = e; }
8+
* }
9+
*/
10+
classSolution {
11+
publicstaticintminMeetingRooms(Interval[]intervals) {
12+
13+
if (intervals.length ==0) {
14+
return0;
15+
}
16+
17+
int[]start =newint[intervals.length];
18+
int[]end =newint[intervals.length];
19+
20+
inti =0;
21+
22+
for (Intervalinterval :intervals) {
23+
start[i] =interval.start;
24+
end[i] =interval.end;
25+
26+
i++;
27+
}
28+
29+
Arrays.sort(start);
30+
Arrays.sort(end);
31+
32+
intslow =0;
33+
intfast =0;
34+
intcount =0;
35+
36+
while (slow <intervals.length) {
37+
if (start[slow] >=end[fast]) {
38+
count--;
39+
fast++;
40+
}
41+
42+
count++;
43+
slow++;
44+
}
45+
46+
returncount;
47+
}
48+
49+
publicstaticintminMeetingRoomsQueue(Interval[]intervals) {
50+
if (intervals.length ==0) {
51+
return0;
52+
}
53+
54+
PriorityQueue<Integer>endTimes =newPriorityQueue<>(intervals.length,newComparator<Integer>() {
55+
@Override
56+
publicintcompare(Integero1,Integero2) {
57+
returno1 -o2;
58+
}
59+
});
60+
61+
Arrays.sort(intervals,newComparator<Interval>() {
62+
@Override
63+
publicintcompare(Intervalo1,Intervalo2) {
64+
returno1.start -o2.start;
65+
}
66+
});
67+
68+
endTimes.add(intervals[0].end);
69+
70+
for (inti=1;i<intervals.length;i++) {
71+
if (intervals[i].start >=endTimes.peek()) {
72+
endTimes.poll();
73+
}
74+
75+
endTimes.add(intervals[i].end);
76+
}
77+
78+
returnendTimes.size();
79+
}
80+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp