package main

import (
    "fmt"
)

type Result struct {
    C rune // character
    L int  // count
}

func main() {
    fmt.Print(LongestRepetition(""))
}
func LongestRepetition(text string) Result {
    if text == "" {
        return Result{}
    }
    var max Result
    if len(text) == 1 {
        max.C = rune(text[0])
        max.L = 1
        return max
    }
    var count Result
    for _, s := range text {
        if count.C == s {
            count.L++
            count.C = s
            if count.L > max.L {
                max.C = count.C

                max.L = count.L
            }
        } else {
            count.L = 1
            count.C = s
        }

    }
    return max
}

/ 预期 <kata.Result>:{C:0,L:0} 达到相等 <kata.Result>:{C:98,L:1}

我正在努力完成https://www.codewars.com/kata/586d6cefbcc21eed7a001155/train/go个 连续重复时间最长的字符 在我的测试中,它工作正常 但当我推到CW时,它不能完成弯道测试 请帮帮我 也许我可以在某个地方改进我的代码,或者是我搞错了什么

推荐答案

你的解决方案太复杂了.简单化.

func LongestRepetition(text string) Result {
    max := Result{}

    r := Result{}
    for _, c := range text {
        if r.C != c {
            r = Result{C: c}
        }
        r.L++

        if max.L < r.L {
            max = r
        }
    }

    return max
}

Time: 1737ms Passed: 2 Failed: 0
Test Results:
Fixed Tests
it should work with the fixed tests
Random Tests
it should work with the random tests
You have passed all of the tests! :) 

Go相关问答推荐

有没有更简单的方法在Go中编写这个逻辑?

try 用GitHub操作中的release标签更新version. go文件,但失败了

如何描述OpenAPI规范中围棋的数据类型.JSON?

JetBrains Goland,禁用突出显示测试文件

启动套接字服务器会干扰 gRPC/http 客户端服务器通信 Golang

在golang二进制中嵌入SvelteKit

在 Go 中解组编号的 XML 标签

AWS Lambda 中的 Websocket URL 超时达到错误

判断不同 go map 类型中的重复键

Go 的垃圾收集器在使用时删除 ZeroMQ 套接字

Golang 通过接口反映/迭代{}

go:embed 文件扩展名模式

有没有办法判断值是否满足接口中定义的类型约束?

Golang prometheus 显示自定义指标

panic :拨号 tcp:在 172.22.64.1:53 上查找 bookstoreDB:没有这样的主机

使用 image/jpeg 编码导致图像饱和/错误像素

Golang SSH客户端错误无法验证,try 的方法[无公钥],没有支持的方法

为什么在 goroutine 中声明时,benbjohnson/clock 模拟计时器不执行?

Go generics:我会在哪里使用 any 而不是 interface{}?

如何在程序退出时使用 golang 删除文件?