dongtiao1817 2019-02-22 05:50 采纳率: 100%
浏览 143
已采纳

如何找到数组中最长的字符串?

Actually I am able to get it done using two loops in Go Language, for example if I have array as:

["aa", "aab", "bcd", "a", "cdf", "bb"]

I need to return strings with maxLength. So output will be:

["aab", "bcd", "cdf"]

Here's what I am doing.

package main

import "fmt"

func allLongestStrings(inputArray []string) []string {
    maxLength := len(inputArray[0])
    outputArray := []string{}
    for _, value := range inputArray {
        if len(value) > maxLength {
            maxLength = len(value)
        }
    }
    for _, val := range inputArray {
        if len(val) == maxLength {
            outputArray = append(outputArray, val)
        }
    }
    return outputArray
}

func main() {
    xs := []string{"aa", "aab", "bcd", "a", "cdf", "bb"}
    fmt.Println(allLongestStrings(xs))
}

Is it possible to do this in one loop because I am running the same loop twice to find length and to append strings in outputArray.

Thanks In Advance.

  • 写回答

3条回答 默认 最新

  • dongyang7152 2019-02-22 05:56
    关注

    Try this:

    func allLongestStrings(inputArray []string) []string {
        max := -1 // -1 is guaranteed to be less than length of string
        var result []string
        for _, s := range inputArray {
            if len(s) < max {
                // Skip shorter string
                continue
            }
            if len(s) > max {
                // Found longer string. Update max and reset result.
                max = len(s)
                result = result[:0]
            }
            // Add to result
            result = append(result, s)
        }
        return result
    }
    

    As peterSO points out in another answer, the result slice can have a capacity larger than required and can contain string values past the length of slice. The extra allocation and string references may be a problem in some contexts (result is retained for a long time, strings are large, ...). Return a copy of the slice if the allocation and references are a concern.

    func allLongestStrings(inputArray []string) []string {
        ...
        return append([]string(nil), result...)
    }
    

    If the function can mutate the original slice, then the function result can be constructed in the input slice. This avoids the allocation of the result slice.

    func allLongestStrings(inputArray []string) []string {
        n := 0
        max := -1
        for i, s := range inputArray {
            if len(s) < max {
                // Skip shorter string
                continue
            }
            if len(s) > max {
                // Found longer string. Update max and reset result.
                max = len(s)
                n = 0
            }
            inputArray[n], inputArray[i] = inputArray[i], inputArray[n]
            n++
        }
        return inputArray[:n]
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记