> For the complete documentation index, see [llms.txt](https://voyz.gitbook.io/voyz-algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://voyz.gitbook.io/voyz-algorithm/fen-zhi-suan-fa/wei-yun-suan-biao-da-shi-she-ji-you-xian-ji.md).

# 为运算表达式设计优先级

## LeetCode 241. 为运算表达式设计优先级

[241. 为运算表达式设计优先级](https://leetcode-cn.com/problems/different-ways-to-add-parentheses/)

### 题目

```
给定一个含有数字和运算符的字符串，为表达式添加括号，改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含 +, - 以及 * 。

示例 1:

输入: "2-1-1"
输出: [0, 2]
解释: 
((2-1)-1) = 0 
(2-(1-1)) = 2
示例 2:

输入: "2*3-4*5"
输出: [-34, -14, -10, -10, 10]
解释: 
(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
```

### 题解

#### 分治算法

```javascript
var diffWaysToCompute = function(expression) {
    if(!/\D/g.test(expression)) return [parseInt(expression)];
    const res = [];

    for(let i=0;i<expression.length;i++){
        const _symbol = expression[i];
        // 符号位
        if(["+","-","*"].indexOf(_symbol) > -1){
            const left_res = diffWaysToCompute(expression.slice(0,i));
            const right_res = diffWaysToCompute(expression.slice(i+1));

            for(let l=0;l<left_res.length;l++){
                for(let r=0;r<right_res.length;r++){
                    switch (_symbol){
                        case "+":
                            res.push(left_res[l]+right_res[r]);
                            break;
                        case "-":
                            res.push(left_res[l]-right_res[r]);
                            break;
                        case "*":
                            res.push(left_res[l]*right_res[r]);
                            break;
                    }
                }
            }
        }
    }

    return res;
};
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://voyz.gitbook.io/voyz-algorithm/fen-zhi-suan-fa/wei-yun-suan-biao-da-shi-she-ji-you-xian-ji.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
