ByteDance | Video Onsite | Cube root
Anonymous User
4233

The simple cube root problem, let me find the cubic root for input x and error is less than k. Here is a solution. I came up with the solution but forget to add float(x). So I did not get the offer. The interview did not mention what you are supposed to return. Therefore, be carful!

Best luck for me next, and best luck for all of you ~~~

This solution idea is same as :https://leetcode.com/problems/sqrtx/ which is simple Newton's method. Love math!

def cubeRoot(x, k):
    """
    :type x: int
	:type k: float
    :rtype: float
    """
    
    temp_value = float(x)
    res = 0
    
    while abs(temp_value * temp_value * temp_value - x) > k:
        temp_value = (temp_value + x / temp_value / temp_value) / 2
        
    res = temp_value
    
    return res
	
Comments (8)