zoukankan      html  css  js  c++  java
  • leetcode每日刷题计划-简单篇day2

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

    脖子疼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+1)!=NULL && *(a+1)=='V')
                {
                    pd=true;
                    return 4;
                }  
                //if((a+1)!=NULL && *(a+1))=='X')
                if((a+1)!=NULL && *(a+1)=='X')
                {
                    pd=true;
                    return 9;
                }
                else 
                    return 1;
            }
            else if(*a=='X')
            {
                if((a+1)!=NULL && *(a+1)=='L')
                //if(a+1!=NULL && *(a+1))=='L')
                {
                    pd=true;
                    return 40;
                } 
                if((a+1)!=NULL && *(a+1)=='C')
                //if(a+1!=NULL && *(a+1))=='C')
                {
                    pd=true;
                    return 90;
                } 
                else 
                    return 10;
            }
            else if(*a=='C')
            {
                if((a+1)!=NULL && *(a+1)=='D')
                //if(a+1!=NULL && *(a+1))=='D')
                {
                    pd=true;
                    return 400;
                } 
                if((a+1)!=NULL && *(a+1)=='M')
                //if(a+1!=NULL && *(a+1))=='M')
                {
                    pd=true;
                    return 900;
                } 
                else 
                    return 100;
            }
            else if(*a=='V')
                return 5;
            else if(*a=='L')
                return 50;
            else if(*a=='D')
                return 500;
            else if(*a=='M')
                return 1000;
            cout<<"en?"<<endl;
            return -1;
        }
        int romanToInt(string s) {
            pd=false;
            int ans=0;
            int len=s.length();
            for(int i=0;i<len;i++)
            {
                if (pd==true)
                {
                    pd=false;
                    continue;
                }
                char *a=&s[i];
                ans=ans+num(a);
            }
            return ans;
        }
    };
    View Code

     Num 20 有效的括号 Valid Parentheses

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

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

    pop是void返回

    用top完成读取以后pop删除

    刚开始括号左右弄反了

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

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

    时间才能证明一切,选好了就尽力去做吧!
  • 相关阅读:
    python_day3
    python-day2
    python-day1
    【收集】安卓手机在市场占比的网址查找
    while循环
    switch多选择结构
    if选择结构
    顺序结构
    Scanner:求和 、求平均值
    Scanner:用户交互
  • 原文地址:https://www.cnblogs.com/tingxilin/p/10693280.html
Copyright © 2011-2022 走看看