0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

文字列の長さを取得する(再帰)

Posted at

思考プロセス

再帰処理で取得するには、文字列の末尾を切り取り、引数として返す。
ベースケースは文字列の要素が全て削除されたとき。

def lengthOfString(s):
    if s == '': # ベースケース
        return 0
    # l(ABCDE) = l(ABCD) + 1 のように、文字列の末尾を切り取り、引数として渡す
    return lengthOfString(s[:-1]) + 1
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?