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

实现:

最新文章

  1. 对js中this的一点点理解
  2. 短视频APP+不同类型社交应用发展分析+化妆品电商
  3. 解决Odoo日期(时间)无效的问题 [转]
  4. 基于Twemproxy的Redis集群方案
  5. db.properties
  6. ADO.NET笔记——SQL注入攻击
  7. ffmpeg windows 雪花声解决方法
  8. hdu 5087 Revenge of LIS II
  9. LoadRunner学习知多少--IP欺骗使用
  10. Kotlin入门第四课:简单工厂模式
  11. java线程池原理及实现方式
  12. ROS探索总结(十八)——重读tf
  13. SVN 通过Shell Bash 获取项目Reversion号码
  14. RF新手常见问题总结
  15. Python中使用cx_Oracle调用Oracle存储过程
  16. git步骤
  17. HDFS知识点总结
  18. rabbitmq使用(三)
  19. P3146 [USACO16OPEN]248 &amp; P3147 [USACO16OPEN]262144
  20. 在linux 中wget 无法解析主机

热门文章

  1. JAVA 中的集合框架
  2. 微信小程序申请。很蛋疼的流程。
  3. 关于sublime text 3 pylinter的错误提示
  4. stacktach和ceilometer
  5. 《Advanced Bash-scripting Guide》学习(五):检查一个可执行文件是否存在
  6. 解决:easygui.msgbox(&quot;Hello there!&quot;)报错:Tcl_Init error: Can&#39;t find a usable init.tcl in the following directories问题的解决
  7. mac中的echo颜色输出
  8. JS开发页面小组件:table组件
  9. 20165210 Java第二次实验报告
  10. BEC translation exercise 4