作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


目录

  • 题目描述
  • 题目大意
  • 解题方法
  • 日期

题目地址:https://leetcode.com/problems/triples-with-bitwise-and-equal-to-zero/

题目描述

Given an array of integers A, find the number of triples of indices (i, j, k) such that:

  • 0 <= i < A.length
  • 0 <= j < A.length
  • 0 <= k < A.length
  • A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.

Example 1:

Input: [2,1,3]
Output: 12Explanation: We could choose the following i, j, k triples:(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2

Note:

  1. 1 <= A.length <= 1000
  2. 0 <= A[i] < 2^16

题目大意

找出给定的数组中,有多少个三元组,使得这个三元组的位与等于0.

解题方法

看了下数组长度是1000,大概分析下这个题目的时间复杂度是O(N^2×log(N))以下。同时注意到三元组的顺序并无要求,即同样的组合可能出现多次。

一个最省事的做法就是,先两重循环,计算任意两个数字之间的位与结果是多少,存储到字典中,字典中保存的是位与出现的次数。然后再次对数组每个位置进行遍历,同时遍历字典中的每个元素,即可分析出任意三个数字位与的结果和次数。

唯一需要注意的是字典保存的是两个数字位与后的结果出现的次数,当第三个数字和两位数字位与结果进行位与的结果是0的时候,需要次数累加。

这个时间复杂度怎么分析?注意题目给出的每个元素的大小是216,所以两个数字位与的结果不会超过216。因此,总的时间复杂度是O(N^2 + 2^16 * N).

class Solution {public:int countTriplets(vector<int>& A) {const int N = A.size();int res = 0;unordered_map<int, int> m_;for (int i = 0; i < N; ++i) {for (int j = 0; j < N; ++j) {++m_[(A[i] & A[j])];}}for (int i = 0; i < N; ++i) {for (auto a : m_) {if ((A[i] & a.first) == 0)res += a.second;}}return res;}
};

日期

2019 年 1 月 27 日 —— 这个周赛不太爽

【LeetCode】982. Triples with Bitwise AND Equal To Zero 解题报告(C++)相关推荐

  1. 【LeetCode】163.Missing Ranges(Medium)(带锁题)解题报告

    [LeetCode]163.Missing Ranges(Medium)(带锁题)解题报告 题目地址:https://leetcode.com/problems/missing-ranges/(带锁题 ...

  2. Leetcode 407. Trapping Rain Water II 收集雨水2 解题报告

    1 解题思想 我看了下题目,发现比预想中的简单,加之我比较烂,所以其实我还是没做,只是看懂了上回贴的代码,然后做了一下注释,现在我来讲下题目. 首先请看下上一题,上一题是2D的这题是3D的: Leet ...

  3. Leetcode 1190. Reverse Substrings Between Each Pair of Parentheses解题报告(python)

    1190. Reverse Substrings Between Each Pair of Parentheses Reverse Substrings Between Each Pair of Pa ...

  4. [LeetCode]844. Backspace String Compare 解题报告(C++)

    [LeetCode]844. Backspace String Compare 解题报告(C++) 题目描述 Given two strings S and T, return if they are ...

  5. LeetCode解题报告汇总

    LeetCode解题报告: [LeetCode]1.Two Sum - Yoona - 博客频道 - CSDN.NET [LeetCode]2.Add Two Numbers - Yoona - 博客 ...

  6. 【LeetCode】436. Find Right Interval 解题报告(Python)

    [LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  7. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  8. LeetCode第45场双周赛-解题报告

    LeetCode第45场双周赛-解题报告 A. 唯一元素的和 原题链接 https://leetcode-cn.com/problems/sum-of-unique-elements/ 解题思路 因为 ...

  9. [LeetCode解题报告] LCP 48. 无限棋局

    [LeetCode解题报告] LCP 48. 无限棋局 一. 题目 1. 题目描述 2. 原题链接 二. 解题报告 1. 思路分析 2. 复杂度分析 3. 代码实现 三. 本题小结 一. 题目 1. ...

最新文章

  1. linux压缩和解压命令总结
  2. 常见的8个前端防御性编程方案
  3. onvif规范 中文介绍
  4. python二维数组换行输出_关于用python绘制二维数组的问题
  5. Visual Studio 2010 C++ 用户属性设置
  6. SQL 2008镜像配置
  7. 学习Unix其实就这样简单
  8. iOS 使用fopen返回null
  9. 很吊炸天的Xcode插件,你想要的这都有
  10. kibana集成内部账号_揭开 Elasticsearch 中身份验证和授权的神秘面纱
  11. 高级项目管理-3、项目立项、变更、整体管理
  12. 相机的对焦是什么意思?为什么需要对焦?
  13. 删除单向链表的最后一个节点
  14. 【玩转数据系列十五】机器学习PAI为你自动写歌词,妈妈再也不用担心我的freestyle了(提供数据、代码)
  15. 交通灯管理系统视频学习
  16. 注意力机制、bmm运算
  17. 解决过渡动画导致的抖动
  18. 课程表APP开发市场现状分析
  19. 一个3位数字.COM的域名一般多少钱??
  20. 10款最佳免费WiFi黑客工具(附传送门)

热门文章

  1. Jsoup简单例子2.0——多线程爬取网页内的邮箱
  2. 家用投影仪什么牌子好又便宜?
  3. 教你批量导出小红书商城中的多款商品图片素材
  4. ACP敏捷认证能学到什么?
  5. android 动画x轴旋转,写给小白——Android旋转动画(3个方向的旋转)
  6. dede 分页 上一页 下一页
  7. 探讨与研究——动态规划算法、回溯法、分支限界法解0-1背包问题
  8. 元宇宙,是噱头还是创新?
  9. android baseadapter优化,2.4.6 BaseAdapter优化
  10. 类似暴风影音的C++影音播放器!(后面留有源代码)