Skip to content

Commit 094d300

Browse files
add 1752
1 parent 1649f1a commit 094d300

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1752|[Check if Array Is Sorted and Rotated](https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1752.java) ||Easy|Array|
1112
|1750|[Minimum Length of String After Deleting Similar Ends](https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1750.java) ||Medium|Two Pointers|
1213
|1749|[Maximum Absolute Sum of Any Subarray](https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1749.java) ||Medium|Greedy|
1314
|1748|[Sum of Unique Elements](https://leetcode.com/problems/sum-of-unique-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1748.java) ||Easy|Array, HashTable|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
public class _1752 {
6+
public static class Solution1 {
7+
public boolean check(int[] nums) {
8+
int[] copy = Arrays.copyOf(nums, nums.length);
9+
Arrays.sort(copy);
10+
for (int i = 1; i <= nums.length; i++) {
11+
int[] rotated = rotate(nums, i);
12+
if (Arrays.equals(rotated, copy)) {
13+
return true;
14+
}
15+
}
16+
return false;
17+
}
18+
19+
private int[] rotate(int[] nums, int start) {
20+
int[] rotated = new int[nums.length];
21+
int j = 0;
22+
for (int i = start; i < nums.length; i++, j++) {
23+
rotated[j] = nums[i];
24+
}
25+
for (int i = 0; i < start; i++) {
26+
rotated[j++] = nums[i];
27+
}
28+
return rotated;
29+
}
30+
}
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1752;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1752Test {
10+
private static _1752.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1752.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(true, solution1.check(new int[]{3, 4, 5, 1, 2}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(false, solution1.check(new int[]{2, 1, 3, 4}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(true, solution1.check(new int[]{1, 2, 3}));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(true, solution1.check(new int[]{1, 1, 1}));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(true, solution1.check(new int[]{2, 1}));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)