Calculate Maximum Value

http://www.lintcode.com/en/problem/calculate-maximum-value/?rand=true

public class Solution {
    /*
     * @param : the given string
     * @return: the maximum value
     */
    public int calcMaxValue(String str) {
        // write your code here
        int res = 0;
        for (int i = 0; i < str.length(); i++) {
            int temp = str.charAt(i) - '0';
            if (i == 0) {
                res = temp;
                continue;
            }
            if (temp <= 1 || res <= 1) {
                res += temp;
            } else {
                res *= temp;
            }
        }
        return res;
    }
};

你可能感兴趣的:(Calculate Maximum Value)