力扣 481. 神奇字符串

解法: 暴力

1
2
3
4
5
6
7
8
9
10
11
12
def magicalString(n):
s = "122"
if n <= 3:
return 1
ret, i, add = 1, 2, "1"
while i < n:
if s[i] == "1":
ret += 1
s += add * int(s[i])
add = "1" if add == "2" else "2"
i += 1
return ret
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信