diff --git a/solution/2100-2199/2149.Rearrange Array Elements by Sign/README.md b/solution/2100-2199/2149.Rearrange Array Elements by Sign/README.md index ef55109061bb1..f0d2d3f0ca2a1 100644 --- a/solution/2100-2199/2149.Rearrange Array Elements by Sign/README.md +++ b/solution/2100-2199/2149.Rearrange Array Elements by Sign/README.md @@ -76,7 +76,15 @@ nums 中的正整数是 [3,1,2] ,负整数是 [-2,-5,-4] 。 -### 方法一 +### 方法一:双指针 + +我们先创建一个长度为 $n$ 的数组 $\textit{ans}$,然后使用两个指针 $i$ 和 $j$ 分别指向 $\textit{ans}$ 的偶数下标和奇数下标,初始时 $i = 0$, $j = 1$。 + +遍历数组 $\textit{nums}$,如果当前元素 $x$ 为正整数,则将 $x$ 放入 $\textit{ans}[i]$,并将 $i$ 增加 $2$;否则将 $x$ 放入 $\textit{ans}[j]$,并将 $j$ 增加 $2$。 + +最后返回 $\textit{ans}$ 即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 @@ -87,12 +95,12 @@ class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: ans = [0] * len(nums) i, j = 0, 1 - for num in nums: - if num > 0: - ans[i] = num + for x in nums: + if x > 0: + ans[i] = x i += 2 else: - ans[j] = num + ans[j] = x j += 2 return ans ``` @@ -101,16 +109,15 @@ class Solution: ```java class Solution { - public int[] rearrangeArray(int[] nums) { int[] ans = new int[nums.length]; int i = 0, j = 1; - for (int num : nums) { - if (num > 0) { - ans[i] = num; + for (int x : nums) { + if (x > 0) { + ans[i] = x; i += 2; } else { - ans[j] = num; + ans[j] = x; j += 2; } } @@ -127,12 +134,12 @@ public: vector rearrangeArray(vector& nums) { vector ans(nums.size()); int i = 0, j = 1; - for (int num : nums) { - if (num > 0) { - ans[i] = num; + for (int x : nums) { + if (x > 0) { + ans[i] = x; i += 2; } else { - ans[j] = num; + ans[j] = x; j += 2; } } @@ -147,12 +154,12 @@ public: func rearrangeArray(nums []int) []int { ans := make([]int, len(nums)) i, j := 0, 1 - for _, num := range nums { - if num > 0 { - ans[i] = num + for _, x := range nums { + if x > 0 { + ans[i] = x i += 2 } else { - ans[j] = num + ans[j] = x j += 2 } } @@ -164,15 +171,14 @@ func rearrangeArray(nums []int) []int { ```ts function rearrangeArray(nums: number[]): number[] { - let ans = []; - let i = 0, - j = 1; - for (let num of nums) { - if (num > 0) { - ans[i] = num; + const ans: number[] = Array(nums.length); + let [i, j] = [0, 1]; + for (const x of nums) { + if (x > 0) { + ans[i] = x; i += 2; } else { - ans[j] = num; + ans[j] = x; j += 2; } } diff --git a/solution/2100-2199/2149.Rearrange Array Elements by Sign/README_EN.md b/solution/2100-2199/2149.Rearrange Array Elements by Sign/README_EN.md index e7b040c53704d..efaf2d931dd35 100644 --- a/solution/2100-2199/2149.Rearrange Array Elements by Sign/README_EN.md +++ b/solution/2100-2199/2149.Rearrange Array Elements by Sign/README_EN.md @@ -73,7 +73,15 @@ It is not required to do the modifications in-place. -### Solution 1 +### Solution 1: Two Pointers + +First, we create an array $\textit{ans}$ of length $n$. Then, we use two pointers $i$ and $j$ to point to the even and odd indices of $\textit{ans}$, respectively, with initial values $i = 0$, $j = 1$. + +We iterate through the array $\textit{nums}$. If the current element $x$ is a positive integer, then we place $x$ into $\textit{ans}[i]$ and increase $i$ by $2$; otherwise, we place $x$ into $\textit{ans}[j]$ and increase $j$ by $2$. + +Finally, we return $\textit{ans}$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. @@ -84,12 +92,12 @@ class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: ans = [0] * len(nums) i, j = 0, 1 - for num in nums: - if num > 0: - ans[i] = num + for x in nums: + if x > 0: + ans[i] = x i += 2 else: - ans[j] = num + ans[j] = x j += 2 return ans ``` @@ -98,16 +106,15 @@ class Solution: ```java class Solution { - public int[] rearrangeArray(int[] nums) { int[] ans = new int[nums.length]; int i = 0, j = 1; - for (int num : nums) { - if (num > 0) { - ans[i] = num; + for (int x : nums) { + if (x > 0) { + ans[i] = x; i += 2; } else { - ans[j] = num; + ans[j] = x; j += 2; } } @@ -124,12 +131,12 @@ public: vector rearrangeArray(vector& nums) { vector ans(nums.size()); int i = 0, j = 1; - for (int num : nums) { - if (num > 0) { - ans[i] = num; + for (int x : nums) { + if (x > 0) { + ans[i] = x; i += 2; } else { - ans[j] = num; + ans[j] = x; j += 2; } } @@ -144,12 +151,12 @@ public: func rearrangeArray(nums []int) []int { ans := make([]int, len(nums)) i, j := 0, 1 - for _, num := range nums { - if num > 0 { - ans[i] = num + for _, x := range nums { + if x > 0 { + ans[i] = x i += 2 } else { - ans[j] = num + ans[j] = x j += 2 } } @@ -161,15 +168,14 @@ func rearrangeArray(nums []int) []int { ```ts function rearrangeArray(nums: number[]): number[] { - let ans = []; - let i = 0, - j = 1; - for (let num of nums) { - if (num > 0) { - ans[i] = num; + const ans: number[] = Array(nums.length); + let [i, j] = [0, 1]; + for (const x of nums) { + if (x > 0) { + ans[i] = x; i += 2; } else { - ans[j] = num; + ans[j] = x; j += 2; } } diff --git a/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.cpp b/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.cpp index 3cf5a6c6805d4..ef006cab32aea 100644 --- a/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.cpp +++ b/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.cpp @@ -3,12 +3,12 @@ class Solution { vector rearrangeArray(vector& nums) { vector ans(nums.size()); int i = 0, j = 1; - for (int num : nums) { - if (num > 0) { - ans[i] = num; + for (int x : nums) { + if (x > 0) { + ans[i] = x; i += 2; } else { - ans[j] = num; + ans[j] = x; j += 2; } } diff --git a/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.go b/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.go index 21dc6cb2ea3a3..69a4188de2467 100644 --- a/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.go +++ b/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.go @@ -1,12 +1,12 @@ func rearrangeArray(nums []int) []int { ans := make([]int, len(nums)) i, j := 0, 1 - for _, num := range nums { - if num > 0 { - ans[i] = num + for _, x := range nums { + if x > 0 { + ans[i] = x i += 2 } else { - ans[j] = num + ans[j] = x j += 2 } } diff --git a/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.java b/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.java index e933560968777..2da89d9ca372f 100644 --- a/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.java +++ b/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.java @@ -1,14 +1,13 @@ class Solution { - public int[] rearrangeArray(int[] nums) { int[] ans = new int[nums.length]; int i = 0, j = 1; - for (int num : nums) { - if (num > 0) { - ans[i] = num; + for (int x : nums) { + if (x > 0) { + ans[i] = x; i += 2; } else { - ans[j] = num; + ans[j] = x; j += 2; } } diff --git a/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.py b/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.py index 54a7aff4a005c..7ec9860e76f4d 100644 --- a/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.py +++ b/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.py @@ -2,11 +2,11 @@ class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: ans = [0] * len(nums) i, j = 0, 1 - for num in nums: - if num > 0: - ans[i] = num + for x in nums: + if x > 0: + ans[i] = x i += 2 else: - ans[j] = num + ans[j] = x j += 2 return ans diff --git a/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.ts b/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.ts index b8e40aee94d33..c9f9c64e12ac8 100644 --- a/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.ts +++ b/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.ts @@ -1,13 +1,12 @@ function rearrangeArray(nums: number[]): number[] { - let ans = []; - let i = 0, - j = 1; - for (let num of nums) { - if (num > 0) { - ans[i] = num; + const ans: number[] = Array(nums.length); + let [i, j] = [0, 1]; + for (const x of nums) { + if (x > 0) { + ans[i] = x; i += 2; } else { - ans[j] = num; + ans[j] = x; j += 2; } } diff --git a/solution/2100-2199/2150.Find All Lonely Numbers in the Array/README.md b/solution/2100-2199/2150.Find All Lonely Numbers in the Array/README.md index 30ca9fd942ca4..6a8a3a4ac5255 100644 --- a/solution/2100-2199/2150.Find All Lonely Numbers in the Array/README.md +++ b/solution/2100-2199/2150.Find All Lonely Numbers in the Array/README.md @@ -64,7 +64,13 @@ tags: -### 方法一 +### 方法一:哈希表 + +我们用一个哈希表 $\textit{cnt}$ 记录每个数字出现的次数,然后遍历哈希表,对于每个数字及其出现次数 $(x, v)$,如果 $v = 1$ 且 $\textit{cnt}[x - 1] = 0$ 且 $\textit{cnt}[x + 1] = 0$,则 $x$ 是一个孤独数字,将其加入答案数组中。 + +遍历结束后,返回答案数组即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 @@ -73,30 +79,28 @@ tags: ```python class Solution: def findLonely(self, nums: List[int]) -> List[int]: - counter = Counter(nums) - ans = [] - for num, cnt in counter.items(): - if cnt == 1 and counter[num - 1] == 0 and counter[num + 1] == 0: - ans.append(num) - return ans + cnt = Counter(nums) + return [ + x for x, v in cnt.items() if v == 1 and cnt[x - 1] == 0 and cnt[x + 1] == 0 + ] ``` #### Java ```java class Solution { - public List findLonely(int[] nums) { - Map counter = new HashMap<>(); - for (int num : nums) { - counter.put(num, counter.getOrDefault(num, 0) + 1); + Map cnt = new HashMap<>(); + for (int x : nums) { + cnt.merge(x, 1, Integer::sum); } List ans = new ArrayList<>(); - counter.forEach((k, v) -> { - if (v == 1 && !counter.containsKey(k - 1) && !counter.containsKey(k + 1)) { - ans.add(k); + for (var e : cnt.entrySet()) { + int x = e.getKey(), v = e.getValue(); + if (v == 1 && !cnt.containsKey(x - 1) && !cnt.containsKey(x + 1)) { + ans.add(x); } - }); + } return ans; } } @@ -108,12 +112,15 @@ class Solution { class Solution { public: vector findLonely(vector& nums) { - unordered_map counter; - for (int num : nums) ++counter[num]; + unordered_map cnt; + for (int x : nums) { + cnt[x]++; + } vector ans; - for (auto& e : counter) { - int k = e.first, v = e.second; - if (v == 1 && !counter.count(k - 1) && !counter.count(k + 1)) ans.push_back(k); + for (auto& [x, v] : cnt) { + if (v == 1 && !cnt.contains(x - 1) && !cnt.contains(x + 1)) { + ans.push_back(x); + } } return ans; } @@ -123,18 +130,17 @@ public: #### Go ```go -func findLonely(nums []int) []int { - counter := make(map[int]int) - for _, num := range nums { - counter[num]++ +func findLonely(nums []int) (ans []int) { + cnt := map[int]int{} + for _, x := range nums { + cnt[x]++ } - var ans []int - for k, v := range counter { - if v == 1 && counter[k-1] == 0 && counter[k+1] == 0 { - ans = append(ans, k) + for x, v := range cnt { + if v == 1 && cnt[x-1] == 0 && cnt[x+1] == 0 { + ans = append(ans, x) } } - return ans + return } ``` @@ -142,14 +148,14 @@ func findLonely(nums []int) []int { ```ts function findLonely(nums: number[]): number[] { - let hashMap: Map = new Map(); - for (let num of nums) { - hashMap.set(num, (hashMap.get(num) || 0) + 1); + const cnt: Map = new Map(); + for (const x of nums) { + cnt.set(x, (cnt.get(x) || 0) + 1); } - let ans: Array = []; - for (let [num, count] of hashMap.entries()) { - if (count == 1 && !hashMap.get(num - 1) && !hashMap.get(num + 1)) { - ans.push(num); + const ans: number[] = []; + for (const [x, v] of cnt) { + if (v === 1 && !cnt.has(x - 1) && !cnt.has(x + 1)) { + ans.push(x); } } return ans; diff --git a/solution/2100-2199/2150.Find All Lonely Numbers in the Array/README_EN.md b/solution/2100-2199/2150.Find All Lonely Numbers in the Array/README_EN.md index bc490d0563c97..8905b9d5fb939 100644 --- a/solution/2100-2199/2150.Find All Lonely Numbers in the Array/README_EN.md +++ b/solution/2100-2199/2150.Find All Lonely Numbers in the Array/README_EN.md @@ -65,7 +65,13 @@ Note that [5, 1] may also be returned. -### Solution 1 +### Solution 1: Hash Table + +We use a hash table $\textit{cnt}$ to record the occurrence count of each number. Then, we iterate through the hash table. For each number and its occurrence count $(x, v)$, if $v = 1$ and $\textit{cnt}[x - 1] = 0$ and $\textit{cnt}[x + 1] = 0$, then $x$ is a lonely number, and we add it to the answer array. + +After finishing the iteration, we return the answer array. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. @@ -74,30 +80,28 @@ Note that [5, 1] may also be returned. ```python class Solution: def findLonely(self, nums: List[int]) -> List[int]: - counter = Counter(nums) - ans = [] - for num, cnt in counter.items(): - if cnt == 1 and counter[num - 1] == 0 and counter[num + 1] == 0: - ans.append(num) - return ans + cnt = Counter(nums) + return [ + x for x, v in cnt.items() if v == 1 and cnt[x - 1] == 0 and cnt[x + 1] == 0 + ] ``` #### Java ```java class Solution { - public List findLonely(int[] nums) { - Map counter = new HashMap<>(); - for (int num : nums) { - counter.put(num, counter.getOrDefault(num, 0) + 1); + Map cnt = new HashMap<>(); + for (int x : nums) { + cnt.merge(x, 1, Integer::sum); } List ans = new ArrayList<>(); - counter.forEach((k, v) -> { - if (v == 1 && !counter.containsKey(k - 1) && !counter.containsKey(k + 1)) { - ans.add(k); + for (var e : cnt.entrySet()) { + int x = e.getKey(), v = e.getValue(); + if (v == 1 && !cnt.containsKey(x - 1) && !cnt.containsKey(x + 1)) { + ans.add(x); } - }); + } return ans; } } @@ -109,12 +113,15 @@ class Solution { class Solution { public: vector findLonely(vector& nums) { - unordered_map counter; - for (int num : nums) ++counter[num]; + unordered_map cnt; + for (int x : nums) { + cnt[x]++; + } vector ans; - for (auto& e : counter) { - int k = e.first, v = e.second; - if (v == 1 && !counter.count(k - 1) && !counter.count(k + 1)) ans.push_back(k); + for (auto& [x, v] : cnt) { + if (v == 1 && !cnt.contains(x - 1) && !cnt.contains(x + 1)) { + ans.push_back(x); + } } return ans; } @@ -124,18 +131,17 @@ public: #### Go ```go -func findLonely(nums []int) []int { - counter := make(map[int]int) - for _, num := range nums { - counter[num]++ +func findLonely(nums []int) (ans []int) { + cnt := map[int]int{} + for _, x := range nums { + cnt[x]++ } - var ans []int - for k, v := range counter { - if v == 1 && counter[k-1] == 0 && counter[k+1] == 0 { - ans = append(ans, k) + for x, v := range cnt { + if v == 1 && cnt[x-1] == 0 && cnt[x+1] == 0 { + ans = append(ans, x) } } - return ans + return } ``` @@ -143,14 +149,14 @@ func findLonely(nums []int) []int { ```ts function findLonely(nums: number[]): number[] { - let hashMap: Map = new Map(); - for (let num of nums) { - hashMap.set(num, (hashMap.get(num) || 0) + 1); + const cnt: Map = new Map(); + for (const x of nums) { + cnt.set(x, (cnt.get(x) || 0) + 1); } - let ans: Array = []; - for (let [num, count] of hashMap.entries()) { - if (count == 1 && !hashMap.get(num - 1) && !hashMap.get(num + 1)) { - ans.push(num); + const ans: number[] = []; + for (const [x, v] of cnt) { + if (v === 1 && !cnt.has(x - 1) && !cnt.has(x + 1)) { + ans.push(x); } } return ans; diff --git a/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.cpp b/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.cpp index 4cdb56deec98d..49e8ae3b1871c 100644 --- a/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.cpp +++ b/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.cpp @@ -1,12 +1,15 @@ class Solution { public: vector findLonely(vector& nums) { - unordered_map counter; - for (int num : nums) ++counter[num]; + unordered_map cnt; + for (int x : nums) { + cnt[x]++; + } vector ans; - for (auto& e : counter) { - int k = e.first, v = e.second; - if (v == 1 && !counter.count(k - 1) && !counter.count(k + 1)) ans.push_back(k); + for (auto& [x, v] : cnt) { + if (v == 1 && !cnt.contains(x - 1) && !cnt.contains(x + 1)) { + ans.push_back(x); + } } return ans; } diff --git a/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.go b/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.go index d8f651526bc11..380a25d4b8284 100644 --- a/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.go +++ b/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.go @@ -1,13 +1,12 @@ -func findLonely(nums []int) []int { - counter := make(map[int]int) - for _, num := range nums { - counter[num]++ +func findLonely(nums []int) (ans []int) { + cnt := map[int]int{} + for _, x := range nums { + cnt[x]++ } - var ans []int - for k, v := range counter { - if v == 1 && counter[k-1] == 0 && counter[k+1] == 0 { - ans = append(ans, k) + for x, v := range cnt { + if v == 1 && cnt[x-1] == 0 && cnt[x+1] == 0 { + ans = append(ans, x) } } - return ans + return } \ No newline at end of file diff --git a/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.java b/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.java index 6ac1cd88650ce..34a22b0321dad 100644 --- a/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.java +++ b/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.java @@ -1,16 +1,16 @@ class Solution { - public List findLonely(int[] nums) { - Map counter = new HashMap<>(); - for (int num : nums) { - counter.put(num, counter.getOrDefault(num, 0) + 1); + Map cnt = new HashMap<>(); + for (int x : nums) { + cnt.merge(x, 1, Integer::sum); } List ans = new ArrayList<>(); - counter.forEach((k, v) -> { - if (v == 1 && !counter.containsKey(k - 1) && !counter.containsKey(k + 1)) { - ans.add(k); + for (var e : cnt.entrySet()) { + int x = e.getKey(), v = e.getValue(); + if (v == 1 && !cnt.containsKey(x - 1) && !cnt.containsKey(x + 1)) { + ans.add(x); } - }); + } return ans; } } \ No newline at end of file diff --git a/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.py b/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.py index 6bc384044f1ac..945eea586c78a 100644 --- a/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.py +++ b/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.py @@ -1,8 +1,6 @@ class Solution: def findLonely(self, nums: List[int]) -> List[int]: - counter = Counter(nums) - ans = [] - for num, cnt in counter.items(): - if cnt == 1 and counter[num - 1] == 0 and counter[num + 1] == 0: - ans.append(num) - return ans + cnt = Counter(nums) + return [ + x for x, v in cnt.items() if v == 1 and cnt[x - 1] == 0 and cnt[x + 1] == 0 + ] diff --git a/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.ts b/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.ts index 44c85163005a1..561ea0f3cfb77 100644 --- a/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.ts +++ b/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.ts @@ -1,12 +1,12 @@ function findLonely(nums: number[]): number[] { - let hashMap: Map = new Map(); - for (let num of nums) { - hashMap.set(num, (hashMap.get(num) || 0) + 1); + const cnt: Map = new Map(); + for (const x of nums) { + cnt.set(x, (cnt.get(x) || 0) + 1); } - let ans: Array = []; - for (let [num, count] of hashMap.entries()) { - if (count == 1 && !hashMap.get(num - 1) && !hashMap.get(num + 1)) { - ans.push(num); + const ans: number[] = []; + for (const [x, v] of cnt) { + if (v === 1 && !cnt.has(x - 1) && !cnt.has(x + 1)) { + ans.push(x); } } return ans;