- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
它被赋予一个字符串数组。字符串具有相同的长度。假设输入数组是 (["ababc", "abbba", "aaabb", "bcccb", "acbbb", "aaabb"]).
a b a b c
a b b b a
a a a b b
b c c c b
a c b b b
a a a b b
问题是关于找到最小的字母连接组,可以垂直和水平地绘制同一个字母,但不能沿对 Angular 线绘制。对于此示例,字母“a”有 4 个组(两个单组,两个多组),字母“b”分为 2 组(一个单一的,一个多重的)和字母“c”的 2 个组(一个单一的,一个多重的)。所以,输出应该是“8”。我找不到使用 JavaScript 的解决方案。
我试图将其视为单独的行和列。此外,为每个字符创建数组并单独分析。但它们都无法正常工作。
function strokesRequired(picture){
let total = picture.join('');
let strLength = picture[0].length;
let counter = 0;
for(let i = 0; i < total.length; i++){
if(total[i] !== total[i+1] || total[i] !== total[i+strLength] || total[i] !== total[i-1] || total[i] !== total[i-strLength]){
continue;
}
else {
counter++;
}
}
return counter;
}
对于这个问题,我的代码返回 4,但应该返回 8。因为我只是计算字母,没有任何水平和垂直匹配。
最佳答案
这是从每个未访问过的单元格运行深度优先搜索的一种方法。我认为这也可以通过泛洪填充来解决,即在显示连接时合并组件标签。
function f(M){
let m = M.length
let n = M[0].length
let visited = new Array(m)
for (let i=0; i<m; i++)
visited[i] = new Array(n).fill(0)
function getNeighbours(y, x){
let c = M[y][x]
let neighbours = []
if (y > 0 && !visited[y-1][x] && M[y-1][x] == c)
neighbours.push([y-1, x])
if (y < m - 1 && !visited[y+1][x] && M[y+1][x] == c)
neighbours.push([y+1, x])
if (x > 0 && !visited[y][x-1] && M[y][x-1] == c)
neighbours.push([y, x-1])
if (x < n - 1 && !visited[y][x+1] && M[y][x+1] == c)
neighbours.push([y, x+1])
return neighbours
}
function dfs(y, x){
let stack = [[y, x]]
while (stack.length){
let [y, x] = stack.pop()
visited[y][x] = 1
stack.push(...getNeighbours(y, x))
}
}
let count = 0
for (let i=0; i<m; i++){
for (let j=0; j<n; j++){
if (!visited[i][j]){
count++
dfs(i, j)
}
}
}
return count
}
var M = [
"ababc",
"abbba",
"aaabb",
"bcccb",
"acbbb",
"aaabb"
]
console.log(f(M))
关于javascript - 给出给定字符串数组所需的最小笔画的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58235415/
在下面的代码中,我得到一个 uninitialized value警告,但仅限于第二个 given/when例子。为什么是这样? #!/usr/bin/env perl use warnings; u
整个“开关”功能是否已成为实验性的?在没有 Perl 的 future 版本破坏我的代码的情况下,我可以依赖其中的某些部分吗?一般来说,将稳定功能更改为实验性的政策是什么? 背景use feature
有没有办法在一个条件语句中写出如下语句? a和b不能同时等于5。 (a可以是5,b可以是5,但是a AND b不能是5) 最佳答案 正如克里斯指出的那样,您要查找的是逻辑异或,相当于逻辑不等于 !=:
我正在寻找一种算法来找到给定 n 条线段的所有交点。以下是来自 http://jeffe.cs.illinois.edu/teaching/373/notes/x06-sweepline.pdf 的伪
数组中有 N 个元素。我可以选择第一项最多 N 次,第二项最多选择 N-1 次,依此类推。 我有 K 个 token 要使用并且需要使用它们以便我可以拥有最大数量的项目。 arr = [3, 4, 8
我正在尝试修复法语文本中的语法性别,想知道是否有办法从某个词条中获取所有单词的列表,以及是否可以在此类列表中进行查找? 最佳答案 尝试: import spacy lemma_lookup = spa
我正在为 Win32 编写一个简单的自动化测试应用程序。它作为一个单独的进程运行,并通过 Windows API 访问目标应用程序。我可以阅读窗口层次结构,查找标签和文本框,并通过发送/发布消息等来单
在 nodeJs 中使用 Sequelize 时,我从 Sequelize 收到此错误,如下所示: { [SequelizeUniqueConstraintError: Validation erro
本文https://arxiv.org/pdf/1703.10757.pdf使用回归激活映射 (RAM) - 而不是类激活映射 (CAM) 来解决问题。有几篇文章描述了如何实现 CAM。但是我找不到
我正在研究 Mach 动态链接器 dyld。这个问题适用于所有 Apple 平台,但很高兴得到特定于平台的答案;我正在使用 ObjC,但如果对你有用的话,我也很乐意翻译 Swift。 The rele
我有一个包含数千个 Instagram 用户 ID 的列表。我如何获得他们的 Instagram 用户名/句柄? 最佳答案 你必须使用这个 Instagram API: https://api.ins
我在下面的代码: def main(args: Array[String]) { val sparkConf = new SparkConf().setAppName("Spark-Hbase").s
我有一个表格,其中包含从 1 到 10 的数字。(从 D2 到 M2) 假设A1中有03/09/2019 并且在B1中有06/09/2019 并且在C1中有Hello 在A 列中,我有多个系列的单词,
我想在给定服务对应的 URI 的情况下检索服务的注释(特别是 @RolesAllowed )。这是一个例子: 服务: @GET @Path("/example") @RolesAllowed({ "B
我看到 OraclePreparedStatementexecuteQuery() 表现出序列化。也就是说,我想使用相同的连接对 Oracle 数据库同时运行两个查询。然而,OraclePrepare
import java.util.Scanner; public class GeometricSumFromK { public static int geometricSum(int k,
我创建了一个抽象基类Page,它说明了如何构建动态网页。我正在尝试想出一种基于作为 HttpServletRequest 传入的 GET 请求生成 Page 的好方法。例如... public cla
我的字符串是一条短信,采用以下两种格式之一: 潜在客户短信: 您已收到 1 条线索 标题:我的领导 潜在客户 ID:12345-2365 警报设置 ID:890 短信回复: 您已收到 1 条回复 标题
我在 python 中有以下代码: class CreateMap: def changeme(listOne, lisrTwo, listThree, listFour, listfive):
这是在 Hibernate 上运行的 JPA2。 我想检索相同实体类型的多个实例,给定它们的 ID。其中许多已经在持久性上下文和/或二级缓存中。 我尝试了几种方法,但似乎都有其缺点: 当我使用 ent
我是一名优秀的程序员,十分优秀!