It seems that coding tests are conducted overseas in interviews with engineers, and in many cases, the main thing is to implement specific functions and classes according to the theme.
As a countermeasure, it seems that a site called Let Code will take measures.
A site that trains algorithmic power that can withstand coding tests that are often done in the home.
I think it's better to have the algorithm power of a human being, so I'll solve the problem irregularly and write down the method I thought at that time as a memo.
Basically, I would like to solve the easy acceptance in descending order.
Last time Leet Code Day5 starting from zero "1266. Minimum Time Visiting All Points"
1342. Number of Steps to Reduce a Number to Zero
If the given natural number num
is odd, subtract 1 and if it is even, divide by 2. The problem is to create a function that counts the number of times it is repeated until it reaches 0 and returns that number.
For the time being, write something that came to your mind.
class Solution:
def numberOfSteps (self, num: int) -> int:
steps = 0
while num > 0:
if num % 2 == 0:
num //= 2
else:
num -=1
steps += 1
return steps
# Runtime: 28 ms, faster than 68.90% of Python3 online submissions for Number of Steps to Reduce a Number to Zero.
# Memory Usage: 13.8 MB, less than 100.00% of Python3 online submissions for Number of Steps to Reduce a Number to Zero.
After I wrote it, I looked at other people's discussions, but nothing was so strange. Because of its simplicity, they may all have similar answers.
~~ I honestly don't write much about this issue ... ~~
Recommended Posts