diff --git a/solution/2900-2999/2956.Find Common Elements Between Two Arrays/README.md b/solution/2900-2999/2956.Find Common Elements Between Two Arrays/README.md index 435235fcb01c4..ce60e59f7b801 100644 --- a/solution/2900-2999/2956.Find Common Elements Between Two Arrays/README.md +++ b/solution/2900-2999/2956.Find Common Elements Between Two Arrays/README.md @@ -52,6 +52,18 @@ +**方法一:哈希表或数组** + +我们可以用两个哈希表或数组 $s1$ 和 $s2$ 分别记录两个数组中出现的元素。 + +接下来,我们创建一个长度为 $2$ 的数组 $ans$,其中 $ans[0]$ 表示 $nums1$ 中出现在 $s2$ 中的元素个数,$ans[1]$ 表示 $nums2$ 中出现在 $s1$ 中的元素个数。 + +然后,我们遍历数组 $nums1$ 中的每个元素 $x$,如果 $x$ 在 $s2$ 中出现过,则将 $ans[0]$ 加一。接着,我们遍历数组 $nums2$ 中的每个元素 $x$,如果 $x$ 在 $s1$ 中出现过,则将 $ans[1]$ 加一。 + +最后,我们返回数组 $ans$ 即可。 + +时间复杂度 $O(n + m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是数组 $nums1$ 和 $nums2$ 的长度。 + ### **Python3** @@ -59,7 +71,10 @@ ```python - +class Solution: + def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]: + s1, s2 = set(nums1), set(nums2) + return [sum(x in s2 for x in nums1), sum(x in s1 for x in nums2)] ``` ### **Java** @@ -67,19 +82,98 @@ ```java - +class Solution { + public int[] findIntersectionValues(int[] nums1, int[] nums2) { + int[] s1 = new int[101]; + int[] s2 = new int[101]; + for (int x : nums1) { + s1[x] = 1; + } + for (int x : nums2) { + s2[x] = 1; + } + int[] ans = new int[2]; + for (int x : nums1) { + ans[0] += s2[x]; + } + for (int x : nums2) { + ans[1] += s1[x]; + } + return ans; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + vector findIntersectionValues(vector& nums1, vector& nums2) { + int s1[101]{}; + int s2[101]{}; + for (int& x : nums1) { + s1[x] = 1; + } + for (int& x : nums2) { + s2[x] = 1; + } + vector ans(2); + for (int& x : nums1) { + ans[0] += s2[x]; + } + for (int& x : nums2) { + ans[1] += s1[x]; + } + return ans; + } +}; ``` ### **Go** ```go +func findIntersectionValues(nums1 []int, nums2 []int) []int { + s1 := [101]int{} + s2 := [101]int{} + for _, x := range nums1 { + s1[x] = 1 + } + for _, x := range nums2 { + s2[x] = 1 + } + ans := make([]int, 2) + for _, x := range nums1 { + ans[0] += s2[x] + } + for _, x := range nums2 { + ans[1] += s1[x] + } + return ans +} +``` +### **TypeScript** + +```ts +function findIntersectionValues(nums1: number[], nums2: number[]): number[] { + const s1: number[] = Array(101).fill(0); + const s2: number[] = Array(101).fill(0); + for (const x of nums1) { + s1[x] = 1; + } + for (const x of nums2) { + s2[x] = 1; + } + const ans: number[] = Array(2).fill(0); + for (const x of nums1) { + ans[0] += s2[x]; + } + for (const x of nums2) { + ans[1] += s1[x]; + } + return ans; +} ``` ### **...** diff --git a/solution/2900-2999/2956.Find Common Elements Between Two Arrays/README_EN.md b/solution/2900-2999/2956.Find Common Elements Between Two Arrays/README_EN.md index b11c5c9423731..424bbbcecc8af 100644 --- a/solution/2900-2999/2956.Find Common Elements Between Two Arrays/README_EN.md +++ b/solution/2900-2999/2956.Find Common Elements Between Two Arrays/README_EN.md @@ -46,30 +46,124 @@ ## Solutions +**Solution 1: Hash Table or Array** + +We can use two hash tables or arrays $s1$ and $s2$ to record the elements that appear in the two arrays respectively. + +Next, we create an array $ans$ of length $2$, where $ans[0]$ represents the number of elements in $nums1$ that appear in $s2$, and $ans[1]$ represents the number of elements in $nums2$ that appear in $s1$. + +Then, we traverse each element $x$ in the array $nums1$. If $x$ has appeared in $s2$, we increment $ans[0]$. After that, we traverse each element $x$ in the array $nums2$. If $x$ has appeared in $s1$, we increment $ans[1]$. + +Finally, we return the array $ans$. + +The time complexity is $O(n + m)$, and the space complexity is $O(n + m)$. Here, $n$ and $m$ are the lengths of the arrays $nums1$ and $nums2$ respectively. + ### **Python3** ```python - +class Solution: + def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]: + s1, s2 = set(nums1), set(nums2) + return [sum(x in s2 for x in nums1), sum(x in s1 for x in nums2)] ``` ### **Java** ```java - +class Solution { + public int[] findIntersectionValues(int[] nums1, int[] nums2) { + int[] s1 = new int[101]; + int[] s2 = new int[101]; + for (int x : nums1) { + s1[x] = 1; + } + for (int x : nums2) { + s2[x] = 1; + } + int[] ans = new int[2]; + for (int x : nums1) { + ans[0] += s2[x]; + } + for (int x : nums2) { + ans[1] += s1[x]; + } + return ans; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + vector findIntersectionValues(vector& nums1, vector& nums2) { + int s1[101]{}; + int s2[101]{}; + for (int& x : nums1) { + s1[x] = 1; + } + for (int& x : nums2) { + s2[x] = 1; + } + vector ans(2); + for (int& x : nums1) { + ans[0] += s2[x]; + } + for (int& x : nums2) { + ans[1] += s1[x]; + } + return ans; + } +}; ``` ### **Go** ```go +func findIntersectionValues(nums1 []int, nums2 []int) []int { + s1 := [101]int{} + s2 := [101]int{} + for _, x := range nums1 { + s1[x] = 1 + } + for _, x := range nums2 { + s2[x] = 1 + } + ans := make([]int, 2) + for _, x := range nums1 { + ans[0] += s2[x] + } + for _, x := range nums2 { + ans[1] += s1[x] + } + return ans +} +``` +### **TypeScript** + +```ts +function findIntersectionValues(nums1: number[], nums2: number[]): number[] { + const s1: number[] = Array(101).fill(0); + const s2: number[] = Array(101).fill(0); + for (const x of nums1) { + s1[x] = 1; + } + for (const x of nums2) { + s2[x] = 1; + } + const ans: number[] = Array(2).fill(0); + for (const x of nums1) { + ans[0] += s2[x]; + } + for (const x of nums2) { + ans[1] += s1[x]; + } + return ans; +} ``` ### **...** diff --git a/solution/2900-2999/2956.Find Common Elements Between Two Arrays/Solution.cpp b/solution/2900-2999/2956.Find Common Elements Between Two Arrays/Solution.cpp new file mode 100644 index 0000000000000..0c9f526245a85 --- /dev/null +++ b/solution/2900-2999/2956.Find Common Elements Between Two Arrays/Solution.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + vector findIntersectionValues(vector& nums1, vector& nums2) { + int s1[101]{}; + int s2[101]{}; + for (int& x : nums1) { + s1[x] = 1; + } + for (int& x : nums2) { + s2[x] = 1; + } + vector ans(2); + for (int& x : nums1) { + ans[0] += s2[x]; + } + for (int& x : nums2) { + ans[1] += s1[x]; + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/2900-2999/2956.Find Common Elements Between Two Arrays/Solution.go b/solution/2900-2999/2956.Find Common Elements Between Two Arrays/Solution.go new file mode 100644 index 0000000000000..71007d0c65eb2 --- /dev/null +++ b/solution/2900-2999/2956.Find Common Elements Between Two Arrays/Solution.go @@ -0,0 +1,18 @@ +func findIntersectionValues(nums1 []int, nums2 []int) []int { + s1 := [101]int{} + s2 := [101]int{} + for _, x := range nums1 { + s1[x] = 1 + } + for _, x := range nums2 { + s2[x] = 1 + } + ans := make([]int, 2) + for _, x := range nums1 { + ans[0] += s2[x] + } + for _, x := range nums2 { + ans[1] += s1[x] + } + return ans +} \ No newline at end of file diff --git a/solution/2900-2999/2956.Find Common Elements Between Two Arrays/Solution.java b/solution/2900-2999/2956.Find Common Elements Between Two Arrays/Solution.java new file mode 100644 index 0000000000000..86c3a54c5e51c --- /dev/null +++ b/solution/2900-2999/2956.Find Common Elements Between Two Arrays/Solution.java @@ -0,0 +1,20 @@ +class Solution { + public int[] findIntersectionValues(int[] nums1, int[] nums2) { + int[] s1 = new int[101]; + int[] s2 = new int[101]; + for (int x : nums1) { + s1[x] = 1; + } + for (int x : nums2) { + s2[x] = 1; + } + int[] ans = new int[2]; + for (int x : nums1) { + ans[0] += s2[x]; + } + for (int x : nums2) { + ans[1] += s1[x]; + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2900-2999/2956.Find Common Elements Between Two Arrays/Solution.py b/solution/2900-2999/2956.Find Common Elements Between Two Arrays/Solution.py new file mode 100644 index 0000000000000..e446fdf23796f --- /dev/null +++ b/solution/2900-2999/2956.Find Common Elements Between Two Arrays/Solution.py @@ -0,0 +1,4 @@ +class Solution: + def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]: + s1, s2 = set(nums1), set(nums2) + return [sum(x in s2 for x in nums1), sum(x in s1 for x in nums2)] diff --git a/solution/2900-2999/2956.Find Common Elements Between Two Arrays/Solution.ts b/solution/2900-2999/2956.Find Common Elements Between Two Arrays/Solution.ts new file mode 100644 index 0000000000000..718f1828d8a6a --- /dev/null +++ b/solution/2900-2999/2956.Find Common Elements Between Two Arrays/Solution.ts @@ -0,0 +1,18 @@ +function findIntersectionValues(nums1: number[], nums2: number[]): number[] { + const s1: number[] = Array(101).fill(0); + const s2: number[] = Array(101).fill(0); + for (const x of nums1) { + s1[x] = 1; + } + for (const x of nums2) { + s2[x] = 1; + } + const ans: number[] = Array(2).fill(0); + for (const x of nums1) { + ans[0] += s2[x]; + } + for (const x of nums2) { + ans[1] += s1[x]; + } + return ans; +} diff --git a/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/README.md b/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/README.md index 9accec35bc8dd..8abcbec14848b 100644 --- a/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/README.md +++ b/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/README.md @@ -55,6 +55,14 @@ +**方法一:贪心** + +我们从下标 $1$ 开始遍历字符串 $word$,如果 $word[i]$ 和 $word[i - 1]$ 相邻近似相等,那么我们就贪心地将 $word[i]$ 替换成一个与 $word[i - 1]$ 和 $word[i + 1]$ 都不相等的字符(可以不执行替换操作,记录操作次数即可)。然后,我们跳过 $word[i + 1]$,继续遍历字符串 $word$。 + +最后,我们返回记录的操作次数。 + +时间复杂度 $O(n)$,其中 $n$ 是字符串 $word$ 的长度。空间复杂度 $O(1)$。 + ### **Python3** @@ -62,7 +70,17 @@ ```python - +class Solution: + def removeAlmostEqualCharacters(self, word: str) -> int: + ans = 0 + i, n = 1, len(word) + while i < n: + if abs(ord(word[i]) - ord(word[i - 1])) < 2: + ans += 1 + i += 2 + else: + i += 1 + return ans ``` ### **Java** @@ -70,19 +88,72 @@ ```java - +class Solution { + public int removeAlmostEqualCharacters(String word) { + int ans = 0, n = word.length(); + for (int i = 1; i < n; ++i) { + if (Math.abs(word.charAt(i) - word.charAt(i - 1)) < 2) { + ++ans; + ++i; + } + } + return ans; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int removeAlmostEqualCharacters(string word) { + int ans = 0, n = word.size(); + for (int i = 1; i < n; ++i) { + if (abs(word[i] - word[i - 1]) < 2) { + ++ans; + ++i; + } + } + return ans; + } +}; ``` ### **Go** ```go +func removeAlmostEqualCharacters(word string) (ans int) { + for i := 1; i < len(word); i++ { + if abs(int(word[i])-int(word[i-1])) < 2 { + ans++ + i++ + } + } + return +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} +``` +### **TypeScript** + +```ts +function removeAlmostEqualCharacters(word: string): number { + let ans = 0; + for (let i = 1; i < word.length; ++i) { + if (Math.abs(word.charCodeAt(i) - word.charCodeAt(i - 1)) < 2) { + ++ans; + ++i; + } + } + return ans; +} ``` ### **...** diff --git a/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/README_EN.md b/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/README_EN.md index 4dd3436509c40..47b303cd91cb1 100644 --- a/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/README_EN.md +++ b/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/README_EN.md @@ -49,30 +49,101 @@ It can be shown that the minimum number of operations needed to remove all adjac ## Solutions +**Solution 1: Greedy** + +We start traversing the string `word` from index $1$. If `word[i]` and `word[i - 1]` are approximately equal, we greedily replace `word[i]` with a character that is not equal to both `word[i - 1]` and `word[i + 1]` (we can choose not to perform the replacement operation, just record the number of operations). Then, we skip `word[i + 1]` and continue to traverse the string `word`. + +Finally, we return the recorded number of operations. + +The time complexity is $O(n)$, where $n$ is the length of the string `word`. The space complexity is $O(1)$. + ### **Python3** ```python - +class Solution: + def removeAlmostEqualCharacters(self, word: str) -> int: + ans = 0 + i, n = 1, len(word) + while i < n: + if abs(ord(word[i]) - ord(word[i - 1])) < 2: + ans += 1 + i += 2 + else: + i += 1 + return ans ``` ### **Java** ```java - +class Solution { + public int removeAlmostEqualCharacters(String word) { + int ans = 0, n = word.length(); + for (int i = 1; i < n; ++i) { + if (Math.abs(word.charAt(i) - word.charAt(i - 1)) < 2) { + ++ans; + ++i; + } + } + return ans; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int removeAlmostEqualCharacters(string word) { + int ans = 0, n = word.size(); + for (int i = 1; i < n; ++i) { + if (abs(word[i] - word[i - 1]) < 2) { + ++ans; + ++i; + } + } + return ans; + } +}; ``` ### **Go** ```go +func removeAlmostEqualCharacters(word string) (ans int) { + for i := 1; i < len(word); i++ { + if abs(int(word[i])-int(word[i-1])) < 2 { + ans++ + i++ + } + } + return +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} +``` +### **TypeScript** + +```ts +function removeAlmostEqualCharacters(word: string): number { + let ans = 0; + for (let i = 1; i < word.length; ++i) { + if (Math.abs(word.charCodeAt(i) - word.charCodeAt(i - 1)) < 2) { + ++ans; + ++i; + } + } + return ans; +} ``` ### **...** diff --git a/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/Solution.cpp b/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/Solution.cpp new file mode 100644 index 0000000000000..4aa45ad1dc0fa --- /dev/null +++ b/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/Solution.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + int removeAlmostEqualCharacters(string word) { + int ans = 0, n = word.size(); + for (int i = 1; i < n; ++i) { + if (abs(word[i] - word[i - 1]) < 2) { + ++ans; + ++i; + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/Solution.go b/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/Solution.go new file mode 100644 index 0000000000000..c7dd891acc40f --- /dev/null +++ b/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/Solution.go @@ -0,0 +1,16 @@ +func removeAlmostEqualCharacters(word string) (ans int) { + for i := 1; i < len(word); i++ { + if abs(int(word[i])-int(word[i-1])) < 2 { + ans++ + i++ + } + } + return +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} \ No newline at end of file diff --git a/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/Solution.java b/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/Solution.java new file mode 100644 index 0000000000000..a5d82695131af --- /dev/null +++ b/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/Solution.java @@ -0,0 +1,12 @@ +class Solution { + public int removeAlmostEqualCharacters(String word) { + int ans = 0, n = word.length(); + for (int i = 1; i < n; ++i) { + if (Math.abs(word.charAt(i) - word.charAt(i - 1)) < 2) { + ++ans; + ++i; + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/Solution.py b/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/Solution.py new file mode 100644 index 0000000000000..b159341acb570 --- /dev/null +++ b/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/Solution.py @@ -0,0 +1,11 @@ +class Solution: + def removeAlmostEqualCharacters(self, word: str) -> int: + ans = 0 + i, n = 1, len(word) + while i < n: + if abs(ord(word[i]) - ord(word[i - 1])) < 2: + ans += 1 + i += 2 + else: + i += 1 + return ans diff --git a/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/Solution.ts b/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/Solution.ts new file mode 100644 index 0000000000000..e242ae50f80f9 --- /dev/null +++ b/solution/2900-2999/2957.Remove Adjacent Almost-Equal Characters/Solution.ts @@ -0,0 +1,10 @@ +function removeAlmostEqualCharacters(word: string): number { + let ans = 0; + for (let i = 1; i < word.length; ++i) { + if (Math.abs(word.charCodeAt(i) - word.charCodeAt(i - 1)) < 2) { + ++ans; + ++i; + } + } + return ans; +}