zoukankan      html  css  js  c++  java
  • [Swift]LeetCode982. 按位与为零的三元组 | Triples with Bitwise AND Equal To Zero

    原文地址:https://www.cnblogs.com/strengthen/p/10326155.html 

    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: 12
    Explanation: 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

    给定一个整数数组 A,找出索引为 (i, j, k) 的三元组,使得:

    • 0 <= i < A.length
    • 0 <= j < A.length
    • 0 <= k < A.length
    • A[i] & A[j] & A[k] == 0,其中 & 表示按位与(AND)操作符。 

    示例:

    输入:[2,1,3]
    输出:12
    解释:我们可以选出如下 i, j, k 三元组:
    (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 

    提示:

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

    9888ms

     1 class Solution {
     2     func countTriplets(_ A: [Int]) -> Int {
     3         var cnt:[Int] = [Int](repeating:0,count:65536)
     4         for i in 0..<65536
     5         {
     6             for n in A
     7             {
     8                 if (i&n)==0
     9                 {
    10                     cnt[i] += 1
    11                 }
    12             }
    13         }
    14         
    15         var res:Int = 0
    16         for a in A
    17         {
    18             for b in A
    19             {
    20                 res += cnt[a&b]
    21             }
    22         }
    23         return res
    24     }
    25 }
  • 相关阅读:
    memory runs at single channel问题解决
    ASP php获取文件URL地址等方法
    zencart的modules下数据库操作templates排版和common首页引用
    php生成html 伪静态??
    Linux服务器自动备份压缩MySQL数据库的实用方法
    dos 命令集
    Zencart 500错误查找和解决方法
    破解JS加密:url unicode加密而已
    .htaccess 保护文件夹
    TCP 的那些事儿(上)
  • 原文地址:https://www.cnblogs.com/strengthen/p/10326155.html
Copyright © 2011-2022 走看看