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

Every day a Leetcode

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

解法1:遍历

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

构成整天的条件:(hours[i] + hours[j]) % 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)。

相关推荐
m0_71557534几秒前
分布式任务调度系统
开发语言·c++·算法
CSDN_RTKLIB22 分钟前
简化版unique_ptr说明其本质
c++
naruto_lnq22 分钟前
泛型编程与STL设计思想
开发语言·c++·算法
踩坑记录38 分钟前
leetcode hot100 94. 二叉树的中序遍历 easy 递归 dfs
leetcode
m0_748708051 小时前
C++中的观察者模式实战
开发语言·c++·算法
时光找茬1 小时前
【瑞萨AI挑战赛-FPB-RA6E2】+ 从零开始:FPB-RA6E2 开箱测评与 e2 studio 环境配置
c++·单片机·边缘计算
qq_537562671 小时前
跨语言调用C++接口
开发语言·c++·算法
猷咪2 小时前
C++基础
开发语言·c++
CSDN_RTKLIB2 小时前
WideCharToMultiByte与T2A
c++
星火开发设计2 小时前
类型别名 typedef:让复杂类型更简洁
开发语言·c++·学习·算法·函数·知识