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

相关推荐
jimy110 分钟前
c++的 Type Traits (二、更详细版本)
开发语言·c++
Scintilla_y1 小时前
DAY2 滑动窗口
算法·leetcode·散列表
特立独行的猫a2 小时前
仓颉语言UI开发实战:我用仓颉写了个桌面版力扣
算法·leetcode·ui·tauri·仓颉·cj-tarui
y1su2 小时前
Leetcode 动态规划 模板
java·数据结构·算法·leetcode·动态规划
半杯咖啡半行码2 小时前
C++11核心特性详解:从C++11到C++20 的现代C++指南
开发语言·c++
All for pursuit.3 小时前
【位运算-3】136.只出现一次的数字
数据结构·c++·算法·leetcode
C++ 老炮儿的技术栈5 小时前
std::atomic 是什么
linux·c语言·网络·c++·visual studio
All for pursuit.5 小时前
【完结】LeetCodeHot100题 详细解题过程及代码汇总
数据结构·c++·算法·leetcode
mmmmath_35 小时前
LeetCode.101.对称二叉树
数据结构·leetcode
重生之小比特6 小时前
【算法拾光】双指针|移动零 + 复写零
数据结构·c++·算法