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(n^2^),其中 n 是数组 hours 的长度。

空间复杂度:O(1)。

相关推荐
程序员-King.2 小时前
【接口封装】——13、登录窗口的标题栏内容设置
c++·qt
leeyayai_xixihah3 小时前
2.21力扣-回溯组合
算法·leetcode·职场和发展
01_3 小时前
力扣hot100——相交,回文链表
算法·leetcode·链表·双指针
萌の鱼3 小时前
leetcode 2826. 将三个组排序
数据结构·c++·算法·leetcode
AllowM3 小时前
【LeetCode Hot100】除自身以外数组的乘积|左右乘积列表,Java实现!图解+代码,小白也能秒懂!
java·算法·leetcode
RAN_PAND3 小时前
STL介绍1:vector、pair、string、queue、map
开发语言·c++·算法
记得早睡~6 小时前
leetcode150-逆波兰表达式求值
javascript·算法·leetcode
qy发大财6 小时前
跳跃游戏(力扣55)
算法·leetcode
mit6.8246 小时前
[实现Rpc] 通信-Muduo库的实现 | && 完美转发 | reserve | unique_lock
c++·网络协议·rpc