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. SpringMVC中使用ModelAndView遇到的问题

    本文记录我在SpringMVC中使用ModelAndView,添加模型数据到ModelAndView中时遇到的问题: 1.jsp页面用EL表达式来获取值时直接显示EL表达式,JSP不解析EL表达式: ...

  2. Windows 安装Mysql8.0 绿色包

    〇.准备: MySQL8.0 Windows zip包下载地址:https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.11-winx64.zip 环 ...

  3. 如何做好App的测试工作

    记得刚开始接触app测试时,可谓是一脸懵状,拿到一个功能不知道该测些什么,会因为测试范围确认不足.测试点考虑不全等导致线上问题,吃一堑才会长一智,栽过几次坑后就学会了如何避免.现总结App测试点如下, ...

  4. Mysql时间戳函数和ip转换函数

    Mysql中对于unix时间戳的转换还是挺方便的, 1.转换为时间戳 select unix_timestamp('2013-07-15 10-06-07') 如果参数为空,则为当前时间 2.转换为时 ...

  5. IDT 查询 hana SQL 聚合问题。

    因为业务需要,用HANA的数据做成DASHBOARD.工厂运营概况.结果发现奇怪的问题.明明是一个类型的但是不会聚合.(数据量特别大,一个月的应该就一条,但是有几千条做不下去.) 比如车辆类型是 焊装 ...

  6. eclipse提交Git时忽略文件

    为了避免每次使用都有百度查找,自己保存一份,方便使用. 实现方式: Eclipse切换到Navigator视图,找到.gitignore文件(如果是maven项目,一般找作为modules的项目的.g ...

  7. Spring_总结_04_高级配置(三)_处理歧义

    一.前言 本文承接上一节:Spring_总结_04_高级配置(二)之条件注解@Conditional 我们前面装配bean时,在Spring容器中,都是只有一个bean能匹配所需的结果. 如果有多个b ...

  8. windows下安装 redis并开机自启动

    1,redis官方下载地址:https://redis.io/download,redis 64位下载地址:https://github.com/ServiceStack/redis-windows, ...

  9. DECLARE_MESSAGE_MAP用法

    DECLARE_MESSAGE_MAP( ) 说明:  你的程序中的每一个CCmdTarget的派生类都可以提供一个消息映射以处理消息.在你的类声明的末尾使用DECLARE_MESSAGE_MAP宏. ...

  10. UVA 11605 Lights inside a 3d Grid

    #include <iostream> #include <stdio.h> #include <cstring> #include <math.h> ...