leetcode-string-easy-696-788

每天坚持写几道leetcode,希望几个月后我就不再是小白
今天的题目是696,788

题目:696. Count Binary Substrings

描述:Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0’s and 1’s, and all the 0’s and all the 1’s in these substrings are grouped consecutively.

Substrings that occur multiple times are counted the number of times they occur.
例子:

Input: "00110011"
Output: 6
Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01".

Notice that some of these substrings repeat and are counted the number of times they occur.

Also, "00110011" is not a valid substring because all the 0's (and 1's) are not grouped together.

代码:

int Solution696::countBinarySubstrings(string s)
{
    int num = 0;
    vector<int> group;
    int index = 0;
    for (int i = 1; i < s.size(); ++i)
    {
        if (s[i] != s[i - 1] && i != s.size() - 1)
        {
            group.push_back(i - index);
            index = i;
        }
        else if (s[i] != s[i - 1] && i == s.size() - 1)//i和index不在同一个片段
        {
            group.push_back(i - index);//先把i前面的片段长度加入
            group.push_back(1);//再将i所在的片段加入
        }
        else if (i == s.size() - 1)//i和index在同一个片段
        {
            group.push_back(i - index + 1);
        }
    }
    for (int i = 1; i < group.size(); ++i)
    {
        num += min(group[i], group[i - 1]);
    }
    return num;
}

解题边界条件:
1. 最后一个元素与前一个元素相同
2. 最后一个元素与前一个元素不同


题目:788. Rotated Digits

描述:X is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from X. Each digit must be rotated - we cannot choose to leave it alone.

A number is valid if each digit remains a digit after rotation. 0, 1, and 8 rotate to themselves; 2 and 5 rotate to each other; 6 and 9 rotate to each other, and the rest of the numbers do not rotate to any other number and become invalid.

Now given a positive number N, how many numbers X from 1 to N are good?
例子:

Input: 10
Output: 4
Explanation: 
There are four good numbers in the range [1, 10] : 2, 5, 6, 9.
Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.

代码:

bool Solution788::isDiff(int x)
{
    int flag = 0;
    while (x > 0)
    {
        int v = x % 10;
        if(v == 0 || v == 1 || v == 8){}
        else if (flag == 0 && v == 2 || v == 5 || v == 6 || v == 9)flag = 1;
        else if(v == 3 || v == 4 || v == 7) return false;
        x = x / 10;
    }
    return flag;
}
int Solution788::rotatedDigits(int N)
{
    //int num = 0;
    //int flag;
    //int invalid;//0-valid 1--invalid
    //for (int i = 1; i <= N; ++i)
    //{
    //  flag = 0;
    //  invalid = 0;
    //  string int_string = to_string(i);
    //  for (int j = 0; j < int_string.size(); ++j)
    //  {
    //      if (flag == 0 && int_string[j] == '2' || int_string[j] == '5' || int_string[j] == '6' || int_string[j] == '9')
    //      {
    //          flag = 1;
    //      }
    //      if (int_string[j] == '3' || int_string[j] == '4' || int_string[j] == '7')
    //      {
    //          invalid = 1;//表示有3,4,7
    //          break;
    //      }
    //  }
    //  if (!invalid && flag)++num;

    //}
    //return num;
    int num = 0;
    while (N > 0)
    {
        if (isDiff(N))++num;
        --N;
    }
    return num;
}
  • 数字比较比字符串比较速度快
  • 将数字转换成字符串的内置函数to_string()
  • 不要忘记invalid digit比较

结束

更多内容请浏览我的blog:小可爱的博客
源码请见我的GitHub:AlisaBen

你可能感兴趣的:(leetcode,c++)