LINTCODE——计算最大值
来源:互联网 发布:网络管理规章制度 编辑:程序博客网 时间:2024/06/16 14:34
LINTCODE——计算最大值
给一个字符串类型的数字, 写一个方法去找到最大值, 你可以在任意两个数字间加 + 或 *
您在真实的面试中是否遇到过这个题? Yes
样例
eg1:给出 str = 01231, 返回 10 ((((0 + 1) + 2) * 3) + 1) = 10 我们得到了最大值 10
eg2:给出 str = 891, 返回 73 因为 8 * 9 * 1 = 72 和 8 * 9 + 1 = 73, 所以73是最大值
这个题目的题意有一点点小问题,例如eg2:891,我如果是8*(9+1)等于80大于73啊,所以题目的意思应该是添加运算符之后,按照从左到右的顺序计算数值,就是eg1的形式,自动添加小括号,那么题目就变得简单多了,利用dfs每调用一次,计算个数值,然后再次调用dfs就好了,直到str的末尾
class Solution {private: int res = 0;public: /* * @param : the given string * @return: the maximum value */ int calcMaxValue(string &str) { // write your code here if(str.empty()) return 0; int n = str.size(); dfs(str, 1,str[0]-'0'); return res; } void dfs(const string &str, int index,int ans) { if(index == str.size()) { //只取最大值 res = max(res,ans); return; } dfs(str, index+1,str[index]-'0'+ans ); dfs(str, index+1, (str[index]-'0')*ans); }};
阅读全文
0 0
- LINTCODE——计算最大值
- 计算最大值-LintCode
- lintcode滑动窗口最大值
- lintcode-滑动窗口的最大值
- LintCode:滑动窗口的最大值
- lintcode,滑动窗口的最大值
- 滑动窗口的最大值-LintCode
- 替换为右侧最大值-LintCode
- MapReduce编程基础(二)——数值概要(计算最大值、最小值、平均值)
- PHP——计算最大值和平均值(关键字重复引起的错误)
- 计算最大值最小值
- 计算最大值和最小值
- mapReduce计算 最大值-----
- lintcode——子树
- Lintcode——中位数
- Lintcode—跳跃游戏
- 最大间距—LintCode
- 快速幂—LintCode
- 助你在精进技术的道路上一臂之力
- switch default 小计
- 使用kotlin
- LockSupport解析与使用
- React学习笔记_realword
- LINTCODE——计算最大值
- HBase各版本对Hadoop版本的支持情况
- HDU2819 Swap
- java递归算法中的排列组合问题及排列组合去重
- 删除了Android Private Libraries之后可重新添加
- MySql数据库中建立索引
- HDU4764 Stone (巴什博奕)
- vscode 在当前文件位置打开控制台
- DMTF 关于 CIM-XML 的幻灯片介绍