• 241 Different Ways to Add Parentheses


    Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *.
    Example 1
    Input: "2-1-1".
    ((2-1)-1) = 0
    (2-(1-1)) = 2
    Output: [0, 2]
    Example 2
    Input: "2*3-4*5"
    (2*(3-(4*5))) = -34
    ((2*3)-(4*5)) = -14
    ((2*(3-4))*5) = -10
    (2*((3-4)*5)) = -10
    (((2*3)-4)*5) = 10
    Output: [-34, -14, -10, -10, 10]

    详见:https://leetcode.com/problems/different-ways-to-add-parentheses/description/

    Java实现:

    采用分治算法,分治算法的基本思想是将一个规模为N的问题分解为K个规模较小的子问题,这些子问题相互独立且与原问题性质相同,求出子问题的解,就可得到原问题的解。那么针对本题,以操作符为分界,将字符串分解为较小的两个子字符串,然后依次对两个子字符串进行同样的划分,直到字符串中只含有数字。再根据操作符对两端的数字进行相应的运算。

    class Solution {
        public List<Integer> diffWaysToCompute(String input) {
            List<Integer> res = new ArrayList<>();
            for(int i=0;i<input.length();++i){
                char c=input.charAt(i);
                if(c=='+'||c=='-'||c=='*'){
                    List<Integer> left=diffWaysToCompute(input.substring(0,i));
                    List<Integer> right=diffWaysToCompute(input.substring(i+1));
                    for(int j=0;j<left.size();++j){
                        for(int k=0;k<right.size();++k){
                            if(c=='+'){
                                res.add(left.get(j)+right.get(k));
                            }else if(c=='-'){
                                res.add(left.get(j)-right.get(k));
                            }else{
                                res.add(left.get(j)*right.get(k));
                            }
                        }
                    }
                }
            }
            if(res.isEmpty()){
                res.add(Integer.parseInt(input));
            }
            return res;
        }
    }
    

    C++实现:

    class Solution {
    public:
        vector<int> diffWaysToCompute(string input) {
            vector<int> res;
            for(int i=0;i<input.size();++i)
            {
                if(input[i]=='+'||input[i]=='-'||input[i]=='*')
                {
                    vector<int> left=diffWaysToCompute(input.substr(0,i));
                    vector<int> right=diffWaysToCompute(input.substr(i+1));
                    for(int j=0;j<left.size();++j)
                    {
                        for(int k=0;k<right.size();++k)
                        {
                            if(input[i]=='+')
                            {
                                res.push_back(left[j]+right[k]);
                            }
                            else if(input[i]=='-')
                            {
                                res.push_back(left[j]-right[k]);
                            }
                            else
                            {
                                res.push_back(left[j]*right[k]);
                            }
                        }
                    }
                }
            }
            if(res.empty())
            {
                res.push_back(stoi(input));
            }
            return res;
        }
    };
    

     参考:https://www.cnblogs.com/grandyang/p/4682458.html

  • 相关阅读:
    开源项目之Android StandOut(浮动窗口)
    小智慧7
    安卓学习
    asp.net学习Request和Response的其他属性
    bash中的转义
    POJ 1833 排列
    Django点滴(四)ORM对象存取
    POJ 1681 Painter's Problem
    linux2.6.32在mini2440开发板上移植(21)之WebServer服务器移植
    [gkk传智]static与多态及向下向上转型,及多态调用总结
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8760366.html
Copyright © 2020-2023  润新知