leetcode每日刷题计划-简单篇day2

时间:2022-05-24 20:52:29

今天数模比赛爆肝&操作系统大作业

脖子疼orz先把题过了保证flag不倒。。个别细节回头看吧

Num 13 罗马数字转整数 Roman to Integer

一遍提交过,开始编译出了点问题

具体:最开始忘了if后面的(a+1)需要括号

strlen(s)不可用,这个回头看一下

s.length()是可用的

不知道有没有简单代码,困到懵逼先码

class Solution {
public:
bool pd=false;
int num(char*a)
{
pd=false;//在true的时候减法
if(*a=='I')
{
if((a+)!=NULL && *(a+)=='V')
{
pd=true;
return ;
}
//if((a+1)!=NULL && *(a+1))=='X')
if((a+)!=NULL && *(a+)=='X')
{
pd=true;
return ;
}
else
return ;
}
else if(*a=='X')
{
if((a+)!=NULL && *(a+)=='L')
//if(a+1!=NULL && *(a+1))=='L')
{
pd=true;
return ;
}
if((a+)!=NULL && *(a+)=='C')
//if(a+1!=NULL && *(a+1))=='C')
{
pd=true;
return ;
}
else
return ;
}
else if(*a=='C')
{
if((a+)!=NULL && *(a+)=='D')
//if(a+1!=NULL && *(a+1))=='D')
{
pd=true;
return ;
}
if((a+)!=NULL && *(a+)=='M')
//if(a+1!=NULL && *(a+1))=='M')
{
pd=true;
return ;
}
else
return ;
}
else if(*a=='V')
return ;
else if(*a=='L')
return ;
else if(*a=='D')
return ;
else if(*a=='M')
return ;
cout<<"en?"<<endl;
return -;
}
int romanToInt(string s) {
pd=false;
int ans=;
int len=s.length();
for(int i=;i<len;i++)
{
if (pd==true)
{
pd=false;
continue;
}
char *a=&s[i];
ans=ans+num(a);
}
return ans;
}
};

 Num 20 有效的括号 Valid Parentheses

题非常简单,一遍过(开始忘了count++,无限循环了不过我没交自己发现的,算过吧)

注意一下stl里面的stack和自己手写习惯的不太一样

pop是void返回

用top完成读取以后pop删除

刚开始括号左右弄反了

class Solution {
public:
bool isValid(string s) {
stack <int> arr;
int count=;
int pd=true;
while(s[count]!='\0')
{
if(s[count]=='(')
{
arr.push();
}
else if(s[count]=='{')
{
arr.push();
}
else if(s[count]=='[')
{
arr.push();
}
else if(s[count]==')')
{
if(arr.empty())
return false;
int a=arr.top();
arr.pop();
if(a!=)
return false;
}
else if(s[count]=='}')
{
if(arr.empty())
return false;
int a=arr.top();
arr.pop();
if(a!=)
return false;
}
else if(s[count]==']')
{
if(arr.empty())
return false;
int a=arr.top();
arr.pop();
if(a!=)
return false;
}
count++;
}
if(!arr.empty())
return false;
return true;
}
};

比较顺利,今天跳了一个用vector的,一周之内补上~