四则运算
Pow(x, n)
Implement pow(x, n), which calculates x raised to the power n (xn).
1 | Input: 2.00000, 10 |
代码
1 | class Solution { |
Divide Two Integers
Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.
Return the quotient after dividing dividend by divisor.
The integer division should truncate toward zero.
Example 1:1
2Input: dividend = 10, divisor = 3
Output: 3
Example 2:1
2Input: dividend = 7, divisor = -3
Output: -2
1 | // 第一种思路是使用log |
1 | // 第二种思路是dividend = 2^k1*divisor + 2^k2*divisor + ... |
Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
Given a = 1 and b = 2, return 3.
代码
1 | class Solution { |
前缀相关
Power of Two
Given an integer, write a function to determine if it is a power of two.1
2
3Input: 1
Output: true
Explanation: 20 = 1
代码
1 | class Solution { |
Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.
1 | Input: [5,7] |
代码
1 | class Solution { |
Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
1 | Input: 5 |
代码
1 | class Solution { |
位个数和位移动操作
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).
代码
1 | class Solution { |
Reverse Bits
Reverse bits of a given 32 bits unsigned integer.
1 | Input: 43261596 |
1 | class Solution { |
Total Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Now your job is to find the total Hamming distance between all pairs of the given numbers.
1 | Input: 4, 14, 2 |
代码
1 | class Solution { |
重复使用抑或操作
在同一个整型上重复偶数次抑或同一个数会导致该数消失,可以使用抑或操作进行一些变化或引导一些变化
Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.1
2
3
4
5
6
7Example 1:
Input: [3,0,1]
Output: 2
Example 2:
Input: [9,6,4,2,3,5,7,0,1]
Output: 8
代码
1 | class Solution { |
Single Number
Given a non-empty array of integers, every element appears twice except for one. Find that single one.1
2
3
4
5
6
7Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
代码
1 | class Solution { |
Single Number II
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
1 | Example 1: |
代码
1 | class Solution { |
Single Number III
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.
1 | Example: |
代码
1 | class Solution { |
与位操作相关的数学题
与位操作相关的数学题通常需要找规律或者先证明,此部分也会不断补充…
Integer Replacement
1 | Given a positive integer n and you can do operations as follow: |
代码
1 | class Solution { |