public class Solution {
public IList<int> DiffWaysToCompute(string input) {
List<int> ret = new List<int>();
for (int i = ; i < input.Length; i++)
{
if (input[i] == '-' ||
input[i] == '*' ||
input[i] == '+')
{
string part1 = input.Substring(, i);
string part2 = input.Substring(i + );
var part1Ret = DiffWaysToCompute(part1);
var part2Ret = DiffWaysToCompute(part2);
foreach (var p1 in part1Ret)
{
foreach (var p2 in part2Ret)
{
int c = ;
switch (input[i])
{
case '+': c = p1 + p2;
break;
case '-': c = p1 - p2;
break;
case '*': c = p1 * p2;
break;
}
ret.Add(c);
}
}
}
}
if (ret.Count == )
{
ret.Add(int.Parse(input));
}
return ret;
}
}

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

补充一个python的

 class Solution:
def diffWaysToCompute(self, input: str) -> 'List[int]':
re = list()
n = len(input)
for i in range(n):
c = input[i]
if c =='+' or c == '-' or c == '*':
left = input[:i]
right = input[i+:]
for l in self.diffWaysToCompute(left):
for r in self.diffWaysToCompute(right):
if c == '+':
re.append(l + r)
elif c == '-':
re.append(l - r)
elif c == '*':
re.append(l * r)
if len(re) == :
re.append(int(input))
return re

实现:

leetcode241的更多相关文章

  1. [Swift]LeetCode241. 为运算表达式设计优先级 | Different Ways to Add Parentheses

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  2. LeetCode241——Different Ways to Add Parentheses

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  3. leetcode241 为运算表达式设计优先级

    class Solution(object): def diffWaysToCompute(self, input): """ :type input: str :rty ...

  4. Leetcode241.Different Ways to Add Parentheses为运算表达式设计优先级

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

随机推荐

  1. QT 使用QTcpServer QTcpSocket 建立TCP服务器端 和 客户端

    1.  如图客户端连接server后,server发送"hello tcp" 给客户端 2. 实例代码 -----------------------------------  s ...

  2. Linux嵌入式 -- 内核 - proc文件系统

    1. 什么是proc文件系统? 实例:通过 /proc/meminfo,查询当前内存使用情况. 结论:proc文件系统是一种在用户态检查内核状态的机制. 2.Proc文件分类 特点  每个文件都规定了 ...

  3. javaScript中的DOM补充

    一.DOM树 二.DOM节点 DOM 是这样规定的:    整个文档是一个文档节点     每个 HTML 标签是一个元素节点     包含在 HTML 元素中的文本是文本节点     每一个 HTM ...

  4. 云服务器pip下载老失败怎么办?

    pip install -i https://pypi.douban.com/simple django==1.9

  5. Python中的Unicode编码和UTF-8编码

    下午看廖雪峰的Python2.7教程,看到 字符串和编码 一节,有一点感受,结合崔庆才的Python博客 ,把这种感受记录下来: ASCII码:是用一个字节(8bit, 0-255)中的127个字母表 ...

  6. Get UTI (uniform type identifier) and ContentType

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...

  7. Uninstall Office 2016 for Mac

    官方原文:https://support.office.com/en-us/article/Uninstall-Office-2016-for-Mac-eefa1199-5b58-43af-8a3d- ...

  8. vue项目接口地址的定义

    对于接口地址域名我们经常会遇到,那么如何去定义呢: 只要在config/dev.env.js中定义变量NODE_ENV就行啦 在.vue文件中的引用方式如下: 嗯,就是这样简单~~~~

  9. hibernate ORM related

    一.单向关联(unidirectional associations): 1.1.1 Many-to-one Employee.hbm.xml <class name="Employe ...

  10. DedeCMS织梦模板标签调用大全

    本文转载:http://www.mubanzhijia.com/jishujiaocheng/1.html 关键描述调用标签: <meta name="keywords" c ...