Accepted C# Solution(Leverage the others)


  • 0
    M
    public IList<int> DiffWaysToCompute(string input) {
            if (string.IsNullOrWhiteSpace(input)) return null;
            var result = new List<int>();
    
            for (int i = 1; i < input.Length - 1; i++)
            {
                if (input[i] == '+' || input[i] == '*' || input[i] == '-')
                {
                    var left = input.Substring(0, i);
                    var right = input.Substring(i + 1);
                    foreach (var l in DiffWaysToCompute(left))
                    {
                        foreach (var r in DiffWaysToCompute(right))
                        {
                            switch (input[i])
                            {
                                case '+':
                                    result.Add(l + r);
                                    break;
                                case '*':
                                    result.Add(l * r);
                                    break;
                                case '-':
                                    result.Add(l - r);
                                    break;
                            }
                        }
                    }
                }
            }
            if(!result.Any()) result.Add(int.Parse(input));
            return result;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.