
算法坑点
文章平均质量分 62
一打雪碧
这个作者很懒,什么都没留下…
展开
-
PATA1074 Reversing Linked List(测试点1、5)
原题链接PATA1074 Reversing Linked List 本题不难,但是还是出了点小问题。题意是,每k个反转,最后不足k个,保持原样原来代码是#include <bits/stdc++.h>using namespace std;struct Node{ int r; int data,address,nextaddress; Node():r(1000000){} bool operator < (const Node &a.原创 2021-03-06 20:05:12 · 455 阅读 · 2 评论 -
PAT乙级1068 万绿丛中一点红(测试点3、测试点5)
原题链接1068 万绿丛中一点红这道题,其实我觉得题目可以描述的再精确一点。我一开始就有点困惑原题要求:要求你找出万绿丛中的一点红,即有独一无二颜色的那个像素点,并且该点的颜色与其周围 8 个相邻像素的颜色差充分大。首先很明确的一点是,是要找出独一无二的那个颜色点。所以,可以使用unorderde_map < int,int > 在读入数据时就统计各个颜色的出现次数,这样在遍历时,首先判断当前颜色是否出现过大于一次,如果是,则当前颜色肯定不是答案,直接跳过当前颜色。其次就是题目说原创 2021-03-04 11:22:36 · 927 阅读 · 2 评论 -
PAT甲级A11411141 PAT Ranking of Institutions超时问题
原题链接添加链接描述思索一番,该题还是写一篇文章,因为对于我帮助挺大的,其实也是困扰了半天。直观思路本题,最直观的思路是,使用unordered_map < string,double > 建立学校到成绩的映射,使用unordered_map < string,int > cnt建立从学校到人数的映射。使用vector < string > total 来存储下所有出现过的学校。代码如下#include <bits/stdc++.h>using原创 2021-02-27 21:41:42 · 193 阅读 · 0 评论 -
刷pat过程中的坑点
开贴记录刷pat过程中困扰我很久的坑点。2021/2/22pat甲级1061 Dating 以为两个字符相加结果是串,比如以为’0’+'5’为”05“,其实是‘e’,因为‘0’的ASC码值为48,‘5’的ASC码值为53,ASC码值为101的字符恰好为‘e’。#include <iostream>#include <string>using namespace std;int main(){ string ans; char ch = '0';//定义原创 2021-02-22 16:33:25 · 150 阅读 · 0 评论 -
使用string时,不要在一行里面加两个字符
在做pat甲级1061 Dating时,出现了问题,就是有几个点是错误。后来发现,原来是有一行代码企图在一行里面将两个字符常量加到一个string中。示例如下#include <iostream>#include <string>using namespace std;int main(){ string ans; ans += '0'; ans += '5'; cout<<ans; return 0;}//运原创 2021-02-22 16:27:35 · 148 阅读 · 0 评论 -
PAT乙级1027 打印沙漏(测试点0、3格式错误)
1027 打印沙漏坑点,天坑,每一行的字符后面是没有空格的。#include <iostream>#include <string>#include <vector>#include <unordered_map>using namespace std;int main(){ int cur = 3; vector<int> nums; unordered_map<int,int> mp;原创 2021-02-21 21:19:56 · 384 阅读 · 1 评论 -
PAT乙级1018 锤子剪刀布(测试点1、2、4)
原题链接1018 锤子剪刀布本题本来是一道简单题,但是我竟然提交了不下十次,就是有三个点是错误的,即测试点1、2、4。这也说明了思路是正确的。其实问题出在了输出使得各自获胜次数最多的手势上面。自定义测试样例1C C按照题目意思,这种情况为进行了一次交锋,并且是平局,也就是说甲和乙都没有胜利,此时按照题意,在第三行应该输出的是B B,因为甲和乙都没有赢,即对于每个人来说,B、C、J三种手势,每个手势下赢的次数都是0,此时都应该输出B。原代码为#include <iostream>原创 2021-02-20 20:10:27 · 1119 阅读 · 6 评论 -
使用max、min函数必须得是相同类型的数据进行比较
在做力扣575. Distribute Candies时,出现一个错误class Solution {public: int distributeCandies(vector<int>&type) { unordered_map<int,bool> vis; int cnt=0; for(int i=0;i<type.size();i++){ if(!vis[type[i]]){原创 2021-01-31 10:47:16 · 887 阅读 · 0 评论 -
注意int类型数据相加的溢出
1、在想不到的地方出了int相加溢出的错误。在做力扣228. 汇总区间时,出现了这个错误。代码如下class Solution {public: vector<string> summaryRanges(vector<int>& nums) { if(!nums.size()) return {}; vector<string> ans; for(int i=0;i<nums.size()原创 2021-01-23 17:45:30 · 1458 阅读 · 0 评论 -
vector的注意点
vector是STL中的序列型容器,好用,看作是变长数组。但是使用它需要注意几点1、不可以在长度为0时进行数组下标的操作想了一会不知道用什么标题来形容接下来的观点。#include <iostream>#include <vector>using namespace std;int main(){ vector<int> ans; ans[0]=9; return 0;}上述代码段定义了一个vector容器ans,并尝试直接原创 2021-01-23 16:14:43 · 360 阅读 · 0 评论 -
关于unordered_map中元素的放置顺序
今天做力扣一道简单的字符串的题目时,使用了无序图来映射int与string的关系。原题链接917. Reverse Only Letters917. Reverse Only LettersGiven a string S, return the “reversed” string where all characters that are not a letter stay in the same place, and all letters reverse their positions.Exa原创 2021-01-20 16:39:02 · 7379 阅读 · 7 评论 -
必须要注意的算法坑点
第一,在遍历数组时尤其是在for()循环的语句块中要改变i的值时,一定要注意在for()循环的三个语句中是否有必要写变量自增与自减的语句。class Solution {public: int lengthOfLastWord(string s) { int ans=0; for(int i=s.size()-1;i>=0; ){//此处空着,在循环体内写变量变化的语句 if(s[i]==' '){原创 2021-01-07 20:32:42 · 226 阅读 · 0 评论