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

相关推荐
8Qi87 小时前
LeetCode 75:颜色分类(荷兰国旗问题)—— Java 题解 ✅
java·算法·leetcode·指针·排序
(●—●)橘子……10 小时前
力扣第503场周赛练习理解
python·学习·算法·leetcode·职场和发展·周赛
feng_you_ying_li12 小时前
C++复习二,继承与多态
c++
小小de风呀12 小时前
de风——【从零开始学C++】(十一):list的基本使用和模拟实现
开发语言·c++·list
陌路2012 小时前
C++高级进阶--夯实进阶基础(1)
开发语言·c++
风筝在晴天搁浅13 小时前
快手 CodeTop LeetCode 224.基本计算器
数据结构·算法·leetcode
郝学胜-神的一滴13 小时前
中级OpenGL教程 008:精准控制高光光斑大小与强度
c++·unity·godot·three.js·图形学·opengl·unreal
牢姐与蒯14 小时前
c++数据结构之c++11(一)
数据结构·c++
折戟不必沉沙14 小时前
构造和析构函数能否是虚函数?能否调用虚函数?
c++
-To be number.wan14 小时前
算法日记 | STL- sort排序
c++·算法