在区间范围内统计奇数数目 - 简单

*************

C++

topic: 1523. 在区间范围内统计奇数数目 - 力扣(LeetCode)

*************

Give it a look at the topic.

How is your vacation. And I always mess up vacation and vocation. I would complain about the confused spelling of the words. But now I donot, because I cannnot change the vocabularies. Just remember it. I would try some short ways to get the point and neglect the significant of using the basic way. It makes you devoting all your resources to rembember the words instead of sharing some of your resurces to doubting what is the short way to get there. I like simple persons and things. I always like them.

Todays topic is really a easy one. It gives you two numbers, which is start and end. So full the nubers first. Before that, let's review some basic options in vector.

initialise the function.

// 初始化函数
vector<int> v1;             // 空vector
vector<int> v2(5);          // 包含5个0(默认初始化)
vector<int> v3(5, 10);      // 包含5个10
vector<int> v4 = {1, 2, 3}; // 初始化列表(C++11)

visit the function

// 访问元素
vector<int> v = {10, 20, 30};

// 通过下标访问(不检查越界)
int a = v[1]; // 20

// 首尾元素
int front = v.front(); // 10
int back = v.back();   // 30

retouch operation

// 尾部添加元素
v.push_back(40); // {10, 20, 30, 40}

// 删除尾部元素
v.pop_back(); // {10, 20, 30}

// 插入元素(迭代器位置)
v.insert(v.begin() + 1, 15); // {10, 15, 20, 30}

// 删除元素(迭代器位置/范围)
v.erase(v.begin() + 2);            // {10, 15, 30}
v.erase(v.begin(), v.begin() + 1); // {15, 20, 30}

capacity operation

vector<int> v = {1, 2, 3};

v.size();       // 当前元素数量(3)
v.empty();      // 是否为空(false)
v.capacity();   // 当前分配的存储容量(≥3)

// 调整容量(可能重新分配内存)
v.reserve(100); // 预分配100个元素的空间

// 调整大小(多余元素被删除/默认填充)
v.resize(5);    // {1, 2, 3, 0, 0}
v.resize(2);    // {1, 2}

That is all I need to know.

The first step is to initialise the vector odd.

class Solution {
public:
    int countOdds(int low, int high) {

        vector<int> odd;
        
    }
};

The second step is to full the vector odd.

class Solution {
public:
    int countOdds(int low, int high) {

        vector<int> odd;
        
        for (int i = low; i < high; i++)
        {
            odd.push_back(i);
        }
    }
};

And the last step is to return the odd int.

class Solution {
public:
    int countOdds(int low, int high) {

        vector<int> odd; // 用于生成完整的数组
        
        for (int i = low; i < high; i++)
        {
            odd.push_back(i);
        }

        vector<int> result; // 用于存储奇数
        int n = odd.size();
        for (int i = 0; i < n; i++)
        {
            if (odd[i] % 2 == 1)
            {
                result.push_back(i);
            }
        }

        return result.size();
    }
};

You will notice that something is wrong. Because  for (int i = low; i < high; i++) should be  for (int i = low; i <= high; i++).

 for (int i = low; i <= high; i++)
        {
            odd.push_back(i);
        }

But as I said, I like simple things. The code is a bit complex. And there is a smart way to find the result. 

if the first number is even number, then the second number is odd. If the last number is even then the number before the last number is odd. so the numbers of odd is (last - frsrt) / 2 + 1.

class Solution {
public:
    int countOdds(int low, int high) {
        // 如果 low 是偶数,则 low+1 是第一个可能的奇数
        if (low % 2 == 0) 
        {
            low++;
        }
        // 如果 high 是偶数,则 high-1 是最后一个可能的奇数
        if (high % 2 == 0) 
        {
            high--;
        }
        // 计算奇数个数
        return (high - low) / 2 + 1;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值