相关疑难解决方法(0)

实现基于整数的幂函数pow(int,int)的最有效方法

将整数提升到C中另一个整数的幂的最有效方法是什么?

// 2^3
pow(2,3) == 8

// 5^5
pow(5,5) == 3125
Run Code Online (Sandbox Code Playgroud)

c algorithm math exponentiation

239
推荐指数
8
解决办法
19万
查看次数

幂函数是否在恒定时间内运行?

当我使用 C# 中的 Math.Pow(double x, double y) 或 C++ 中的 math.h pow 函数等幂函数时,这些函数是否以恒定时间运行?

我问的原因是因为我想知道形式 (1-t)^n*p0 + ... + t^(n) * pN 上的“预先计算的”贝塞尔函数是否可以在线性时间内运行,这可以然后比以控制点和 t 作为参数的 De Casteljaus 算法的实现更快。

math bezier time-complexity

5
推荐指数
1
解决办法
3204
查看次数

找到带有O(1)空格和O(n)时间的重复数字

我在leetcode中解决了一个问题

给定包含n + 1个整数的数组nums,其中每个整数在1和n之间(包括1和n),证明必须存在至少一个重复的数字.假设只有一个重复的数字,在O(n)时间和O(1)空间复杂度中找到重复的数字

class Solution(object):
    def findDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        xor=0
        for num in nums:
            newx=xor^(2**num)
            if newx<xor:
                return num
            else:
                xor=newx
Run Code Online (Sandbox Code Playgroud)

我接受了解决方案,但我被告知它既不是O(1)空间也不是O(n)时间.

谁能帮助我理解为什么?

python algorithm data-structures

2
推荐指数
1
解决办法
796
查看次数