Expression Add Operators
来源:互联网 发布:闪电邮mac版 编辑:程序博客网 时间:2024/05/21 18:40
Given a string that contains only digits 0-9
and a target value, return all possibilities to addbinary operators (not unary) +
, -
, or *
between the digits so they evaluate to the target value.
Examples:
"123", 6 -> ["1+2+3", "1*2*3"] "232", 8 -> ["2*3+2", "2+3*2"]"105", 5 -> ["1*0+5","10-5"]"00", 0 -> ["0+0", "0-0", "0*0"]"3456237490", 9191 -> []class Solution {public: vector<string> addOperators(string num, int target) { int n = num.length(); vector<string> result; visit(num, n, 0, 0, 0, "", target, result); return result; }private: void visit(string &num, int n, int pos, long cur, long prev, string buf, int target, vector<string> &result) { if (pos == n) { if (cur == target) { result.push_back(buf); } return; } for (int i = pos; i < n; i++) { if (num[pos] == '0' && i > pos) { break; } string temp = num.substr(pos, i-pos+1); long val = stol(temp); if (pos == 0) { visit(num, n, i+1, val, val, temp, target, result); } else { visit(num, n, i+1, cur+val, val, buf+"+"+temp, target, result); visit(num, n, i+1, cur-val, -val, buf+"-"+temp, target, result); visit(num, n, i+1, cur-prev+prev*val, prev*val, buf+"*"+temp, target, result); } } }};
0 0
- LeetCode:Expression Add Operators
- 【LeetCode】Expression Add Operators
- Expression Add Operators【282】
- Expression Add Operators
- Expression Add Operators
- Expression Add Operators
- Expression Add Operators -- leetcode
- leetcode282 : Expression Add Operators
- Leetcode -- Expression Add Operators
- LeetCode -- Expression Add Operators
- LeetCode Expression Add Operators
- Expression Add Operators
- leetcode:Expression Add Operators
- 282. Expression Add Operators
- Expression Add Operators
- 282. Expression Add Operators
- Expression Add Operators
- Expression Add Operators
- JAVA笔记---反射
- 我的mac使用备忘
- 浅谈数据库设计原则(适合新人)
- JAVA笔记---线程同步
- Java环境配置
- Expression Add Operators
- Dalvik 和 Java 虚拟机的区别
- 单例在ObjectiveC中的正确打开方式
- Tomcat环境配置
- ZeroMQ 的java 插件
- 蓝桥杯--十六进制转十进制&十进制转十六进制&十六进制转八进制
- scala 对比java 贴
- oracle面向对象编程的多态性
- NSNumber各类型包装转换