avatar
文章
5
标签
1
分类
0

主页
归档
标签
摄影
友链
关于我
921l
主页
归档
标签
摄影
友链
关于我

921l

力扣1281———整数的各位积和之差
发表于2023-09-14
Problem: 1281. 整数的各位积和之差 [TOC] 思路 选取n的每一位分别进行加法和乘法计算,得到最后的乘积结果和加和结果,对结构进行运算得到最终答案 Code[]12345678910111213class Solution {public: int subtractProductAndSum(int n) { int sum=0,mul=1; while(n>0){ sum+=n%10; mul=(n%10)*mul; n=n/10; } return mul-sum; }};
力扣121———买卖股票的最佳时机
发表于2023-09-14
Problem: 121. 买卖股票的最佳时机 [TOC] 思路 开始想用暴力,过不去;解法是使用一次遍历每次使用mi记录最小的节点,并不断去更新这个最小的值;每次使用Mx记录在目前最小的值下的可获得的最大利润,并不断更新; Code[]123456789101112class Solution {public: int maxProfit(vector<int>& prices) { int mi=1e9,mx=0; for(int i=0;i<prices.size();i++){ mx=max(mx,prices[i]-mi); mi=min(prices[i],mi); } return mx; }};
力扣2682———模拟
发表于2023-08-09
Problem: 2682. 找出转圈游戏输家 [TOC] 思路 使用count作为步数来进行计算,当数组中不存在值为2的数字时,继续循环,因为每次超过N会回到原点所以进行取余,某个数字出现第二次时,跳出循环。 Code[]12345678910111213141516171819class Solution {public: vector<int> circularGameLosers(int n, int k) { vector<int> num(n,0); vector<int> ans; int i=0,count=k; while(1){ num[i]++; if(num[i]==2) break; i=(i+count)%n; count+=k; } for(int i=0;i<n;i++){ ...
力扣1186———动态规划解决
发表于2023-08-09
Problem: 1186. 删除一次得到子数组最大和 [TOC] 思路 动态规划 使用二维数组,一维数组里没个均含有不删除一个元素情况下的最大值,和删除了一个元素后的最大值,将着两个最大值进行对比,得到的就是答案的最大值; 递推关系为: 12345for(int i=1;i<n;i++){ dp[i][0]=max(dp[i-1][0],0)+arr[i]; dp[i][1]=max(dp[i-1][1]+arr[i],dp[i-1][0]); mx=max(mx,max(dp[i][0],dp[i][1]));} dp[i][0]确定以i为结尾的前i个子数组的最大和;dp[i][1]确定删除一个元素(被删除的元素是现在这个为dp[i-1][0],被删除元素为之前的某一个为dp[i-1][1]+arr[i])之后的前i个子数组的最大和。 确定以后与之前选取的最大值进行比较,选取现在值最大的数字 Code[]1234567891011121314151617class Solution {public: int maxi ...
Hello World
发表于2021-11-03
Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub. Quick StartCreate a new post1$ hexo new "My New Post" More info: Writing Run server1$ hexo server More info: Server Generate static files1$ hexo generate More info: Generating Deploy to remote sites1$ hexo deploy More info: Deployment
1
avatar
921l
行则将至
文章
5
标签
1
分类
0
Follow Me
公告
This is my Blog
最新文章
力扣1281———整数的各位积和之差2023-09-14
力扣121———买卖股票的最佳时机2023-09-14
力扣2682———模拟2023-08-09
力扣1186———动态规划解决2023-08-09
Hello World2021-11-03
标签
算法
归档
  • 九月 20232
  • 八月 20232
  • 十一月 20211
网站资讯
文章数目 :
5
最后更新时间 :
©2021 - 2023 By 921l
框架 Hexo|主题 Butterfly