标签:
题目描述:public class Solution { public IList<int> DiffWaysToCompute(string input) { List<int> result = new List<int>(); if (input == null || input.Length == 0) { return result; } for (int i = 0; i < input.Length; i++) { char c = input[i]; if (c != ‘+‘ && c != ‘-‘ && c != ‘*‘) { continue; } var left = DiffWaysToCompute(input.Substring(0, i)); var right = DiffWaysToCompute(input.Substring(i + 1, input.Length- i - 1)); for (var j = 0;j < left.Count; j++) { for (var k = 0;k < right.Count; k++) { if (c == ‘+‘) { result.Add(left[j] + right[k]); } else if (c == ‘-‘) { result.Add(left[j] - right[k]); } else if (c == ‘*‘) { result.Add(left[j] * right[k]); } } } } if (result.Count == 0) { result.Add(int.Parse(input)); } return result; } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
LeetCode -- Different Ways to Add Parentheses
标签:
原文地址:http://blog.csdn.net/lan_liang/article/details/48575941