evaluate-reverse-polish-notation

Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are+,-,*,/. Each operand may be an integer or another expression.
Some examples:
[“2”, “1”, “+”, “3”, ““] -> ((2 + 1) 3) -> 9
[“4”, “13”, “5”, “/”, “+”] -> (4 + (13 / 5)) -> 6

下面代码在牛客网没有通过,但是感觉逻辑是对的,在vs里也输入了几个例子进行了测试,不知道没有通过的原因是不是我写的太繁琐了。

思路:定义一个栈存放数字,如果遇到运算符,就取栈最上层的两个数进行运算,然后再将运算结果压入栈。

代码:

class Solution {
public:
    int evalRPN(vector<string> &tokens) {
        if (tokens.size()<=0)
            return 0;
        stack<int> st;
        int a = 0, b = 0, res = 0;
        for (int i = 0; i < tokens.size(); ++i) {
            string cur = tokens[i];
            if (!isoperator(tokens[i]) && !isnumber(tokens[i]))
                return 0;
            if (isnumber(cur))
                st.push(strtoint(cur));
            else if (isoperator(cur)) {
                b = st.top();//先取到的是第二个操作数
                st.pop();
                a = st.top();
                st.pop();
                res = computab(a, b, cur);
                st.push(res);
            }

        }
        return res;
    }
    //执行运算符的操作
    int computab(int a, int b, string operate) {
        if (operate[0] == '+')
            return a + b;
        else if (operate[0] == '-')
            return a - b;
        else if (operate[0] == '*')
            return a * b;
        else if (operate[0] == '/')
            return a / b;
        return 0;
    }
    //判断是否是运算符
    bool isoperator(string x) {
        if (x.size() == 1 && (x[0] == '+' || x[0] == '-' || x[0] == '*' || x[0] == '/'))
            return true;
        else
            return false;
    }
    //判断是否是数字
    bool isnumber(string x) {
        if (x.size() <= 0)
            return false;
        for (int i = 0; i < x.size(); ++i) {
            if (x[i] >= '0' && x[i] <= '9')
                continue;
            else
                return false;
        }
        return true;
    }
    //把字符串里的数字转换成int
    int strtoint(string x) {
        if (x.size() <= 0)
            return 0;
        int res = x[0] - '0';
        for (int i = 1; i < x.size(); ++i) {
            res = res * 10 + (x[i] - '0');
        }
        return res;
    }
};



字符串里的数字转换成int的两种写法
1、一位一位转换

int strtoint(string x) {
        if (x.size() <= 0)
            return 0;
        int res = x[0] - '0';
        for (int i = 1; i < x.size(); ++i) {
            res = res * 10 + (x[i] - '0');
        }
        return res;
    }

2、用stringstream

    stringstream ss;
    ss << str;
    int temp;
    ss >> temp;//注意这里别写成temp>>ss
    cout << temp << endl;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里是 `evaluate-hand` 函数的实现: ``` (defn evaluate-hand [hand community-cards] (let [all-cards (concat hand community-cards) sorted-cards (sort-by :rank all-cards) flush-suit (->> all-cards (group-by :suit) (sort-by #(count (val %))) last first :suit) straight (some #(apply = (map :rank %)) (partition 5 1 sorted-cards)) straight-flush (and flush-suit straight)] (cond->> {:hand all-cards} straight-flush (assoc :rank :straight-flush) flush-suit (assoc :rank :flush) straight (assoc :rank :straight) :else (let [by-rank (group-by :rank all-cards) counts (map count by-rank) max-count (apply max counts)] (cond->> {:rank :high-card} (= max-count 2) (assoc :rank :pair) (= max-count 3) (assoc :rank :three-of-a-kind) (= max-count 4) (assoc :rank :four-of-a-kind) (and (= max-count 2) (= (count (distinct counts)) 2)) (assoc :rank :two-pair) :else (let [sorted-counts (reverse (sort counts)) kickers (map first (filter #(= (second %) 1) (partition-all 2 sorted-counts)))] (cond (= max-count 1) (assoc :rank :high-card :kickers kickers) (= max-count 3) (assoc :rank :full-house :kickers kickers) :else (assoc :rank :three-of-a-kind :kickers kickers))))))) ``` 该函数接收两个参数,一个是表示手牌的列表 `hand`,一个是表示公共牌的列表 `community-cards`。在该函数中,首先将手牌和公共牌合并成一副牌,并将其按照牌面大小排序。 然后,通过一系列判断,可以判断出当前牌型的大小,并将其保存在一个字典中,其中包括了牌型和可能的“踢牌”(如果有的话)。最终,这个字典会被返回。 这个函数并不是最优的实现方法,但是足以用于简单的德州扑克游戏。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值