• [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 }
  • 相关阅读:
    进程提权小结
    进程工作集WorkingSet (PSAPI 01)
    GetSystemInfo 和 GlobalMemoryStatus获取系统信息,内存信息
    C++11智能指针 share_ptr,unique_ptr,weak_ptr用法
    结构体及类对象的内存对齐
    菱形继承问题和虚继承
    TLS反调试
    获取程序目录,模块路径
    docker install for centos7
    docker镜像与容器概念
  • 原文地址:https://www.cnblogs.com/strengthen/p/10326155.html
Copyright © 2020-2023  润新知