class Solution { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int i = m - 1; // nums1 的有效部分的最后一个元素的索引 int j = n - 1; // nums2 的最后一个元素的索引 int k = m + n - 1; // 合并后的数组的最后一个位置的索引
// 从后向前遍历数组,将较大的元素放到 nums1 的后面 while (i >= 0 && j >= 0) { if (nums1[i] > nums2[j]) { nums1[k--] = nums1[i--]; } else { nums1[k--] = nums2[j--]; } }
// 如果 nums2 还有剩余元素,将它们拷贝到 nums1 while (j >= 0) { nums1[k--] = nums2[j--]; } quick_sort(nums1.data(), 0, m + n - 1); }
void quick_sort(int q[], int l, int r) { if (l >= r) return;
int i = l - 1, j = r + 1, x = q[l + r >> 1]; while (i < j) { do i++; while (q[i] < x); do j--; while (q[j] > x); if (i < j) swap(q[i], q[j]); } quick_sort(q, l, j), quick_sort(q, j + 1, r); } };
给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素。元素的顺序可能发生改变。然后返回 nums 中与 val 不同的元素的数量。
假设 nums 中不等于 val 的元素数量为 k,要通过此题,您需要执行以下操作:
更改 nums 数组,使 nums 的前 k 个元素包含不等于 val 的元素。nums 的其余元素和 nums 的大小并不重要。
返回 k。
用户评测:
评测机将使用以下代码测试您的解决方案:
1 2 3 4 5 6 7 8 9 10 11 12
int[] nums = [...]; // 输入数组 int val = ...; // 要移除的值 int[] expectedNums = [...]; // 长度正确的预期答案。 // 它以不等于 val 的值排序。
int k = removeElement(nums, val); // 调用你的实现
assert k == expectedNums.length; sort(nums, 0, k); // 排序 nums 的前 k 个元素 for (int i = 0; i < actualLength; i++) { assert nums[i] == expectedNums[i]; }
如果所有的断言都通过,你的解决方案将会 通过。
示例 1:
1 2 3 4
输入:nums = [3,2,2,3], val = 3 输出:2, nums = [2,2,_,_] 解释:你的函数函数应该返回 k = 2, 并且 nums 中的前两个元素均为 2。 你在返回的 k 个元素之外留下了什么并不重要(因此它们并不计入评测)。
示例 2:
1 2 3 4 5
输入:nums = [0,1,2,2,3,0,4,2], val = 2 输出:5, nums = [0,1,4,0,3,_,_,_] 解释:你的函数应该返回 k = 5,并且 nums 中的前五个元素为 0,0,1,3,4。 注意这五个元素可以任意顺序返回。 你在返回的 k 个元素之外留下了什么并不重要(因此它们并不计入评测)。
提示:
0 <= nums.length <= 100
0 <= nums[i] <= 50
0 <= val <= 100
1 2 3 4 5 6 7 8 9 10
class Solution { public: int removeElement(vector<int>& nums, int val) { int index = 0; for(auto x: nums){ if (x!=val) nums[index++] = x; } return index; } };
class Solution { public: int strStr(string haystack, string needle) { if (needle.empty()) return 0; int m = haystack.size(); int n = needle.size(); if (n > m) return -1; // 构建 next 数组 vector<int> next(n, 0); int j = 0; for (int i = 1; i < n; ++i) { while (j > 0 && needle[i] != needle[j]) { j = next[j - 1]; } if (needle[i] == needle[j]) { ++j; } next[i] = j; } // KMP 匹配过程 int i = 0; // haystack int k = 0; // needle while (i < m) { if (k == n) return i - k; if (haystack[i] == needle[k]) { ++i; ++k; } else if (k > 0) { k = next[k - 1]; } else { ++i; } } return k == n ? i - k : -1; } };
爆破
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class Solution { public: int strStr(string haystack, string needle) { int m = haystack.size(); int n = needle.size(); if (n == 0) return 0; // 如果 needle 为空字符串,返回 0 for (int i = 0; i <= m - n; ++i) { int j = 0; // 检查从当前位置 i 开始的子字符串是否与 needle 匹配 while (j < n && haystack[i + j] == needle[j]) { ++j; } // 如果 j 等于 needle 的长度,说明找到了匹配 if (j == n) return i; } // 如果没有找到匹配,返回 -1 return -1; } };
class Solution { public: int searchInsert(vector<int>& nums, int target) { int n = nums.size(); int left = 0, right = n - 1, ans = n; while (left <= right) { int mid = ((right - left) >> 1) + left; if (target <= nums[mid]) { ans = mid; right = mid - 1; } else { left = mid + 1; } } return ans; } };