zoukankan      html  css  js  c++  java
  • [每日一题] leetcode 640. 求解方程

    等号左右两边代码一样,就是+ - 不一样

    将x都移到左边 整数移到右边

    class Solution {
    public:
    
        int change(string str)
        {
            if(str == "0") return 1;
            int len = str.length();
            int ret = 0;
            for(int i = 0; i < len; i++)
            {
                ret = ret * 10 + str[i] - '0';
            }
            return ret;
        }
        string rev(int x)
        {
            if(x == 0) return "0";
            string str = "";
            while(x)
            {
                int tmp = x % 10;
                x /= 10;
                str += '0' + tmp;
            }
            reverse(str.begin(), str.end());
            return str;
        }
    
    
        string solveEquation(string equation) {
            int cnt1 = 0, cnt2 = 0, f = 0, s;
            int len = equation.length();
            string str;
            int i = 0;
            while(i < len)
            {
                s = 0;
    
                str = "0";
                if(equation[i] == '=')
                {
                    f = 1;
                    i++;
                }
                if(equation[i] == '-')
                {
                    s = 1;
                    i++;
                }
                else if(equation[i] == '+') i++;
                if(f == 1)
                {
                    while(equation[i] >= '0' && equation[i] <= '9') str += equation[i++];
                    if(equation[i] == 'x')
                    {
                        if(s == 0) cnt2 -= change(str);
                        else cnt2 += change(str);
                        i++;
                    }
                    else 
                    {
                        if(s == 0) cnt1 += change(str);
                        else cnt1 -= change(str);
                    }
                }
                else
                {
                    while(equation[i] >= '0' && equation[i] <= '9') str += equation[i++];
                    if(equation[i] == 'x')
                    {
                        if(s == 0) cnt2 += change(str);
                        else cnt2 -= change(str);
                        i++;
                    }
                    else 
                    {
                   //     cout << str << endl;
                        if(s == 0) cnt1 -= change(str);
                        else cnt1 += change(str);
                    }
                }
    
    
            }
           // cout << cnt2 << "  " << cnt1 << endl;
            if(cnt2 == 0)
                if(cnt1 == 0) str = "Infinite solutions";
                else str = "No solution";
            else
            {
                str = "x=";
                int tmp = cnt1 / cnt2;
                if(tmp < 0) str += '-', tmp = -tmp;
              // cout << tmp << endl;
                str += rev(tmp);
            }
            return str;
    
    
    
    
    
    
        }
    };
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    ubuntu系统下Python虚拟环境的安装和使用
    jquery访问浏览器本地存储cookie,localStorage和sessionStorage
    前端笔记----jquery入门知识点总结
    jquery事件使用方法总结
    ajax和jsonp使用总结
    用python的TK模块实现猜成语游戏(附源码)
    前端笔记----类型转换display
    Java标识符
    Java的关键字
    java环境变量的配置
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/14741376.html
Copyright © 2011-2022 走看看