不使用乘除和pow计算一个数的平方

Calculate square of a number without using *, / and pow()

不使用乘除和pow计算一个数的平方 

给定一个整数n,在不使用乘除和pow函数的前提下计算其平方的值。

Examples:

Input: n = 5
Output: 25

Input: 7
Output: 49

Input: n = 12
Output: 144

一个直观的解法是重复地向结果加n。下面是该思路的C++实现。

// Simple solution to calculate square without
// using * and pow()
#include<iostream> ...

继续阅读

[LeetCode]Number of 1 Bits

题目描述:

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).

For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3 ...

继续阅读

[LeetCode]Reverse Bits

题目描述:

Reverse bits of a given 32 bits unsigned integer.

For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).

Follow up:
If this function is called many times, how would you optimize ...

继续阅读

Python实现整数均分

将整数m划分为n个整数,使得到的整数之间的差值不超过1,结果按照升序排列。

例如输入55, 6,返回[9, 9, 9, 9, 9, 10]。

Python代码:

def splitInteger(m, n):
    assert n > 0
    quotient = m / n
    remainder = m % n
    if remainder > 0:
        return [quotient] * (n - remainder) + [quotient + 1] * remainder
    if remainder ...

继续阅读