[Swift]LeetCode1033. 边框着色 | Moving Stones Until Consecutive

Given a 2-dimensional grid of integers, each value in the grid represents the color of the grid square at that location.

Two squares belong to the same connected component if and only if they have the same color and are next to each other in any of the 4 directions.

The border of a connected component is all the squares in the connected component that are either 4-directionally adjacent to a square not in the component, or on the boundary of the grid (the first or last row or column).

Given a square at location (r0, c0) in the grid and a color, color the border of the connected component of that square with the given color, and return the final grid.

Example 1:

Input: grid = [[1,1],[1,2]], r0 = 0, c0 = 0, color = 3
Output: [[3, 3], [3, 2]]

Example 2:

Input: grid = [[1,2,2],[2,3,2]], r0 = 0, c0 = 1, color = 3
Output: [[1, 3, 3], [2, 3, 3]]

Example 3:

Input: grid = [[1,1,1],[1,1,1],[1,1,1]], r0 = 1, c0 = 1, color = 2
Output: [[2, 2, 2], [2, 1, 2], [2, 2, 2]]

Note:

  1. 1 <= grid.length <= 50
  2. 1 <= grid[0].length <= 50
  3. 1 <= grid[i][j] <= 1000
  4. 0 <= r0 < grid.length
  5. 0 <= c0 < grid[0].length
  6. 1 <= color <= 1000


给出一个二维整数网格 grid,网格中的每个值表示该位置处的网格块的颜色。

只有当两个网格块的颜色相同,而且在四个方向中任意一个方向上相邻时,它们属于同一连通分量。

连通分量的边界是指连通分量中的所有与不在分量中的正方形相邻(四个方向上)的所有正方形,或者在网格的边界上(第一行/列或最后一行/列)的所有正方形。

给出位于 (r0, c0) 的网格块和颜色 color,使用指定颜色 color 为所给网格块的连通分量的边界进行着色,并返回最终的网格 grid 。

示例 1:

输入:grid = [[1,1],[1,2]], r0 = 0, c0 = 0, color = 3
输出:[[3, 3], [3, 2]]

示例 2:

输入:grid = [[1,2,2],[2,3,2]], r0 = 0, c0 = 1, color = 3
输出:[[1, 3, 3], [2, 3, 3]]

示例 3:

输入:grid = [[1,1,1],[1,1,1],[1,1,1]], r0 = 1, c0 = 1, color = 2
输出:[[2, 2, 2], [2, 1, 2], [2, 2, 2]]

提示:

  1. 1 <= grid.length <= 50
  2. 1 <= grid[0].length <= 50
  3. 1 <= grid[i][j] <= 1000
  4. 0 <= r0 < grid.length
  5. 0 <= c0 < grid[0].length
  6. 1 <= color <= 1000


Runtime: 8 ms

Memory Usage: 19.1 MB

 1 class Solution {
 2     func numMovesStones(_ a: Int, _ b: Int, _ c: Int) -> [Int] {
 3         var arr:[Int] = [a,b,c].sorted(by:<)
 4         let a = arr[0]
 5         let b = arr[1]
 6         let c = arr[2]
 7         var minNum:Int = 0
 8         if abs(a - b) == 2 || abs(c - b) == 2
 9         {
10             minNum +=  1
11         }
12         else
13         {
14             if abs(a - b) > 2
15             {
16                 minNum +=  1
17             }
18             if  abs(c - b) > 2
19             {
20                 minNum +=  1
21             }
22         }
23         return [minNum, abs(b - a) + abs(c - b) - 2]
24     }
25 }

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

时间: 2024-08-30 08:37:01

[Swift]LeetCode1033. 边框着色 | Moving Stones Until Consecutive的相关文章

滑动窗口-Moving Stones Until Consecutive II

2020-02-20 16:34:16 问题描述: 问题求解: public int[] numMovesStonesII(int[] stones) { int n = stones.length; Arrays.sort(stones); int min = n; int start = 0; for (int end = 0; end < n; end++) { while (stones[end] - stones[start] + 1 > n) start += 1; int cur

LeetCode.1033-移动石头直到连续(Moving Stones Until Consecutive)

这是小川的第386次更新,第414篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第247题(顺位题号是1033).在a,b和c位置的数字线上有三块石头.每次,你在一个终点(即最低或最高位置的石头)上拾取一块石头,然后将它移动到这些终点之间的空置位置. 形式上,假设石头当前位于x,y,z位置,x <y <z.你在x位置或z位置拾取石头,然后将石头移动到整数位置k,x <k <z且k!= y. 当你不能做任何移动时,游戏结束.例如,当石头处于连续的位置时.

Mac之button的使用Show+NSMenu+next+to+NSButton+in+Swift+OSX

http://www.itdaan.com/keywords/Show+NSMenu+next+to+NSButton+in+Swift+OSX.html http://www.itdaan.com/keywords/Swift+Mac+OSX+NSButton+title+color.html 1在SwiftOSX中显示NSButton旁边的NSMenu - ShowNSMenunext to NSButton in SwiftOSX 2016年04月11 - How can i show a

学习Canvas绘图与动画基础 为多边形着色(三)

1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title>多边形着色</title> 6 </head> 7 <body> 8 <canvas id="canvas" style="border:1px solid #aaa; disp

[转] Xcode 高级调试技巧

在苹果的官方文档中列出了我们在调试中能用到的一些命令,我们在这重点讲一些常用的命令 调试本地文件方法(Mac OS X):(lldb) target create "/Users/piaoyun/Desktop/xx.app/Contents/MacOS/xxxx" 远程调试方法: 设备端运行: 附加进程: ./debugserver *:1234 -a "YourAPPName" 直接启动进程: debugserver -x backboard *:1234 /p

Leetcode题解 - DFS部分题目代码+思路(756、1034、1110、491、721、988)

756. 金字塔转换矩阵 """ 学到的新知识: from collections import defaultditc可以帮我们初始化字典,不至于取到某个不存在的值的时候报错.例如列表类型就会默认初始值为[],str对应的是空字符串,set对应set( ),int对应0 思路: 通过本层构建上一层(DFS,类似于全排列),看是否能构建成功(递归) """ from collections import defaultdict class Sol

error in Swift. “Consecutive Declarations On A Line Must Be Separated By &#39;;&#39;”

当输入的时候以上代码的时候出现这个错误,经过尝试发现条件表达式?前面应该有一个空格  不然会和swift中的?和 !混淆 error in Swift. "Consecutive Declarations On A Line Must Be Separated By ';'"

Swift - 给图片和按钮添加阴影边框

最近比较忙,想要做的事情有很多,能做出来的就只有一部份,我觉得也许是我没有计划和规律造成的,我需要坚持下去,今天写了一个swift2.0给按钮或者图片添加阴影的效果,就当做笔记吧:-) Swift Code: let image1 = UIImageView(frame: CGRectMake(20,50,self.view.frame.width-50,180)) image1.image = UIImage(named: "sea.jpg") self.view.addSubvie

swift UI专项训练36 ImageVi图片边框阴影

ImageView是我们经常用到的组件,但是我们发现storyboard中图片的属性编辑器中没有对于图片边框的设计.在view中添加一张普通的图片,效果是这样的: 可以看到,白色背景上的深色图片,效果总是有点突兀,我们想要加一个阴影边框来过渡一下图片和背景,打开图片所属的控制器代码,类中所显示的图片名为image,现在来设置它的边框.在viewDidLoad中输入以下代码: image.layer.backgroundColor = UIColor.orangeColor().CGColor i