数组类算法 - 合集

embedded/2024/12/26 11:56:56/

*************

C++

topics include:

数组类算法 - LeetBook - 力扣(LeetCode)全球极客挚爱的技术成长平台

283. 移动零 - 力扣(LeetCode)

27. 移除元素 - 力扣(LeetCode)

26. 删除有序数组中的重复项 - 力扣(LeetCode)

80. 删除有序数组中的重复项 II - 力扣(LeetCode)

75. 颜色分类 - 力扣(LeetCode)

215. 数组中的第K个最大元素 - 力扣(LeetCode)

88. 合并两个有序数组 - 力扣(LeetCode)

167. 两数之和 II - 输入有序数组 - 力扣(LeetCode)

125. 验证回文串 - 力扣(LeetCode)

*************

As doing the array, something interesting happens:

The origininal topic is to move all 0 to the end of the array:

移动0 - 简单-CSDN博客icon-default.png?t=O83Ahttps://blog.csdn.net/ElseWhereR/article/details/144652952?spm=1001.2014.3001.5502And, move 0 out of the array:

移除0 - 简单-CSDN博客icon-default.png?t=O83Ahttps://blog.csdn.net/ElseWhereR/article/details/144654458?spm=1001.2014.3001.5502

*************

And next comes a little hard, need to move and delete at the same time:

This topic likes delete the repeat. As the topic is double pointer, so use two pointer here. The first pointer moves faster so just name it fast. The second pointer moves slower and name it slow. The most significant code is 

if (nums[fast + 1] != nums[fast])
{
nums[slow] = nums[fast];
}

and the code comes easy:

class Solution {
public:int removeDuplicates(vector<int>& nums) {int slow = 1; // 慢指针从索引1开始for (int fast = 1; fast < n; fast++) { // 快指针从索引1开始if (nums[fast] != nums[fast - 1]) {nums[slow] = nums[fast]; // 复制不同的元素slow++; // 慢指针向前移动}}return slow; // 返回新的数组长度}
};

*************

Guess what, delete repeat ones if it repeat 3 or more times:

80. 删除有序数组中的重复项 II - 力扣(LeetCode)icon-default.png?t=O83Ahttps://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/description/

It does have some challenge. The nums is orsered and it makes a little bit easy. Compare the nums[fast + 2] with nums[fast]. It ensures only 2 elements remains the same. If you want keep 3 same elements, just compare nums[fast + 3] with nums[fast].

class Solution {
public:int removeDuplicates(vector<int>& nums) {int n = nums.size();int slow = 0; // 慢指针,指向下一个可能的非重复元素的位置for (int fast = 2; fast < n; fast++) { // 快指针,遍历数组if (nums[fast] != nums[slow + 2]) { // 如果当前元素不等于慢指针前两个位置的元素nums[slow] = nums[fast]; // 将当前元素复制到慢指针的位置,并移动慢指针slow++;}}return slow; // 返回新数组的长度}
};

The code dosenot run:

Line 1122: Char 34: runtime error: addition of unsigned offset to 0x503000000070 overflowed to 0x50300000006c (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/stl_vector.h:1131:34

This is because data goes beyond borders. For example:

nums = [1 2 3]n = 2if fast = 2nums[2] != nums[4]however nums[4] 实际是不存在的

Take borders into consideration:

class Solution {
public:int removeDuplicates(vector<int>& nums) {if (nums.size() <= 2) {return nums.size(); // 如果数组长度小于等于2,不需要删除任何元素}int slow = 2; // 慢指针,指向下一个可能的非重复元素的位置for (int fast = 2; fast < nums.size(); fast++) { // 快指针,遍历数组if (nums[fast] != nums[slow - 2]) { // 如果当前元素不等于慢指针前两个位置的元素nums[slow] = nums[fast]; // 将当前元素复制到慢指针的位置,并移动慢指针slow++;}}return slow; // 返回新数组的长度}
};

*************

Nums are ordered in the topics before. And what if the order is unordered?

75. 颜色分类 - 力扣(LeetCode)icon-default.png?t=O83Ahttps://leetcode.cn/problems/sort-colors/description/

Need to order them without using sort. Let's see if I use sort:

One line code runs.

And use double pointer  seems a little bit noncommittal. Only 3 elements: 0 1 2, just ordering 0 and 1 is fine. The rest of the elements will place themselves at the right place. Use double pointer. If nums[left] == 0, moves left. If nums[right] == 2, moves right.

class Solution {
public:void sortColors(vector<int>& nums) {int n = nums.size();int left = 0;int right = n - 1;int current = 0;for (current = 0; current < n; current++){if (nums[current] == 0){nums[left] = nums[current];left++;} else if (nums[current] == 2){nums[right] = nums[current];right--;} }}
};

A brilliant idea whit running wrong:

Simulate the progress:

  • Initial array: [2, 0, 2, 1, 1, 0]
  • left = 0, right = 5, current = 0
  • Iteration 1:
  • current = 0, nums[0] = 2
  • Assign nums[5] = 2 (nums[5] was 0, now it's 2)
  • right = 4
  • Array: [2, 0, 2, 1, 1, 2]
  • Iteration 2:
  • current = 1, nums[1] = 0
  • Assign nums[0] = 0
  • left = 1
  • Array: [0, 0, 2, 1, 1, 2]
  • Iteration 3:
  • current = 2, nums[2] = 2
  • Assign nums[4] = 2 (nums[4] was 1, now it's 2)
  • right = 3
  • Array: [0, 0, 2, 1, 2, 2]
  • Iteration 4:
  • current = 3, nums[3] = 1
  • Do nothing
  • current = 4
  • Iteration 5:
  • current = 4, nums[4] = 2
  • Assign nums[3] = 2
  • right = 2
  • Array: [0, 0, 2, 2, 2, 2]
  • Iteration 6:
  • current = 5, nums[5] = 2
  • Assign nums[2] = 2
  • right = 1
  • Array: [0, 0, 2, 2, 2, 2]
  • End of loop.

The problem is that just swap.

class Solution {
public:void sortColors(vector<int>& nums) {int n = nums.size();int left = 0;int right = n - 1;int current = 0;while (current <= right) {if (nums[current] == 0) {swap(nums[left], nums[current]);left++;current++;} else if (nums[current] == 2) {swap(nums[right], nums[current]);right--;} else { // nums[current] == 1current++;}}}
};

*************

215. 数组中的第K个最大元素 - 力扣(LeetCode)icon-default.png?t=O83Ahttps://leetcode.cn/problems/kth-largest-element-in-an-array/description/

First eye on the topic, the demand confuse me. Note that it is the kth largest element in the sorted order, not the kth distinct element. The requirement is to return nums[n - k + 1].

First at the begining, bouble sort comes first. However sorting is forbidden.

Still cannot solve it. leave it alone.

*************

88. 合并两个有序数组 - 力扣(LeetCode)icon-default.png?t=O83Ahttps://leetcode.cn/problems/merge-sorted-array/

Merge two ordered arries. Copy nums1 and nums2 to make a new array 3 and sort array 3:

class Solution {
public:void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {// 创建临时数组vector<int> temp(m + n);// 复制 nums1 的前 m 个元素到 tempfor (int i = 0; i < m; i++) {temp[i] = nums1[i];}// 复制 nums2 的前 n 个元素到 tempfor (int i = 0; i < n; i++) {temp[m + i] = nums2[i];}// 对 temp 进行排序sort(temp.begin(), temp.end());// 将排序后的 temp 复制回 nums1for (int i = 0; i < m + n; i++) {nums1[i] = temp[i];}}
};

*************

167. 两数之和 II - 输入有序数组 - 力扣(LeetCode)icon-default.png?t=O83Ahttps://leetcode.cn/problems/two-sum-ii-input-array-is-sorted/description/

Think about it. I have two pointers i and j. make pointer moves from left. Make pointer j moves from right. each moving estimating numbers[i[] + numsbers[j] != target.

class Solution {
public:vector<int> twoSum(vector<int>& numbers, int target) {int n = numbers.size();for (int i = 0; i < n; i++){for (int j = n - 1; j > 0; j--){if (numbers[i] + numbers[j] == target){return {i + 1, j + 1};}}}return { };}
};

It runs overtime. I cannot understand.

copy the answer:

class Solution {
public:vector<int> twoSum(vector<int>& numbers, int target) {int left = 0;int right = numbers.size() - 1;while (left < right) {int sum = numbers[left] + numbers[right];if (sum == target) {return {left + 1, right + 1};} else if (sum < target) {left++;} else {right--;}}// 题目保证有解,所以理论上不会执行到这一步return {};}
};

*************

125. 验证回文串 - 力扣(LeetCode)icon-default.png?t=O83Ahttps://leetcode.cn/problems/valid-palindrome/description/

This topic is the last topic of this paper because I have do a lot about Longest Palindromic Substring. 

In this topic, count the Longest Palindromic Substring of string:

最长回文子序列-CSDN博客icon-default.png?t=O83Ahttps://blog.csdn.net/ElseWhereR/article/details/143785381?spm=1001.2014.3001.5502

class Solution {
public:bool isPalindrome(string s) {int n = s.size(); // get the length of the stringvector<vector<int>> dp(n, vector<int>(n, 0)); // initialise the array into elements zerofor (int i = 0; i < n; i++){dp[i][i] = 1; // ez to understand that one letter is always true}// caculate the length more than 2for (int len = 2; len <= n; ++len) { // out-cicle is the length of the new stringfor (int i = 0; i <= n - len; ++i) { // in-cicle start from the different placeint j = i + len - 1; // define the end of the stringif (s[i] == s[j]) { // in new srtring, stare == end, which has the possibility, so need checkdp[i][j] = (i + 1 <= j - 1 ? dp[i + 1][j - 1] : 0) + 2;} else {dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);}}}// dp[0][n-1] returnreturn dp[0][n - 1];if (dp[0][n - 1] = n){return true;}else{return false;}}
};


http://www.ppmy.cn/embedded/148884.html

相关文章

LLaMA-Factory(一)环境配置及包下载

LLaMA-Factory(一&#xff09;环境配置及包下载 本机配置1. git下载2.创建虚拟环境3. 下载官方包内依赖4. 下载bitsandbytes5. 启动项目6. 可能出现问题1&#xff1a;pip install 出现 error: subprocess-exited-with-error 错误7. 可能出现问题2&#xff1a; ModuleNotFoundEr…

记录jvm进程号

日常开发中&#xff0c;相信大家会经常用到这么一行命令&#xff1a; ps -ef | grep xxx.jar | grep -v grep | awk {print $2} | xargs -r kill -9 就是杀掉xxx相关的进程&#xff0c;然后启动&#xff0c;当然也还有其他的方式可以实现类似的功能&#xff0c;我就不列举了&…

重温设计模式--5、职责链模式

文章目录 职责链模式的详细介绍C 代码示例C示例代码2 职责链模式的详细介绍 定义与概念 职责链模式&#xff08;Chain of Responsibility Pattern&#xff09;是一种行为型设计模式&#xff0c;它旨在将请求的发送者和多个接收者解耦&#xff0c;让多个对象都有机会处理请求&am…

Leetcode 695 Max Area of Island

题意 给定一个二维矩阵&#xff0c;矩阵中包含0和1&#xff0c;所有相连的1被视为一个岛屿&#xff0c;求这些所有岛屿的最大区域是多少 题目链接 https://leetcode.com/problems/max-area-of-island/description/ 题解 遍历二维矩阵&#xff0c;当遇到没有被访问过的1时&…

【Linux进程】进程信号

目录 1. 信号 2. 信号的产生 2.1 终端按键 自定义信号处理 2.2 系统调用 kill raise abort 2.3 硬件异常 2.4 软件条件产生 思考 总结 1. 信号 在Linux中存在着一种通信的方式&#xff0c;与管道和System V IPC不同&#xff0c;更准确的说是一种通知机制&#xff0c;…

如何查看flink错误信息

flink出错时&#xff0c;可以通过以下步骤查看flink错误信息&#xff1a; 1.打开flink webui界面 2.进入overview或running jobs页面 3.点击出错的job name&#xff0c;出错的jobname一般后面的status会变红 4.在job 详情页面&#xff0c;点击exception&#xff0c;即可查看错…

eth_type_trans 函数

eth_type_trans 是 Linux 内核网络子系统中的一个函数,它主要用于确定接收到的以太网数据包(Ethernet frame)的协议类型,并设置相应的 sk_buff 结构体的协议字段。以下是关于 eth_type_trans 的详细解释: 功能 eth_type_trans 函数的主要功能是根据以太网数据包的目的 M…

Flink中并行度和slot的关系——任务和任务槽

一、任务槽&#xff08;task slots) Flink的每一个TaskManager是一个JVM进程&#xff0c;在其上可以运行多个线程&#xff08;任务task&#xff09;&#xff0c;那么每个线程可以拥有多少进程资源呢&#xff1f;任务槽就是这样一个概念&#xff0c;对taskManager上每个任务运行…