计算最大值-LintCode
来源:互联网 发布:多功能双肩背包 知乎 编辑:程序博客网 时间:2024/06/06 09:14
给一个字符串类型的数字, 写一个方法去找到最大值, 你可以在任意两个数字间加 + 或 *
样例:
给出 str = 01231, 返回 10 ((((0 + 1) + 2) * 3) + 1) = 10 我们得到了最大值 10
给出 str = 891, 返回 73 因为 8 * 9 * 1 = 72 和 8 * 9 + 1 = 73, 所以73是最大值
思路:
遍历字符串的每一位,转化为数字num,并将当前的结果res置为max{res*num,res+num},
最后返回res。
#ifndef C719_H#define C719_H#include<iostream>#include<string>using namespace std;class Solution {public: /* * @param : the given string * @return: the maximum value */ int calcMaxValue(string &str) { // write your code here if (str.empty()) return 0; int res = 0; //遍历字符串的每一位,转化为数字num,并将当前的结果res置为max{res*num,res+num},最后返回res for (int i = 0; i < str.size(); ++i) { int num = stoi(str.substr(i, 1)); res = maxVal(res*num, res + num); } return res; } int maxVal(int a, int b) { return a>b ? a : b; }};#endif
阅读全文
0 0
- 计算最大值-LintCode
- LINTCODE——计算最大值
- lintcode滑动窗口最大值
- lintcode-滑动窗口的最大值
- LintCode:滑动窗口的最大值
- lintcode,滑动窗口的最大值
- 滑动窗口的最大值-LintCode
- 替换为右侧最大值-LintCode
- 计算最大值最小值
- 计算最大值和最小值
- mapReduce计算 最大值-----
- lintcode(362)滑动窗口的最大值
- 计算4个数的最大值
- 计算整型数组中的最大值
- 计算两整数的最大值
- 计算两整数的最大值
- 计算两整数的最大值
- 使用遗传算法计算方程的最大值
- 设计一个字符串子串删除函数 将结果子串指针返回 用主程序验证
- URL取值编码问题
- std::map用法
- 12月送书活动
- 注解(1)
- 计算最大值-LintCode
- flex Alert实用弹出确认框
- 基于Eclipse搭建Kettle插件调试环境的两种方法
- GoEasy(version 0.3.8)推送的使用方式
- 1111111111111111111
- 我的android多线程编程之路(1)之经验详解,源码分析
- js数组与字符串的相互转换方法
- 小程序商城--将商品加入购物车缓存
- 在linux上部署报错NoSuchMethodError