diff --git a/solution/1000-1099/1060.Missing Element in Sorted Array/README.md b/solution/1000-1099/1060.Missing Element in Sorted Array/README.md index 1b826003198c3..ed5df7c4c2f30 100644 --- a/solution/1000-1099/1060.Missing Element in Sorted Array/README.md +++ b/solution/1000-1099/1060.Missing Element in Sorted Array/README.md @@ -55,6 +55,12 @@ +**方法一:二分查找** + +我们设计一个函数 $missing(i)$,表示 $nums[i]$ 与 $nums[0]$ 之间缺失的元素个数。那么 $missing(i)$ 就等于 $nums[i] - nums[0] - i$。我们可以通过二分查找找到最小的 $i$,使得 $missing(i) \geq k$,那么 $nums[i - 1] + k - missing(i - 1)$ 就是第 $k$ 个缺失的元素。 + +时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $nums$ 的长度。 + ### **Python3** @@ -62,7 +68,22 @@ ```python - +class Solution: + def missingElement(self, nums: List[int], k: int) -> int: + def missing(i: int) -> int: + return nums[i] - nums[0] - i + + n = len(nums) + if k > missing(n - 1): + return nums[n - 1] + k - missing(n - 1) + l, r = 0, n - 1 + while l < r: + mid = (l + r) >> 1 + if missing(mid) >= k: + r = mid + else: + l = mid + 1 + return nums[l - 1] + k - missing(l - 1) ``` ### **Java** @@ -70,7 +91,79 @@ ```java +class Solution { + public int missingElement(int[] nums, int k) { + int n = nums.length; + if (k > missing(nums, n - 1)) { + return nums[n - 1] + k - missing(nums, n - 1); + } + int l = 0, r = n - 1; + while (l < r) { + int mid = (l + r) >> 1; + if (missing(nums, mid) >= k) { + r = mid; + } else { + l = mid + 1; + } + } + return nums[l - 1] + k - missing(nums, l - 1); + } + + private int missing(int[] nums, int i) { + return nums[i] - nums[0] - i; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int missingElement(vector& nums, int k) { + auto missing = [&](int i) { + return nums[i] - nums[0] - i; + }; + int n = nums.size(); + if (k > missing(n - 1)) { + return nums[n - 1] + k - missing(n - 1); + } + int l = 0, r = n - 1; + while (l < r) { + int mid = (l + r) >> 1; + if (missing(mid) >= k) { + r = mid; + } else { + l = mid + 1; + } + } + return nums[l - 1] + k - missing(l - 1); + } +}; +``` +### **Go** + +```go +func missingElement(nums []int, k int) int { + missing := func(i int) int { + return nums[i] - nums[0] - i + } + n := len(nums) + if k > missing(n-1) { + return nums[n-1] + k - missing(n-1) + } + l, r := 0, n-1 + for l < r { + mid := (l + r) >> 1 + if missing(mid) >= k { + r = mid + } else { + l = mid + 1 + } + } + return nums[l-1] + k - missing(l-1) +} ``` ### **...** diff --git a/solution/1000-1099/1060.Missing Element in Sorted Array/README_EN.md b/solution/1000-1099/1060.Missing Element in Sorted Array/README_EN.md index 184e93138bc96..4baa243a21a06 100644 --- a/solution/1000-1099/1060.Missing Element in Sorted Array/README_EN.md +++ b/solution/1000-1099/1060.Missing Element in Sorted Array/README_EN.md @@ -51,13 +51,100 @@ ### **Python3** ```python - +class Solution: + def missingElement(self, nums: List[int], k: int) -> int: + def missing(i: int) -> int: + return nums[i] - nums[0] - i + + n = len(nums) + if k > missing(n - 1): + return nums[n - 1] + k - missing(n - 1) + l, r = 0, n - 1 + while l < r: + mid = (l + r) >> 1 + if missing(mid) >= k: + r = mid + else: + l = mid + 1 + return nums[l - 1] + k - missing(l - 1) ``` ### **Java** ```java +class Solution { + public int missingElement(int[] nums, int k) { + int n = nums.length; + if (k > missing(nums, n - 1)) { + return nums[n - 1] + k - missing(nums, n - 1); + } + int l = 0, r = n - 1; + while (l < r) { + int mid = (l + r) >> 1; + if (missing(nums, mid) >= k) { + r = mid; + } else { + l = mid + 1; + } + } + return nums[l - 1] + k - missing(nums, l - 1); + } + + private int missing(int[] nums, int i) { + return nums[i] - nums[0] - i; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int missingElement(vector& nums, int k) { + auto missing = [&](int i) { + return nums[i] - nums[0] - i; + }; + int n = nums.size(); + if (k > missing(n - 1)) { + return nums[n - 1] + k - missing(n - 1); + } + int l = 0, r = n - 1; + while (l < r) { + int mid = (l + r) >> 1; + if (missing(mid) >= k) { + r = mid; + } else { + l = mid + 1; + } + } + return nums[l - 1] + k - missing(l - 1); + } +}; +``` +### **Go** + +```go +func missingElement(nums []int, k int) int { + missing := func(i int) int { + return nums[i] - nums[0] - i + } + n := len(nums) + if k > missing(n-1) { + return nums[n-1] + k - missing(n-1) + } + l, r := 0, n-1 + for l < r { + mid := (l + r) >> 1 + if missing(mid) >= k { + r = mid + } else { + l = mid + 1 + } + } + return nums[l-1] + k - missing(l-1) +} ``` ### **...** diff --git a/solution/1000-1099/1060.Missing Element in Sorted Array/Solution.cpp b/solution/1000-1099/1060.Missing Element in Sorted Array/Solution.cpp new file mode 100644 index 0000000000000..6132439e3ad61 --- /dev/null +++ b/solution/1000-1099/1060.Missing Element in Sorted Array/Solution.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int missingElement(vector& nums, int k) { + auto missing = [&](int i) { + return nums[i] - nums[0] - i; + }; + int n = nums.size(); + if (k > missing(n - 1)) { + return nums[n - 1] + k - missing(n - 1); + } + int l = 0, r = n - 1; + while (l < r) { + int mid = (l + r) >> 1; + if (missing(mid) >= k) { + r = mid; + } else { + l = mid + 1; + } + } + return nums[l - 1] + k - missing(l - 1); + } +}; \ No newline at end of file diff --git a/solution/1000-1099/1060.Missing Element in Sorted Array/Solution.go b/solution/1000-1099/1060.Missing Element in Sorted Array/Solution.go new file mode 100644 index 0000000000000..1bcfb55219d3e --- /dev/null +++ b/solution/1000-1099/1060.Missing Element in Sorted Array/Solution.go @@ -0,0 +1,19 @@ +func missingElement(nums []int, k int) int { + missing := func(i int) int { + return nums[i] - nums[0] - i + } + n := len(nums) + if k > missing(n-1) { + return nums[n-1] + k - missing(n-1) + } + l, r := 0, n-1 + for l < r { + mid := (l + r) >> 1 + if missing(mid) >= k { + r = mid + } else { + l = mid + 1 + } + } + return nums[l-1] + k - missing(l-1) +} \ No newline at end of file diff --git a/solution/1000-1099/1060.Missing Element in Sorted Array/Solution.java b/solution/1000-1099/1060.Missing Element in Sorted Array/Solution.java new file mode 100644 index 0000000000000..4fe10cca06185 --- /dev/null +++ b/solution/1000-1099/1060.Missing Element in Sorted Array/Solution.java @@ -0,0 +1,22 @@ +class Solution { + public int missingElement(int[] nums, int k) { + int n = nums.length; + if (k > missing(nums, n - 1)) { + return nums[n - 1] + k - missing(nums, n - 1); + } + int l = 0, r = n - 1; + while (l < r) { + int mid = (l + r) >> 1; + if (missing(nums, mid) >= k) { + r = mid; + } else { + l = mid + 1; + } + } + return nums[l - 1] + k - missing(nums, l - 1); + } + + private int missing(int[] nums, int i) { + return nums[i] - nums[0] - i; + } +} \ No newline at end of file diff --git a/solution/1000-1099/1060.Missing Element in Sorted Array/Solution.py b/solution/1000-1099/1060.Missing Element in Sorted Array/Solution.py new file mode 100644 index 0000000000000..c8194fea5a239 --- /dev/null +++ b/solution/1000-1099/1060.Missing Element in Sorted Array/Solution.py @@ -0,0 +1,16 @@ +class Solution: + def missingElement(self, nums: List[int], k: int) -> int: + def missing(i: int) -> int: + return nums[i] - nums[0] - i + + n = len(nums) + if k > missing(n - 1): + return nums[n - 1] + k - missing(n - 1) + l, r = 0, n - 1 + while l < r: + mid = (l + r) >> 1 + if missing(mid) >= k: + r = mid + else: + l = mid + 1 + return nums[l - 1] + k - missing(l - 1) diff --git a/solution/1200-1299/1287.Element Appearing More Than 25% In Sorted Array/README.md b/solution/1200-1299/1287.Element Appearing More Than 25% In Sorted Array/README.md index a795e105d7546..2212d8d997cef 100644 --- a/solution/1200-1299/1287.Element Appearing More Than 25% In Sorted Array/README.md +++ b/solution/1200-1299/1287.Element Appearing More Than 25% In Sorted Array/README.md @@ -112,6 +112,24 @@ var findSpecialInteger = function (arr) { }; ``` +### **PHP** + +```php +class Solution { + /** + * @param Integer[] $arr + * @return Integer + */ + function findSpecialInteger($arr) { + $len = count($arr); + for ($i = 0; $i < $len; $i++) { + if ($arr[$i] == $arr[$i + ($len >> 2)]) return $arr[$i]; + } + return -1; + } +} +``` + ### **...** ``` diff --git a/solution/1200-1299/1287.Element Appearing More Than 25% In Sorted Array/README_EN.md b/solution/1200-1299/1287.Element Appearing More Than 25% In Sorted Array/README_EN.md index eb2c8b9f435e4..58f0df7a8c0b8 100644 --- a/solution/1200-1299/1287.Element Appearing More Than 25% In Sorted Array/README_EN.md +++ b/solution/1200-1299/1287.Element Appearing More Than 25% In Sorted Array/README_EN.md @@ -107,6 +107,24 @@ var findSpecialInteger = function (arr) { }; ``` +### **PHP** + +```php +class Solution { + /** + * @param Integer[] $arr + * @return Integer + */ + function findSpecialInteger($arr) { + $len = count($arr); + for ($i = 0; $i < $len; $i++) { + if ($arr[$i] == $arr[$i + ($len >> 2)]) return $arr[$i]; + } + return -1; + } +} +``` + ### **...** ``` diff --git a/solution/1200-1299/1287.Element Appearing More Than 25% In Sorted Array/Solution.php b/solution/1200-1299/1287.Element Appearing More Than 25% In Sorted Array/Solution.php new file mode 100644 index 0000000000000..0bb791071d335 --- /dev/null +++ b/solution/1200-1299/1287.Element Appearing More Than 25% In Sorted Array/Solution.php @@ -0,0 +1,13 @@ +class Solution { + /** + * @param Integer[] $arr + * @return Integer + */ + function findSpecialInteger($arr) { + $len = count($arr); + for ($i = 0; $i < $len; $i++) { + if ($arr[$i] == $arr[$i + ($len >> 2)]) return $arr[$i]; + } + return -1; + } +} \ No newline at end of file diff --git a/solution/1800-1899/1848.Minimum Distance to the Target Element/README.md b/solution/1800-1899/1848.Minimum Distance to the Target Element/README.md index 19870c63ce616..52305ca29720c 100644 --- a/solution/1800-1899/1848.Minimum Distance to the Target Element/README.md +++ b/solution/1800-1899/1848.Minimum Distance to the Target Element/README.md @@ -53,6 +53,12 @@ +**方法一:一次遍历** + +遍历数组,找到所有等于 `target` 的下标,然后计算 `abs(i - start)`,取最小值即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。 + ### **Python3** @@ -62,11 +68,11 @@ ```python class Solution: def getMinDistance(self, nums: List[int], target: int, start: int) -> int: - res = inf - for i, num in enumerate(nums): - if num == target: - res = min(res, abs(i - start)) - return res + ans = inf + for i, x in enumerate(nums): + if x == target: + ans = min(ans, abs(i - start)) + return ans ``` ### **Java** @@ -76,13 +82,14 @@ class Solution: ```java class Solution { public int getMinDistance(int[] nums, int target, int start) { - int res = Integer.MAX_VALUE; - for (int i = 0; i < nums.length; ++i) { + int n = nums.length; + int ans = n; + for (int i = 0; i < n; ++i) { if (nums[i] == target) { - res = Math.min(res, Math.abs(i - start)); + ans = Math.min(ans, Math.abs(i - start)); } } - return res; + return ans; } } ``` @@ -93,17 +100,39 @@ class Solution { class Solution { public: int getMinDistance(vector& nums, int target, int start) { - int res = nums.size(); - for (int i = 0; i < nums.size(); ++i) { + int n = nums.size(); + int ans = n; + for (int i = 0; i < n; ++i) { if (nums[i] == target) { - res = min(res, abs(i - start)); + ans = min(ans, abs(i - start)); } } - return res; + return ans; } }; ``` +### **Go** + +```go +func getMinDistance(nums []int, target int, start int) int { + ans := 1 << 30 + for i, x := range nums { + if t := abs(i - start); x == target && t < ans { + ans = t + } + } + return ans +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} +``` + ### **...** ``` diff --git a/solution/1800-1899/1848.Minimum Distance to the Target Element/README_EN.md b/solution/1800-1899/1848.Minimum Distance to the Target Element/README_EN.md index b24485e9d6951..4c98fb96e0d41 100644 --- a/solution/1800-1899/1848.Minimum Distance to the Target Element/README_EN.md +++ b/solution/1800-1899/1848.Minimum Distance to the Target Element/README_EN.md @@ -54,11 +54,11 @@ ```python class Solution: def getMinDistance(self, nums: List[int], target: int, start: int) -> int: - res = inf - for i, num in enumerate(nums): - if num == target: - res = min(res, abs(i - start)) - return res + ans = inf + for i, x in enumerate(nums): + if x == target: + ans = min(ans, abs(i - start)) + return ans ``` ### **Java** @@ -66,13 +66,14 @@ class Solution: ```java class Solution { public int getMinDistance(int[] nums, int target, int start) { - int res = Integer.MAX_VALUE; - for (int i = 0; i < nums.length; ++i) { + int n = nums.length; + int ans = n; + for (int i = 0; i < n; ++i) { if (nums[i] == target) { - res = Math.min(res, Math.abs(i - start)); + ans = Math.min(ans, Math.abs(i - start)); } } - return res; + return ans; } } ``` @@ -83,17 +84,39 @@ class Solution { class Solution { public: int getMinDistance(vector& nums, int target, int start) { - int res = nums.size(); - for (int i = 0; i < nums.size(); ++i) { + int n = nums.size(); + int ans = n; + for (int i = 0; i < n; ++i) { if (nums[i] == target) { - res = min(res, abs(i - start)); + ans = min(ans, abs(i - start)); } } - return res; + return ans; } }; ``` +### **Go** + +```go +func getMinDistance(nums []int, target int, start int) int { + ans := 1 << 30 + for i, x := range nums { + if t := abs(i - start); x == target && t < ans { + ans = t + } + } + return ans +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} +``` + ### **...** ``` diff --git a/solution/1800-1899/1848.Minimum Distance to the Target Element/Solution.cpp b/solution/1800-1899/1848.Minimum Distance to the Target Element/Solution.cpp index 3881c1959159f..040715c0c2499 100644 --- a/solution/1800-1899/1848.Minimum Distance to the Target Element/Solution.cpp +++ b/solution/1800-1899/1848.Minimum Distance to the Target Element/Solution.cpp @@ -1,12 +1,13 @@ class Solution { public: int getMinDistance(vector& nums, int target, int start) { - int res = nums.size(); - for (int i = 0; i < nums.size(); ++i) { + int n = nums.size(); + int ans = n; + for (int i = 0; i < n; ++i) { if (nums[i] == target) { - res = min(res, abs(i - start)); + ans = min(ans, abs(i - start)); } } - return res; + return ans; } }; \ No newline at end of file diff --git a/solution/1800-1899/1848.Minimum Distance to the Target Element/Solution.go b/solution/1800-1899/1848.Minimum Distance to the Target Element/Solution.go new file mode 100644 index 0000000000000..edbc3bb64dafa --- /dev/null +++ b/solution/1800-1899/1848.Minimum Distance to the Target Element/Solution.go @@ -0,0 +1,16 @@ +func getMinDistance(nums []int, target int, start int) int { + ans := 1 << 30 + for i, x := range nums { + if t := abs(i - start); x == target && t < ans { + ans = t + } + } + return ans +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} \ No newline at end of file diff --git a/solution/1800-1899/1848.Minimum Distance to the Target Element/Solution.java b/solution/1800-1899/1848.Minimum Distance to the Target Element/Solution.java index 0d8fe16c450c1..9d20666892815 100644 --- a/solution/1800-1899/1848.Minimum Distance to the Target Element/Solution.java +++ b/solution/1800-1899/1848.Minimum Distance to the Target Element/Solution.java @@ -1,11 +1,12 @@ class Solution { public int getMinDistance(int[] nums, int target, int start) { - int res = Integer.MAX_VALUE; - for (int i = 0; i < nums.length; ++i) { + int n = nums.length; + int ans = n; + for (int i = 0; i < n; ++i) { if (nums[i] == target) { - res = Math.min(res, Math.abs(i - start)); + ans = Math.min(ans, Math.abs(i - start)); } } - return res; + return ans; } } \ No newline at end of file diff --git a/solution/1800-1899/1848.Minimum Distance to the Target Element/Solution.py b/solution/1800-1899/1848.Minimum Distance to the Target Element/Solution.py index 72d61e14408c9..861011590c4d0 100644 --- a/solution/1800-1899/1848.Minimum Distance to the Target Element/Solution.py +++ b/solution/1800-1899/1848.Minimum Distance to the Target Element/Solution.py @@ -1,7 +1,7 @@ class Solution: def getMinDistance(self, nums: List[int], target: int, start: int) -> int: - res = inf - for i, num in enumerate(nums): - if num == target: - res = min(res, abs(i - start)) - return res + ans = inf + for i, x in enumerate(nums): + if x == target: + ans = min(ans, abs(i - start)) + return ans diff --git a/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/README.md b/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/README.md index ddfb117caa6a2..4f96ba4ecfe9e 100644 --- a/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/README.md +++ b/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/README.md @@ -67,6 +67,12 @@ +**方法一:DFS** + +从字符串的第一个字符开始,枚举所有可能的拆分位置,判断拆分出来的子串是否满足题目要求,如果满足则继续递归判断剩余的子串是否满足题目要求,直到遍历完整个字符串。 + +时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为字符串的长度。 + ### **Python3** @@ -74,7 +80,19 @@ ```python - +class Solution: + def splitString(self, s: str) -> bool: + def dfs(i, x, k): + if i == len(s): + return k > 1 + y = 0 + for j in range(i, len(s)): + y = y * 10 + int(s[j]) + if (x == -1 or x - y == 1) and dfs(j + 1, y, k + 1): + return True + return False + + return dfs(0, -1, 0) ``` ### **Java** @@ -82,7 +100,80 @@ ```java +class Solution { + private String s; + + public boolean splitString(String s) { + this.s = s; + return dfs(0, -1, 0); + } + + private boolean dfs(int i, long x, int k) { + if (i == s.length()) { + return k > 1; + } + long y = 0; + for (int j = i; j < s.length(); ++j) { + y = y * 10 + (s.charAt(j) - '0'); + if ((x == -1 || x - y == 1) && dfs(j + 1, y, k + 1)) { + return true; + } + } + return false; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + bool splitString(string s) { + function dfs = [&](int i, long long x, int k) -> bool { + if (i == s.size()) { + return k > 1; + } + long long y = 0; + for (int j = i; j < s.size(); ++j) { + y = y * 10 + (s[j] - '0'); + if (y > 1e10) { + break; + } + if ((x == -1 || x - y == 1) && dfs(j + 1, y, k + 1)) { + return true; + } + } + return false; + }; + return dfs(0, -1, 0); + } +}; +``` +### **Go** + +```go +func splitString(s string) bool { + var dfs func(i, x, k int) bool + dfs = func(i, x, k int) bool { + if i == len(s) { + return k > 1 + } + y := 0 + for j := i; j < len(s); j++ { + y = y*10 + int(s[j]-'0') + if y > int(1e10) { + break + } + if (x == -1 || x-y == 1) && dfs(j+1, y, k+1) { + return true + } + } + return false + } + return dfs(0, -1, 0) +} ``` ### **...** diff --git a/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/README_EN.md b/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/README_EN.md index 4195ce10215e7..d6ed7b9b34677 100644 --- a/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/README_EN.md +++ b/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/README_EN.md @@ -58,13 +58,98 @@ The values are in descending order with adjacent values differing by 1. ### **Python3** ```python - +class Solution: + def splitString(self, s: str) -> bool: + def dfs(i, x, k): + if i == len(s): + return k > 1 + y = 0 + for j in range(i, len(s)): + y = y * 10 + int(s[j]) + if (x == -1 or x - y == 1) and dfs(j + 1, y, k + 1): + return True + return False + + return dfs(0, -1, 0) ``` ### **Java** ```java +class Solution { + private String s; + + public boolean splitString(String s) { + this.s = s; + return dfs(0, -1, 0); + } + + private boolean dfs(int i, long x, int k) { + if (i == s.length()) { + return k > 1; + } + long y = 0; + for (int j = i; j < s.length(); ++j) { + y = y * 10 + (s.charAt(j) - '0'); + if ((x == -1 || x - y == 1) && dfs(j + 1, y, k + 1)) { + return true; + } + } + return false; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + bool splitString(string s) { + function dfs = [&](int i, long long x, int k) -> bool { + if (i == s.size()) { + return k > 1; + } + long long y = 0; + for (int j = i; j < s.size(); ++j) { + y = y * 10 + (s[j] - '0'); + if (y > 1e10) { + break; + } + if ((x == -1 || x - y == 1) && dfs(j + 1, y, k + 1)) { + return true; + } + } + return false; + }; + return dfs(0, -1, 0); + } +}; +``` +### **Go** + +```go +func splitString(s string) bool { + var dfs func(i, x, k int) bool + dfs = func(i, x, k int) bool { + if i == len(s) { + return k > 1 + } + y := 0 + for j := i; j < len(s); j++ { + y = y*10 + int(s[j]-'0') + if y > int(1e10) { + break + } + if (x == -1 || x-y == 1) && dfs(j+1, y, k+1) { + return true + } + } + return false + } + return dfs(0, -1, 0) +} ``` ### **...** diff --git a/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/Solution.cpp b/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/Solution.cpp new file mode 100644 index 0000000000000..5e1c28e619df3 --- /dev/null +++ b/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/Solution.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + bool splitString(string s) { + function dfs = [&](int i, long long x, int k) -> bool { + if (i == s.size()) { + return k > 1; + } + long long y = 0; + for (int j = i; j < s.size(); ++j) { + y = y * 10 + (s[j] - '0'); + if (y > 1e10) { + break; + } + if ((x == -1 || x - y == 1) && dfs(j + 1, y, k + 1)) { + return true; + } + } + return false; + }; + return dfs(0, -1, 0); + } +}; \ No newline at end of file diff --git a/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/Solution.go b/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/Solution.go new file mode 100644 index 0000000000000..32c8b798cd992 --- /dev/null +++ b/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/Solution.go @@ -0,0 +1,20 @@ +func splitString(s string) bool { + var dfs func(i, x, k int) bool + dfs = func(i, x, k int) bool { + if i == len(s) { + return k > 1 + } + y := 0 + for j := i; j < len(s); j++ { + y = y*10 + int(s[j]-'0') + if y > int(1e10) { + break + } + if (x == -1 || x-y == 1) && dfs(j+1, y, k+1) { + return true + } + } + return false + } + return dfs(0, -1, 0) +} \ No newline at end of file diff --git a/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/Solution.java b/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/Solution.java new file mode 100644 index 0000000000000..584b3fafa2b49 --- /dev/null +++ b/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/Solution.java @@ -0,0 +1,22 @@ +class Solution { + private String s; + + public boolean splitString(String s) { + this.s = s; + return dfs(0, -1, 0); + } + + private boolean dfs(int i, long x, int k) { + if (i == s.length()) { + return k > 1; + } + long y = 0; + for (int j = i; j < s.length(); ++j) { + y = y * 10 + (s.charAt(j) - '0'); + if ((x == -1 || x - y == 1) && dfs(j + 1, y, k + 1)) { + return true; + } + } + return false; + } +} \ No newline at end of file diff --git a/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/Solution.py b/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/Solution.py new file mode 100644 index 0000000000000..6e4bb0f29830b --- /dev/null +++ b/solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/Solution.py @@ -0,0 +1,13 @@ +class Solution: + def splitString(self, s: str) -> bool: + def dfs(i, x, k): + if i == len(s): + return k > 1 + y = 0 + for j in range(i, len(s)): + y = y * 10 + int(s[j]) + if (x == -1 or x - y == 1) and dfs(j + 1, y, k + 1): + return True + return False + + return dfs(0, -1, 0) diff --git a/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/README.md b/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/README.md index 36540b6681a53..792e3550200ce 100644 --- a/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/README.md +++ b/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/README.md @@ -12,12 +12,10 @@
  • 选出两个下标(下标 从 0 开始 计数)iji != j),并 更新 triplets[j][max(ai, aj), max(bi, bj), max(ci, cj)] 。 -
    • 例如,triplets[i] = [2, 5, 3]triplets[j] = [1, 7, 5]triplets[j] 将会更新为 [max(2, 1), max(5, 7), max(3, 5)] = [2, 7, 5]
  • -

如果通过以上操作我们可以使得目标 三元组 target 成为 triplets 的一个 元素 ,则返回 true ;否则,返回 false

@@ -75,6 +73,16 @@ +**方法一:贪心** + +我们记 $target = [x, y, z]$,初始时 $d = e = f = 0$,表示当前的 $a, b, c$ 的最大值。 + +我们遍历数组 $triplets$,对于每个三元组 $[a, b, c]$,如果 $a \le x, b \le y, c \le z$,则将 $d, e, f$ 分别更新为 $max(d, a), max(e, b), max(f, c)$。 + +最后判断 $[d, e, f]$ 是否等于 $target$ 即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $triplets$ 的长度。 + ### **Python3** @@ -84,14 +92,14 @@ ```python class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: - maxA = maxB = maxC = 0 - tA, tB, tC = target + x, y, z = target + d = e = f = 0 for a, b, c in triplets: - if a <= tA and b <= tB and c <= tC: - maxA = max(maxA, a) - maxB = max(maxB, b) - maxC = max(maxC, c) - return (maxA, maxB, maxC) == (tA, tB, tC) + if a <= x and b <= y and c <= z: + d = max(d, a) + e = max(e, b) + f = max(f, c) + return [d, e, f] == target ``` ### **Java** @@ -101,35 +109,81 @@ class Solution: ```java class Solution { public boolean mergeTriplets(int[][] triplets, int[] target) { - int maxA = 0, maxB = 0, maxC = 0; - for (int[] triplet : triplets) { - int a = triplet[0], b = triplet[1], c = triplet[2]; - if (a <= target[0] && b <= target[1] && c <= target[2]) { - maxA = Math.max(maxA, a); - maxB = Math.max(maxB, b); - maxC = Math.max(maxC, c); + int x = target[0], y = target[1], z = target[2]; + int d = 0, e = 0, f = 0; + for (var t : triplets) { + int a = t[0], b = t[1], c = t[2]; + if (a <= x && b <= y && c <= z) { + d = Math.max(d, a); + e = Math.max(e, b); + f = Math.max(f, c); } } - return maxA == target[0] && maxB == target[1] && maxC == target[2]; + return d == x && e == y && f == z; } } ``` +### **C++** + +```cpp +class Solution { +public: + bool mergeTriplets(vector>& triplets, vector& target) { + int x = target[0], y = target[1], z = target[2]; + int d = 0, e = 0, f = 0; + for (auto& t : triplets) { + int a = t[0], b = t[1], c = t[2]; + if (a <= x && b <= y && c <= z) { + d = max(d, a); + e = max(e, b); + f = max(f, c); + } + } + return d == x && e == y && f == z; + } +}; +``` + +### **Go** + +```go +func mergeTriplets(triplets [][]int, target []int) bool { + x, y, z := target[0], target[1], target[2] + d, e, f := 0, 0, 0 + for _, t := range triplets { + a, b, c := t[0], t[1], t[2] + if a <= x && b <= y && c <= z { + d = max(d, a) + e = max(e, b) + f = max(f, c) + } + } + return d == x && e == y && f == z +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` + ### **TypeScript** ```ts function mergeTriplets(triplets: number[][], target: number[]): boolean { - let [x, y, z] = target; // 目标值 - let [i, j, k] = [0, 0, 0]; // 最大值 - for (let triplet of triplets) { - let [a, b, c] = triplet; // 当前值 + const [x, y, z] = target; + let [d, e, f] = [0, 0, 0]; + for (const [a, b, c] of triplets) { if (a <= x && b <= y && c <= z) { - i = Math.max(i, a); - j = Math.max(j, b); - k = Math.max(k, c); + d = Math.max(d, a); + e = Math.max(e, b); + f = Math.max(f, c); } } - return i == x && j == y && k == z; + return d === x && e === y && f === z; } ``` diff --git a/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/README_EN.md b/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/README_EN.md index 35c49ebbf7111..88b1c8626ac50 100644 --- a/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/README_EN.md +++ b/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/README_EN.md @@ -10,12 +10,10 @@
  • Choose two indices (0-indexed) i and j (i != j) and update triplets[j] to become [max(ai, aj), max(bi, bj), max(ci, cj)]. -
    • For example, if triplets[i] = [2, 5, 3] and triplets[j] = [1, 7, 5], triplets[j] will be updated to [max(2, 1), max(5, 7), max(3, 5)] = [2, 7, 5].
  • -

Return true if it is possible to obtain the target triplet [x, y, z] as an element of triplets, or false otherwise.

@@ -68,14 +66,14 @@ The target triplet [5,5,5] is now an element of triplets. ```python class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: - maxA = maxB = maxC = 0 - tA, tB, tC = target + x, y, z = target + d = e = f = 0 for a, b, c in triplets: - if a <= tA and b <= tB and c <= tC: - maxA = max(maxA, a) - maxB = max(maxB, b) - maxC = max(maxC, c) - return (maxA, maxB, maxC) == (tA, tB, tC) + if a <= x and b <= y and c <= z: + d = max(d, a) + e = max(e, b) + f = max(f, c) + return [d, e, f] == target ``` ### **Java** @@ -83,17 +81,64 @@ class Solution: ```java class Solution { public boolean mergeTriplets(int[][] triplets, int[] target) { - int maxA = 0, maxB = 0, maxC = 0; - for (int[] triplet : triplets) { - int a = triplet[0], b = triplet[1], c = triplet[2]; - if (a <= target[0] && b <= target[1] && c <= target[2]) { - maxA = Math.max(maxA, a); - maxB = Math.max(maxB, b); - maxC = Math.max(maxC, c); + int x = target[0], y = target[1], z = target[2]; + int d = 0, e = 0, f = 0; + for (var t : triplets) { + int a = t[0], b = t[1], c = t[2]; + if (a <= x && b <= y && c <= z) { + d = Math.max(d, a); + e = Math.max(e, b); + f = Math.max(f, c); + } + } + return d == x && e == y && f == z; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + bool mergeTriplets(vector>& triplets, vector& target) { + int x = target[0], y = target[1], z = target[2]; + int d = 0, e = 0, f = 0; + for (auto& t : triplets) { + int a = t[0], b = t[1], c = t[2]; + if (a <= x && b <= y && c <= z) { + d = max(d, a); + e = max(e, b); + f = max(f, c); } } - return maxA == target[0] && maxB == target[1] && maxC == target[2]; + return d == x && e == y && f == z; } +}; +``` + +### **Go** + +```go +func mergeTriplets(triplets [][]int, target []int) bool { + x, y, z := target[0], target[1], target[2] + d, e, f := 0, 0, 0 + for _, t := range triplets { + a, b, c := t[0], t[1], t[2] + if a <= x && b <= y && c <= z { + d = max(d, a) + e = max(e, b) + f = max(f, c) + } + } + return d == x && e == y && f == z +} + +func max(a, b int) int { + if a > b { + return a + } + return b } ``` @@ -101,17 +146,16 @@ class Solution { ```ts function mergeTriplets(triplets: number[][], target: number[]): boolean { - let [x, y, z] = target; // 目标值 - let [i, j, k] = [0, 0, 0]; // 最大值 - for (let triplet of triplets) { - let [a, b, c] = triplet; // 当前值 + const [x, y, z] = target; + let [d, e, f] = [0, 0, 0]; + for (const [a, b, c] of triplets) { if (a <= x && b <= y && c <= z) { - i = Math.max(i, a); - j = Math.max(j, b); - k = Math.max(k, c); + d = Math.max(d, a); + e = Math.max(e, b); + f = Math.max(f, c); } } - return i == x && j == y && k == z; + return d === x && e === y && f === z; } ``` diff --git a/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/Solution.cpp b/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/Solution.cpp new file mode 100644 index 0000000000000..3877fdc3b9836 --- /dev/null +++ b/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/Solution.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + bool mergeTriplets(vector>& triplets, vector& target) { + int x = target[0], y = target[1], z = target[2]; + int d = 0, e = 0, f = 0; + for (auto& t : triplets) { + int a = t[0], b = t[1], c = t[2]; + if (a <= x && b <= y && c <= z) { + d = max(d, a); + e = max(e, b); + f = max(f, c); + } + } + return d == x && e == y && f == z; + } +}; \ No newline at end of file diff --git a/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/Solution.go b/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/Solution.go new file mode 100644 index 0000000000000..f4df779ca93ba --- /dev/null +++ b/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/Solution.go @@ -0,0 +1,20 @@ +func mergeTriplets(triplets [][]int, target []int) bool { + x, y, z := target[0], target[1], target[2] + d, e, f := 0, 0, 0 + for _, t := range triplets { + a, b, c := t[0], t[1], t[2] + if a <= x && b <= y && c <= z { + d = max(d, a) + e = max(e, b) + f = max(f, c) + } + } + return d == x && e == y && f == z +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} \ No newline at end of file diff --git a/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/Solution.java b/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/Solution.java index 65c78b02ce64e..f15329426e684 100644 --- a/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/Solution.java +++ b/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/Solution.java @@ -1,14 +1,15 @@ class Solution { public boolean mergeTriplets(int[][] triplets, int[] target) { - int maxA = 0, maxB = 0, maxC = 0; - for (int[] triplet : triplets) { - int a = triplet[0], b = triplet[1], c = triplet[2]; - if (a <= target[0] && b <= target[1] && c <= target[2]) { - maxA = Math.max(maxA, a); - maxB = Math.max(maxB, b); - maxC = Math.max(maxC, c); + int x = target[0], y = target[1], z = target[2]; + int d = 0, e = 0, f = 0; + for (var t : triplets) { + int a = t[0], b = t[1], c = t[2]; + if (a <= x && b <= y && c <= z) { + d = Math.max(d, a); + e = Math.max(e, b); + f = Math.max(f, c); } } - return maxA == target[0] && maxB == target[1] && maxC == target[2]; + return d == x && e == y && f == z; } } \ No newline at end of file diff --git a/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/Solution.py b/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/Solution.py index 7fb2608750c26..454a47c648899 100644 --- a/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/Solution.py +++ b/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/Solution.py @@ -1,10 +1,10 @@ class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: - maxA = maxB = maxC = 0 - tA, tB, tC = target + x, y, z = target + d = e = f = 0 for a, b, c in triplets: - if a <= tA and b <= tB and c <= tC: - maxA = max(maxA, a) - maxB = max(maxB, b) - maxC = max(maxC, c) - return (maxA, maxB, maxC) == (tA, tB, tC) + if a <= x and b <= y and c <= z: + d = max(d, a) + e = max(e, b) + f = max(f, c) + return [d, e, f] == target diff --git a/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/Solution.ts b/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/Solution.ts index 14e8722f1454d..a37467be48192 100644 --- a/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/Solution.ts +++ b/solution/1800-1899/1899.Merge Triplets to Form Target Triplet/Solution.ts @@ -1,13 +1,12 @@ function mergeTriplets(triplets: number[][], target: number[]): boolean { - let [x, y, z] = target; // 目标值 - let [i, j, k] = [0, 0, 0]; // 最大值 - for (let triplet of triplets) { - let [a, b, c] = triplet; // 当前值 + const [x, y, z] = target; + let [d, e, f] = [0, 0, 0]; + for (const [a, b, c] of triplets) { if (a <= x && b <= y && c <= z) { - i = Math.max(i, a); - j = Math.max(j, b); - k = Math.max(k, c); + d = Math.max(d, a); + e = Math.max(e, b); + f = Math.max(f, c); } } - return i == x && j == y && k == z; + return d === x && e === y && f === z; }