leetcode241
2024-10-20 03:35:34
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
实现:
最新文章
- 对js中this的一点点理解
- 短视频APP+不同类型社交应用发展分析+化妆品电商
- 解决Odoo日期(时间)无效的问题 [转]
- 基于Twemproxy的Redis集群方案
- db.properties
- ADO.NET笔记——SQL注入攻击
- ffmpeg windows 雪花声解决方法
- hdu 5087 Revenge of LIS II
- LoadRunner学习知多少--IP欺骗使用
- Kotlin入门第四课:简单工厂模式
- java线程池原理及实现方式
- ROS探索总结(十八)——重读tf
- SVN 通过Shell Bash 获取项目Reversion号码
- RF新手常见问题总结
- Python中使用cx_Oracle调用Oracle存储过程
- git步骤
- HDFS知识点总结
- rabbitmq使用(三)
- P3146 [USACO16OPEN]248 &; P3147 [USACO16OPEN]262144
- 在linux 中wget 无法解析主机
热门文章
- JAVA 中的集合框架
- 微信小程序申请。很蛋疼的流程。
- 关于sublime text 3 pylinter的错误提示
- stacktach和ceilometer
- 《Advanced Bash-scripting Guide》学习(五):检查一个可执行文件是否存在
- 解决:easygui.msgbox(";Hello there!";)报错:Tcl_Init error: Can&#39;t find a usable init.tcl in the following directories问题的解决
- mac中的echo颜色输出
- JS开发页面小组件:table组件
- 20165210 Java第二次实验报告
- BEC translation exercise 4