leetcode-string-easy-696-788

10 篇文章 0 订阅

每天坚持写几道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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值