Leetcode3184. 构成整天的下标对数目 I

Every day a Leetcode

题目来源:3184. 构成整天的下标对数目 I

解法1:遍历

统计满足 i < j 且 hoursi + hoursj 构成整天的下标对 i, j 的数目。

构成整天的条件:(hoursi + hoursj) % 24 == 0。

代码:

c 复制代码
/*
 * @lc app=leetcode.cn id=3184 lang=cpp
 *
 * [3184] 构成整天的下标对数目 I
 */

// @lc code=start
class Solution
{
public:
    int countCompleteDayPairs(vector<int> &hours)
    {
        int n = hours.size();
        int count = 0;
        for (int i = 0; i < n - 1; i++)
            for (int j = i + 1; j < n; j++)
                if ((hours[i] + hours[j]) % 24 == 0)
                    count++;
        return count;
    }
};
// @lc code=end

结果:

复杂度分析:

时间复杂度:O(n2),其中 n 是数组 hours 的长度。

空间复杂度:O(1)。

相关推荐
小溪学编程12 小时前
从 C++ 的规范变迁看语言发展
jvm·c++·面试
ShineWinsu13 小时前
对于Redis:string类型的解析
java·c++·redis·分布式·缓存·面试·string
YYYing.13 小时前
【设计模式系列 (七) 】桥接模式
c++·后端·设计模式·桥接模式
高山有多高14 小时前
【Linux笔记】Linux自定义Shell
linux·c++
悠仁さん15 小时前
【C++】vector 类
开发语言·c++
_upupup15 小时前
智能指针的原理及简单模拟实现(C++)
开发语言·c++
l1t15 小时前
DeepSeek总结的OpenZL压缩变换器
c++·算法
想吃火锅100515 小时前
【leetcode】238.除了自身以外数组的乘积js
javascript·算法·leetcode
郝学胜-神的一滴15 小时前
C++ Templates 01:拨开迷雾,读懂C++模板经典著作
开发语言·c++·程序人生·游戏·游戏引擎·软件构建
坤坤子吖15 小时前
算法学习——高精度加减乘除
c++·笔记·学习·算法