• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode835. 图像重叠 | Image Overlap

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10576125.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Two images A and B are given, represented as binary, square matrices of the same size.  (A binary matrix has only 0s and 1s as values.)

We translate one image however we choose (sliding it left, right, up, or down any number of units), and place it on top of the other image.  After, the overlap of this translation is the number of positions that have a 1 in both images.

(Note also that a translation does not include any kind of rotation.)

What is the largest possible overlap?

Example 1:

Input: A = [[1,1,0],
            [0,1,0],
            [0,1,0]]
       B = [[0,0,0],
            [0,1,1],
            [0,0,1]]
Output: 3
Explanation: We slide A to right by 1 unit and down by 1 unit.

Notes: 

  1. 1 <= A.length = A[0].length = B.length = B[0].length <= 30
  2. 0 <= A[i][j], B[i][j] <= 1

给出两个图像 A 和 B ,A 和 B 为大小相同的二维正方形矩阵。(并且为二进制矩阵,只包含0和1)。

我们转换其中一个图像,向左,右,上,或下滑动任何数量的单位,并把它放在另一个图像的上面。之后,该转换的重叠是指两个图像都具有 1 的位置的数目。

(请注意,转换不包括向任何方向旋转。)

最大可能的重叠是什么?

示例 1:

输入:A = [[1,1,0],
          [0,1,0],
          [0,1,0]]
     B = [[0,0,0],
          [0,1,1],
          [0,0,1]]
输出:3
解释: 将 A 向右移动一个单位,然后向下移动一个单位。

注意: 

  1. 1 <= A.length = A[0].length = B.length = B[0].length <= 30
  2. 0 <= A[i][j], B[i][j] <= 1

288ms

 1 class Solution {
 2     func largestOverlap(_ A: [[Int]], _ B: [[Int]]) -> Int {
 3         var LA = [Int](), LB = [Int]()
 4         let N = A.count
 5         var count = [Int: Int]()
 6         for i in 0..<N*N {
 7             if A[i/N][i%N] == 1 {
 8                 LA.append( i/N * 100 + i % N)
 9             }
10             if B[i/N][i%N] == 1  {
11                 LB.append( i/N * 100 + i % N)
12             }
13         }
14 
15         for i in LA {
16             for j in LB {
17                 count[i-j, default: 0] += 1
18             }
19         }
20 
21         var res = 0
22         for (k, v) in count {
23             res = max(res, v)
24         }
25         return res
26     }
27 }

Runtime: 292 ms
Memory Usage: 19.3 MB
 1 class Solution {
 2     func largestOverlap(_ A: [[Int]], _ B: [[Int]]) -> Int {
 3         var res:Int = 0
 4         var n:Int = A.count
 5         var listA:[Int] = [Int]()
 6         var listB:[Int] = [Int]()
 7         var diffCnt:[Int:Int] = [Int:Int]()
 8         for i in 0..<(n * n)
 9         {
10             if A[i / n][i % n] == 1
11             {
12                 listA.append(i / n * 100 + i % n)
13             }
14             if B[i / n][i % n] == 1
15             {
16                 listB.append(i / n * 100 + i % n)
17             }
18         }
19         for a in listA
20         {
21             for b in listB
22             {
23                 diffCnt[a - b,default:0] += 1
24             }
25         }
26         for diff in diffCnt
27         {
28             res = max(res, diff.value)
29         }
30         return res
31     }
32 }

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]LeetCode1078.Bigram分词|OccurrencesAfterBigram发布时间:2022-07-13
下一篇:
Swift(一)简单值发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap