Skip to content

Latest commit

 

History

History
187 lines (130 loc) · 4.89 KB

其他.md

File metadata and controls

187 lines (130 loc) · 4.89 KB

其他类总结

Nim游戏

LeetCode中文

LeetCode英文

你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头。 拿掉最后一块石头的人就是获胜者。你作为先手。

你们是聪明人,每一步都是最优解。 编写一个函数,来判断你是否可以在给定石头数量的情况下赢得游戏。

示例:

输入: 4
输出: false 
解释: 如果堆中有 4 块石头,那么你永远不会赢得比赛;
     因为无论你拿走 1 块、2 块 还是 3 块石头,最后一块石头总是会被你的朋友拿走。

解答

可以找出规律:当石头的个数不是4的整数倍时,先手输;否则,先手胜利。那么只需要判断石头数n对4取余是否为0即可。

class Solution {
public:
    bool canWinNim(int n) {
        return n % 4;
    }
};

四数相加II

LeetCode中文

LeetCode英文

给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0

为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N,且 0 ≤ N ≤ 500 。所有整数的范围在 -228 到 228 - 1 之间,最终结果不会超过 231 - 1 。

例如:

输入:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]

输出:
2

解释:
两个元组如下:
1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0

解答

如果直接暴力求解,遍历四个数组的元素来穷举所有,那么将会超时(时间复杂度:O(n4) )。可以利用哈希表mp,用空间换时间,建立数组A和数组B中的元素之和a+b与出现次数之间的映射,然后同时遍历数组C的元素c和数组D的元素d,如果0-c-d在哈希表中存在,则将它在哈希表中的映射(即出现次数)加到结果。遍历完数组A和数组B的所有元素,就得到了结果。

  • 时间复杂度:O(n2)
  • 空间复杂度:O(n2)
class Solution {
public:
    int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
        unordered_map<int,int> mp;
        for(auto a : A)
        {
            for(auto b : B)
            {
                mp[a+b]++;
            }
        }
        
        int cnt = 0;
        for(auto c : C)
        {
            for(auto d : D)
            {
                if(mp.find(-c-d) != mp.end())
                    cnt += mp[-c-d];
            }
        }
        
        return cnt;
    }
};

打乱数组

LeetCode中文

LeetCode英文

打乱一个没有重复元素的数组。

示例:

// 以数字集合 1, 2 和 3 初始化数组。
int[] nums = {1,2,3};
Solution solution = new Solution(nums);

// 打乱数组 [1,2,3] 并返回结果。任何 [1,2,3]的排列返回的概率应该相同。
solution.shuffle();

// 重设数组到它的初始状态[1,2,3]。
solution.reset();

// 随机返回数组[1,2,3]打乱后的结果。
solution.shuffle();

解答

利用优化的洗牌算法:该算法类似于插入排序

  1. 从数组的最后一个数(下标为i = len - 1)开始,进行随机取余(除数为i + 1,确保下标不过界);

  2. 将随机得到的下标对应的元素和最后一个数nums[i]交换;

  3. 将最后一个数nums[i]不再视为数组中的元素,继续循环。

重复以上步骤,直到遍历到第一个元素为止。

  • 时间复杂度:O(n)
  • 空间复杂度:O(1)
class Solution {
public:
    Solution(vector<int> nums) : vec(nums){
        srand(time(0));
    }
    
    /** Resets the array to its original configuration and return it. */
    vector<int> reset() {
        return vec;
    }
    
    /** Returns a random shuffling of the array. */
    vector<int> shuffle() {
        vector<int> res(vec);
        
        int len = res.size();
        for(int i = len-1;i >= 1;i--)
        {
            int rdm = rand() % (i + 1);
            swap(res[rdm],res[i]);
        }
        
        return res;
    }
    
private:
    vector<int> vec;
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(nums);
 * vector<int> param_1 = obj.reset();
 * vector<int> param_2 = obj.shuffle();
 */