Back to course home
0% completed
Vote For New Content
Without a stack. Arguably same space complexity. One less iteration needed
Leopoldo Hernandez
Feb 7, 2024
class Solution2: def decimalToBinary(self, num): # 2 Approach two is to simply create a string with the remainders on our first loop new_num = '' while num > 0: new_num = str(num % 2) + new_num num = num // 2 return new_num
0
0
Comments
Comments
L
Lee 2 years ago
be careful. String concatenation in a loop like this makes your code n squared. See this article: [https://dev.to/fayomihorace/python-how-simple-string-concatenation-can-kill-your-code-performance-2636](https://dev.to/fayomihorace/python-how-simple-string-concatenation-...
On this page