Check in (14)

The data structure class is written in reverse Polish style, and this topic can be simplified because it only needs +-two operations.
This question is still a bit boring with empty characters. If you feel that it is not easy to write up, it is actually because you have not remembered the knowledge of data structure.
This is someone else's code that you can understand, using the lexical analysis of the principle of compilation.
I haven't learned it yet, but it seems that his logic is from top to bottom, rather than the usual way of thinking.
(It is recommended to write silently.

class Solution {
    
    
public:
    long long calculate(string s) {
    
    
        idx  = 0,len= s.size();//初始化序号为0,并且计算其字符串的长度
        this->s = s;//将私有的s设置为s
        long long tmp = findElem();//寻找第一个计算的元素
        while(idx != len){
    
    //当还没有遍历所有的元素的时候,继续向下操作
            operate(tmp);//抽象操作
        }
        return tmp;//将答案返回
    }
private:
    int idx,len;//定义序号和长度
    string s;//所以上述的s必须要使用this指针,否则将会造成值无法进行传递
    long long findElem(){
    
    //寻找下一个元素
        // 寻找当前准备参与运算的数
        while (idx < len && s[idx] == ' ')
            ++idx;
        if (s[idx] == '(')
            return calBrackets();//若遇见了左边括号,那么先计算括号中的元素值
        long long tmp = 0;//中间变量
        if (s[idx] == '+' || s[idx] == '-')//若判定运算为加减运算
            return operate(tmp);//继续操作
        while (idx != len && isdigit(s[idx]))//若该数字是连续的多位数数字,则进行如此处理
            tmp = tmp*10 + s[idx++] -'0';
        return tmp;//返回值
    }
    int calBrackets(){
    
    
        // 计算括号内的值
        ++idx; //'('
        long long tmp = findElem();
        while (idx < len && s[idx]!= ')'){
    
    
            operate(tmp);//递归到括号内进行操作
        }
        ++idx; // ')'
        return tmp;//返回值
    }
    long long operate(long long& tmpAns){
    
    
        // 寻找当前的运算符和下一个运算数,并且将该运算作用于当前结果
        while (idx < len && s[idx] == ' ')
            ++idx;
        if (s[idx] == '+'){
    
    
            ++idx; // '+'
            tmpAns += findElem();
        }
        else if (s[idx] == '-'){
    
    
            ++idx; // '-'
            tmpAns -= findElem();
        }
        return tmpAns;
    }
};

So is the current level just enough to write simple questions? Awkward laugh
https://leetcode-cn.com/problems/bu-ke-pai-zhong-de-shun-zi-lcof/submissions/

class Solution {
    
    
public:
    bool isStraight(vector<int>& nums) {
    
    
        int min=14;
        int max=-1;
        int cnt0=0;
        int dex[14]={
    
    0};
        for(int i=0;i<5;i++){
    
    
            if(nums[i]==0){
    
     
                cnt0++;
                continue;
            }
            if(++dex[nums[i]]==2) return false;
            if(min>nums[i]) min=nums[i];
            if(max<nums[i]) max=nums[i];
        }
        int leap=max-min;
        if(leap==4||leap<4&&(leap+cnt0>=4)) return true;
        else return false;
    }
};

On the first day of running, I felt very hypnotic.

Guess you like

Origin blog.csdn.net/weixin_47741017/article/details/114806944