标签:
没啥好说的,注意用long(明明题目里说integer,汗)
public class Solution { public boolean isAdditiveNumber(String num) { int length = num.length(); int i = 1; while (i < length && Long.valueOf(num.substring(0, i)) <= Integer.MAX_VALUE) { int j = i + 1; while (j <= length && Long.valueOf(num.substring(i, j)) <= Integer.MAX_VALUE) { if ((num.substring(0, i).length() > 1 && num.substring(0, i).startsWith("0")) || (num.substring(i, j).length() > 1 && num.substring(i, j).startsWith("0"))) { break; } long first = Integer.valueOf(num.substring(0, i)); long second = Integer.valueOf(num.substring(i, j)); if (num.substring(j).length() == 0) { break; } if (isvalid(first, second, num.substring(j))) { return true; } j ++; } i ++; } return false; } public boolean isvalid(long first, long second, String num) { if (num.length() == 0) { return true; } long third = first + second; if (num.length() >= String.valueOf(third).length() && num.substring(0, String.valueOf(third).length()).equals(String.valueOf(third))) { return isvalid(second, third, num.substring(String.valueOf(third).length())); } return false; } }
标签:
原文地址:http://www.cnblogs.com/vision-love-programming/p/4996589.html