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

Commitd333b34

Browse files
committed
Added 4 solutions
1 parent9ebe6ce commitd333b34

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed

‎Easy/Fair Candy Swap.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
classSolution {
2+
publicint[]fairCandySwap(int[]A,int[]B) {
3+
intsumA =0;
4+
intsumB =0;
5+
6+
Set<Integer>set =newHashSet<>();
7+
8+
for (intnum :A) {
9+
sumA +=num;
10+
set.add(num);
11+
}
12+
13+
for (intnum :B) {
14+
sumB +=num;
15+
}
16+
17+
intdiff = (sumA -sumB)/2;
18+
19+
int[]ans =newint[2];
20+
for (intnum :B) {
21+
if (set.contains(num +diff)) {
22+
ans[0] =num +diff;
23+
ans[1] =num;
24+
break;
25+
}
26+
}
27+
28+
returnans;
29+
}
30+
}

‎Easy/Positions of Large Groups.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
classSolution {
2+
publicList<List<Integer>>largeGroupPositions(StringS) {
3+
List<List<Integer>>positions =newArrayList<>();
4+
5+
inti =1;
6+
intcount =1;
7+
intstart =0;
8+
9+
while (i <S.length()) {
10+
if (S.charAt(i) ==S.charAt(i-1)) {
11+
count++;
12+
}
13+
else {
14+
if (count >=3) {
15+
List<Integer>temp =Arrays.asList(start,i-1);
16+
positions.add(temp);
17+
}
18+
19+
start =i;
20+
count =1;
21+
}
22+
23+
i++;
24+
}
25+
26+
if (count >=3) {
27+
positions.add(Arrays.asList(start,i-1));
28+
}
29+
30+
returnpositions;
31+
}
32+
}

‎Easy/Transpose Matrix.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
classSolution {
2+
publicint[][]transpose(int[][]A) {
3+
introws =A.length;
4+
intcolumns =A[0].length;
5+
6+
int[][]newArr =newint[columns][rows];
7+
8+
for (inti=0;i<columns;i++) {
9+
for (intj=0;j<rows;j++) {
10+
newArr[i][j] =A[j][i];
11+
}
12+
}
13+
14+
returnnewArr;
15+
}
16+
}

‎Medium/Design Circular Queue.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
classMyCircularQueue {
2+
3+
/** Initialize your data structure here. Set the size of the queue to be k. */
4+
Nodehead;
5+
Nodecurr;
6+
intcapacity;
7+
intcount =0;
8+
9+
publicMyCircularQueue(intk) {
10+
head =newNode(-1);
11+
curr =head;
12+
capacity =k;
13+
}
14+
15+
/** Insert an element into the circular queue. Return true if the operation is successful. */
16+
publicbooleanenQueue(intvalue) {
17+
if (count ==capacity) {
18+
returnfalse;
19+
}
20+
21+
curr.next =newNode(value);
22+
curr =curr.next;
23+
count++;
24+
25+
returntrue;
26+
}
27+
28+
/** Delete an element from the circular queue. Return true if the operation is successful. */
29+
publicbooleandeQueue() {
30+
if (count <=0) {
31+
returnfalse;
32+
}
33+
34+
if (head.next ==curr) {
35+
curr =head;
36+
head.next =null;
37+
}
38+
else {
39+
head.next =head.next.next;
40+
}
41+
42+
count--;
43+
44+
returntrue;
45+
}
46+
47+
/** Get the front item from the queue. */
48+
publicintFront() {
49+
if (count ==0) {
50+
return -1;
51+
}
52+
53+
returnhead.next.value;
54+
}
55+
56+
/** Get the last item from the queue. */
57+
publicintRear() {
58+
if (count ==0) {
59+
return -1;
60+
}
61+
62+
returncurr.value;
63+
}
64+
65+
/** Checks whether the circular queue is empty or not. */
66+
publicbooleanisEmpty() {
67+
returncount ==0;
68+
}
69+
70+
/** Checks whether the circular queue is full or not. */
71+
publicbooleanisFull() {
72+
returncount ==capacity;
73+
}
74+
75+
privatevoidprintList(Nodenode) {
76+
while (node !=null) {
77+
System.out.print(node.value +" ");
78+
}
79+
80+
System.out.println();
81+
}
82+
83+
classNode {
84+
intvalue;
85+
Nodenext;
86+
Node(intvalue) {
87+
this.value =value;
88+
}
89+
}
90+
}
91+
92+
/**
93+
* Your MyCircularQueue object will be instantiated and called as such:
94+
* MyCircularQueue obj = new MyCircularQueue(k);
95+
* boolean param_1 = obj.enQueue(value);
96+
* boolean param_2 = obj.deQueue();
97+
* int param_3 = obj.Front();
98+
* int param_4 = obj.Rear();
99+
* boolean param_5 = obj.isEmpty();
100+
* boolean param_6 = obj.isFull();
101+
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp