gpt4 book ai didi

algorithm - 解析表示为字符串的数字并返回满足斐波那契关系的数字数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:06:36 25 4
gpt4 key购买 nike

给定一个字符串形式的数字,返回满足 arr[i] = arr[i-1] + arr[i-2] 的数字数组。

Example:-
'112358' = [1,1,2,3,5,8]
'1111213' = [11,1,12,13]
'1111223' = [1,11,12,23]

最佳答案

解决方案的伪代码是:

input: string with size n
output: string_array[m]

for i <- 2 to n
create all possible non-empty two splitting and
generate the sequence based on those two.
if somewhere the generated sequence does not match with the input "string"
try another splitting.
else:
return the found splitting with all generated numbers.

将一个大小为k的字符串拆分成两个字符串的可能次数是k-1。此外,生成序列并检查字符串的最后部分是 O(n)。因此这个算法的时间复杂度是sum_{k = 2}^n (k-1)n = n sum_{k = 2}^n (k-1) = O(n^3).

关于algorithm - 解析表示为字符串的数字并返回满足斐波那契关系的数字数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56456249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com