leetcode241
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的更多相关文章
- [Swift]LeetCode241. 为运算表达式设计优先级 | Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
- LeetCode241——Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
- leetcode241 为运算表达式设计优先级
class Solution(object): def diffWaysToCompute(self, input): """ :type input: str :rty ...
- Leetcode241.Different Ways to Add Parentheses为运算表达式设计优先级
给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输入: "2-1 ...
随机推荐
- 使用java代码打开特定网页
第一种方法的代码如下所示: import java.io.File; public class Test04 { public static void main(String[] args) { // ...
- 常用的CDN 链接 http://cdn.code.baidu.com/ http://www.bootcdn.cn/
http://cdn.code.baidu.com/ http://developer.baidu.com/wiki/index.php?title=docs/cplat/libs ★新浪CDN,感觉 ...
- 分享知识-快乐自己:Spring线程池配置
Spring通过ThreadPoolTaskExecutor实现线程池技术,它是使用jdk中的Java.util.concurrent.ThreadPoolExecutor进行实现. Spring 配 ...
- 163的Ubuntu apt镜像源
在/etc/apt/sources.list靠前的地方加入即可. 说明:http://mirrors.163.com/.help/ubuntu.html #15.10 wily, 15.04 vivi ...
- poj 2513 欧拉图/trie
http://poj.org/problem?id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submi ...
- java maven、springmvc、mybatis 搭建简单Web项目学习笔记
前言: 空余的时间,学学 Java,没准哪天用的到: 环境搭建折腾了好几天,总算搞顺了,也做个学习笔记,以防后面会忘记: 一.安装文件及介绍 JDK:jdk1.8.0 77 eclipse-maven ...
- vue组件父子组件传递引用类型数据
今天在写分页功能时,发现父子组件传值时,子组件监听不到父组件中数据的变化,传递的是一个引用类型的数据 其原因是引用类型共用一个内存地址,父子组件用的是同一个对象,故子组件监听不到变化,此时就需要做一个 ...
- LeetCode OJ:Combination Sum III(组合之和III)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- @angular/cli项目构建--路由3
路由定位: modifyUser(user) { this.router.navigate(['/auction/users', user.id]); } 路由定义: {path: 'users/:i ...
- New Concept English three (50)
31 39 The New Year is a time for resolutions. Mentally, at least, most of us could compile formidabl ...