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)。

相关推荐
洛水水6 小时前
【力扣100题】53.最长回文子串
算法·leetcode·职场和发展
过期动态8 小时前
【LeetCode 热题 100】盛最多水的容器
java·数据结构·spring boot·算法·leetcode·spring cloud·职场和发展
凌波粒8 小时前
LeetCode--700.二叉搜索树中的搜索(二叉树)
算法·leetcode·职场和发展
洛水水8 小时前
【力扣100题】58.轮转数组
算法·leetcode
风筝在晴天搁浅8 小时前
阿里 LeetCode 876.链表的中间节点
算法·leetcode·链表
玖釉-9 小时前
二叉树展开为链表:从先序遍历到原地指针重排
c++·windows·算法·leetcode·链表
Mister西泽9 小时前
C++ Primer Plus 第六版 编程练习题及详细答案
开发语言·c++·学习·visual studio
Qt程序员9 小时前
从上电到系统就绪:ARM+U-Boot 嵌入式 Linux 启动流程
linux·运维·c++·内核·设备树·嵌入式·ram
洛水水9 小时前
【力扣100题】52.最小路径和
算法·leetcode
圣保罗的大教堂9 小时前
leetcode 3043. 最长公共前缀的长度 中等
leetcode