news 2026/1/25 11:13:16

实习刷题18

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
实习刷题18

今天再摆一次吧,嘿嘿嘿

一:乘积最大子数组

class Solution { public: int maxProduct(vector<int>& nums) { int n = nums.size(); int pre_min = 1; int pre_max = 1; int max = INT_MIN; for(int i = 0 ; i < n ; ++i) { int curr_min = std::min(nums[i],std::min(pre_min*nums[i],pre_max*nums[i])); int curr_max = std::max(nums[i],std::max(pre_max*nums[i],pre_min*nums[i])); pre_max = curr_max; pre_min = curr_min; max = std::max(max,curr_max); } return max; } };

二:分割和等子集

class Solution { public: bool canPartition(vector<int>& nums) { int sum = std::accumulate(nums.begin() , nums.end(),0); if(!sum || sum % 2) { return false; } int target = sum / 2; std::vector<int> dp(target+1); dp[0] = 1; for(auto& num : nums) { for(int i = target ; i >= num ; --i) { if(dp[i - num]) { dp[i] = 1; } if(dp[target]) { return true; } } } return false; } };

三:最长有效括号

class Solution { public: int longestValidParentheses(string s) { int n = s.size(); if(n <= 1) { return 0; } std::stack<int> stk; stk.push(-1); int max = 0; for(int i = 0 ; i < n ; ++i) { if(s[i] == ')') { if(stk.top() == -1 || s[stk.top()] != '(') { while(stk.size()) { stk.pop(); } } else if(s[stk.top()] == '(') { stk.pop(); max = std::max(max,i-stk.top()); continue; } } stk.push(i); } return max; } };

四:不同路径

class Solution { public: int uniquePaths(int m, int n) { if(!m || !n) { return 0; } std::vector<std::vector<int>> dp(n,std::vector<int>(m)); for(int i = 0 ; i < n ; ++i) { for(int j = 0 ; j < m ; ++j) { if(!i && !j) { dp[i][j] = 1; } else if(!i || !j) { if(!i) { dp[0][j] = 1; } else { dp[i][0] = 1; } } else { dp[i][j] = dp[i-1][j] + dp[i][j-1]; } } } return dp[n-1][m-1]; } };

五:完全平方数

class Solution { public: int numSquares(int n) { std::vector<int> dp(n+1,n+1); dp[0] = 0; for(int i = 1 ; i <= n ; ++i) { for(int j = 1; j*j <= i ; ++j) { dp[i] = std::min(dp[i],dp[i-j*j]+1); } } return dp[n]; } };

六:只出现一次的数字

class Solution { public: int singleNumber(vector<int>& nums) { int ret = 0; for(auto&num : nums) { ret ^= num; } return ret; } };

七:多数元素

class Solution { public: int majorityElement(vector<int>& nums) { int ret = 0; int max = 0; for(auto& num : nums) { if(ret != num) { if(max == 0) { ret = num; max = 1; } else { --max; } } else { ++max; } } return ret; } };

八:颜色分类

class Solution { public: void sortColors(vector<int>& nums) { int n = nums.size(); int zero = 0; int one = 0; for(int i = 0 ; i < n ; ++i) { if(nums[i] == 0) { std::swap(nums[zero],nums[i]); if(one > zero) { std::swap(nums[one],nums[i]); } ++zero; ++one; } else if(nums[i] == 1) { std::swap(nums[i],nums[one]); ++one; } } } };

九:寻找重复数

class Solution { public: int findDuplicate(vector<int>& nums) { int fast = 0; int slow = 0; while(true) { fast = nums[nums[fast]]; slow = nums[slow]; if(fast == slow) { fast = 0; while(fast != slow) { fast = nums[fast]; slow = nums[slow]; } return fast; } } return 0; } };
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/1/15 3:43:28

Anaconda更换清华源后仍无法安装PyTorch?原因解析

Anaconda更换清华源后仍无法安装PyTorch&#xff1f;原因解析 在深度学习项目启动阶段&#xff0c;最令人沮丧的场景之一莫过于&#xff1a;明明已经配置了国内镜像源&#xff0c;执行 conda install pytorch 却依然卡住、报错或提示“PackagesNotFoundError”。尤其当错误信息…

作者头像 李华
网站建设 2026/1/16 21:48:05

世界的双重底色:悲剧与喜剧的共生图景

世界的双重底色&#xff1a;悲剧与喜剧的共生图景当我们追问“世界是一场悲剧还是喜剧”时&#xff0c;本质上是在探寻人类生存体验的核心底色。这个问题没有绝对唯一的答案——世界既非纯粹的悲剧&#xff0c;也非单纯的喜剧&#xff0c;而是两者相互交织、彼此渗透的共生体。…

作者头像 李华
网站建设 2026/1/22 16:30:30

Jupyter Notebook中运行PyTorch模型?这个Docker镜像全搞定

用一个镜像&#xff0c;打通从代码到 GPU 的最后一公里 在深度学习项目中&#xff0c;你是否经历过这样的场景&#xff1a;好不容易写完模型代码&#xff0c;兴冲冲地运行 import torch&#xff0c;结果却弹出一行红色错误——“libcudart.so not found”&#xff1f;或者团队成…

作者头像 李华
网站建设 2026/1/2 4:50:18

No.173 S7 - 1200与MCGS实现M7120型平面磨床电气控制系统的PLC改造

No.173 S7-1200 MCGS M7120型平面磨床电气控制系统的PLC改造带解释的梯形图接线图原理图图纸&#xff0c;io分配&#xff0c;组态画面在自动化控制领域&#xff0c;对传统设备进行PLC改造是提升设备性能与可靠性的重要手段。今天咱们就来聊聊M7120型平面磨床电气控制系统借助S7…

作者头像 李华