• 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 Studio 工具栏添加图标
    Spring 概述
    Spring的一种拦截器SimpleUrlHandlerMapping
    Spring注解@Resource和@Autowired区别对比
    关于spring中注解和xml混合使用
    MyEclipse快捷键大全(绝对全)
    技术简历
    计算坐标中心点
    Ideal常用 快捷键
    Ideal 报错之 Class ** is never used 解决办法
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8760366.html
Copyright © 2020-2023  润新知