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

相关推荐
blueman88881 小时前
VS2022 切换定义(F12 / Go to Definition)反应慢
c++·visual studio
凡人叶枫1 小时前
Effective C++ 条款35:考虑 virtual 函数以外的其他选择
java·c++·spring
郝学胜-神的一滴1 小时前
CMake 017:彩色日志输出实战
linux·c语言·开发语言·c++·软件工程·软件构建·cmake
Navigator_Z1 小时前
LeetCode //C - 1096. Brace Expansion II
c语言·算法·leetcode
桀人2 小时前
C++——string类的详细介绍
开发语言·c++
笨笨没好名字2 小时前
Leetcode刷题python版第一周
python·算法·leetcode
一只齐刘海的猫2 小时前
【Leetcode】无重复字符的最长子串
算法·leetcode·职场和发展
插件开发2 小时前
vs2015 cuda c++ cdpSimplePrint范例,递归功能实现演示
linux·c++·算法
Tisfy2 小时前
LeetCode 2130.链表最大孪生和:转数组 / 快慢指针+链表翻转(O(1))
算法·leetcode·链表·题解
zh_xuan3 小时前
PC端操作SQLite数据库
数据库·c++·sqlite