Convert string to number & vice versa complexity

前端 未结 5 1920
梦如初夏
梦如初夏 2021-02-05 19:42

What would be the complexity of converting a string to its equivalent number or vice versa? Does it change depending on programming language?

On the face of it, one ne

5条回答
  •  暖寄归人
    2021-02-05 20:30

    To convert to a number you always have to read all digits. So it's at least O(n).

    Now doing something like (pseudocode)

    a = 0
    foreach digit in string
    do
       a = 10 * a + digit
    end
    

    Is O(n). So the complexity is O(n)

提交回复
热议问题