zoukankan      html  css  js  c++  java
  • leetcode 306. Additive Number

    传送门

    306. Additive Number

       My Submissions
    Total Accepted: 10259 Total Submissions: 39906 Difficulty: Medium

    Additive number is a string whose digits can form additive sequence.

    A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two.

    For example:
    "112358" is an additive number because the digits can form an additive sequence: 1, 1, 2, 3, 5, 8.

    1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8
    "199100199" is also an additive number, the additive sequence is: 1, 99, 100, 199.
    1 + 99 = 100, 99 + 100 = 199

    Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid.

    Given a string containing only digits '0'-'9', write a function to determine if it's an additive number.

    Follow up:
    How would you handle overflow for very large input integers?

    Credits:
    Special thanks to @jeantimex for adding this problem and creating all test cases.

    Subscribe to see which companies asked this question

    Submission Details

    37 / 37 test cases passed.
    Status: 

    Accepted

    Runtime: 3 ms

    题解转自:

    http://blog.csdn.net/qq508618087/article/details/51359965

    暴力

    思路: 一个基本的思想就是确定了第一个和第二个数之后, 以后的数就是验证了, 所以只要枚举第一和第二个数, 然后不断验证下面的字符串子串是否是前两个数的和即可. 因为数字可能超出整数的范围, 因此在验证一个数是否是另外两个和的时候, 可以用字符串相加来模拟整数相加. 另外需要注意的是'0'字符, 如果他在子串的首位, 那么他只能以"0"的形式存在.

     1 class Solution {
     2 public:
     3     string add(string s1, string s2)
     4     {
     5         string result;
     6         int flag = 0, i = s1.size()-1, j = s2.size()-1;
     7         while(i >= 0 || j >= 0)
     8         {
     9             int num1 = 0, num2 = 0;
    10             if(i >= 0) num1 = s1[i]- '0' ;
    11             if(j >= 0) num2 = s2[j]- '0' ;
    12             int sum = num1 + num2 + flag;
    13             result.insert(result.begin(), '0' + sum % 10);
    14             flag = sum / 10;
    15             i--;j--;
    16         }
    17         if(flag == 1) result.insert(result.begin(), '1');
    18         return result;
    19     }
    20     
    21     bool check(string num, string num1, string num2)
    22     {
    23         if(num.size()==0) return false;
    24         string sum = add(num1,num2);
    25         for(int i = 0; i < num.size(); i++)
    26         {
    27             string x = num.substr(0, i + 1); 
    28             if(x == sum)
    29             {
    30                 if(i == num.size() - 1) return true;
    31                 return check(num.substr(i + 1), num2, x);
    32             }
    33             if(num[0] == '0') break;
    34         }
    35         return false;
    36     }
    37     
    38     bool isAdditiveNumber(string num) {
    39         int len = num.size();
    40         if(len < 3) return false;
    41         string num1, num2;
    42         for(int i = 0; i < len - 2; i++)
    43         {
    44             num1 = num.substr(0, i + 1);
    45             for(int j = i + 1; j < len - 1; j++)
    46             {
    47                 num2 = num.substr(i + 1, j - i);
    48                 if( check(num.substr( j + 1), num1, num2) ) return true;
    49                 if(num[i + 1] == '0') break;
    50             }
    51             if(num[0] == '0') break;
    52         }
    53         return false;
    54     }
    55 };
  • 相关阅读:
    NX二次开发-测量投影距离
    NX二次开发-弹出选择文件夹对话框
    NX二次开发-获得图纸抑制尺寸的表达式UF_DRF_ask_controlling_exp
    NX二次开发-创建图纸尺寸表达式抑制UF_DRF_add_controlling_exp
    NX文件名与工程图名自动关联
    NX二次开发-删除功能区工具栏UF_UI_remove_ribbon
    NX二次开发-删除经典工具栏UF_UI_remove_toolbar
    NX二次开发-设置功能区工具栏的可见性UF_UI_set_ribbon_vis
    NX二次开发-创建功能区工具栏UF_UI_create_ribbon
    NX二次开发-设置经典工具栏的可见性UF_UI_set_toolbar_vis
  • 原文地址:https://www.cnblogs.com/njczy2010/p/5488605.html
Copyright © 2011-2022 走看看