【daily leetcode】20191028

新的知识点

  • 洗牌算法/随机乱置
    • https://blog.csdn.net/qq_26399665/article/details/79831490
  • 厄拉多塞筛法
  • 字符串/字符 和 整数的 相互转换

  • 384 打乱数组
  • 155 最小栈
  • 412 Fizz Buzz 【 vector fizzBuzz(int n)】
  • 204 计数质数 【 int countPrimes(int n)】
  • 326 3的幂 【 bool isPowerOfThree(int n)】
  • 13 罗马数字转整数 【 int romanToInt(string s)

384. 打乱数组

  • X % N 的范围为 0 - N-1
class Solution {
private:
    vector source;
public:
    Solution(vector& nums) {
        source = nums;
    }
    
    /** Resets the array to its original configuration and return it. */
    vector reset() {
        return source;
    }
    
    /** Returns a random shuffling of the array. */
    vector shuffle() {
        vector nums = source;
        for(int i = nums.size(); i > 0; i--){
            int pos = rand()%i;
            swap(nums[pos],nums[i-1]);
        }
        return nums;
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution* obj = new Solution(nums);
 * vector param_1 = obj->reset();
 * vector param_2 = obj->shuffle();
 */
  • 版本2
class Solution {
private:
    vector source;
public:
    Solution(vector& nums) {
        source = nums;
    }
    
    /** Resets the array to its original configuration and return it. */
    vector reset() {
        return source;
    }
    
    /** Returns a random shuffling of the array. */
    vector shuffle() {
        vector nums = source;
        for(int i = 0; i < nums.size(); i++){
            int pos = rand()%(i+1);
            swap(nums[pos],nums[i]);
        }
        return nums;
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution* obj = new Solution(nums);
 * vector param_1 = obj->reset();
 * vector param_2 = obj->shuffle();
 */
https://www.cnblogs.com/yuehouse/p/10116691.html

155. 最小栈

  • 在维护一个普通栈的同时,维护一个单调栈
  • 单调栈的维护
    • 入栈
      • 单调栈为空
      • 入栈元素 小于等于 单调栈栈顶元素 (想想小于为产生什么后果)
    • 出栈
      • 入出栈元素 恰好 是 最小元素 单调栈也执行出栈操作
其他注意的地方
  • push 操作中 if 条件语句 中 逻辑判断的顺序
  • stack.top() 返回值的类型是 什么

Parameters : No value is needed to pass as the parameter.
Return Value: Direct reference to the top element of the stack container.

class MinStack {
public:
    /** initialize your data structure here. */
    
    stack stackValue;
    stack stackMin;
    
    MinStack() {
    
           
    }
    
    void push(int x) {
        stackValue.push(x);
        if( stackMin.empty() || x <= stackMin.top())   // 注意 或条件 的 先后顺序
            stackMin.push(x);
        
    }
    
    void pop() {
        if(stackValue.top() == stackMin.top())
            stackMin.pop();
        stackValue.pop();
        
    }
    
    int top() {
        return stackValue.top();
    }
    
    int getMin() {
        return stackMin.top();
    }
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(x);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->getMin();
 */

412. Fizz Buzz

  • 字符串 和 整数 的相互转换
  • 字符 和 整数 的 相互转换
class Solution {
public:
    vector fizzBuzz(int n) {
        vector res;
        for(int i = 1; i <= n; i++){
            if(i%3 != 0 && i%5 != 0 )
            {
                res.push_back(to_string(i));
            }
            if(i%3 == 0 && i%5 != 0)
                res.push_back("Fizz");
            if(i%3 != 0 && i%5 == 0)
                res.push_back("Buzz");
            if(i%15 == 0 )
                res.push_back("FizzBuzz");
        }
        return  res;
    }
};

204. 计数质数

  • 公约数
  • 公倍数
  • 素数/质数
  • 完数

一个数为质数,那么他的倍数都不是质数

  • vector isPrimes 初始化都为质数,也就是TURE
  • 遍历的过程中 维护修改
class Solution {
public:
    int countPrimes(int n) {
        if(n < 3) return 0;
        int res = 0;
        vector isPrimes(n,true);
        for(int i =2; i < n; i++ ){
            if(isPrimes[i]){
                for(int j = i*2; j < n; j=j+i)
                {
                    isPrimes[j] = false;
                }
                res++;
            }
        }
        return res;
    }
};

326. 3的幂

  • 取余
  • 整除
class Solution {
public:
    bool isPowerOfThree(int n) {
        if( n < 1) return false;
        while(n > 1){
            int temp = n%3;  // 是3的倍数吗
            if(temp != 0)
                return false;
            n=n/3; //是的话除以3
        }
        return true;
    }
};
  • 对数大法
class Solution {
public:
    bool isPowerOfThree(int n) {
        double temp = log10(n)/log10(3.0);
        if( int(temp) - temp == 0) //判断其是不是整数
            return true;
        else
            return false;
    }
};

13. 罗马数字转整数

  • 建立哈希表,并初始化
  • 指针从头到尾遍历
    • 若 后一位 比 当前位 的映射值大,加上两者的差值,指针移动 2位
    • 否则,直接加上 当前位的 映射值, 指针移动 1位

拓展 : 12 整数转罗马数字

class Solution {
public:
    int romanToInt(string s) {
        unordered_map hash;
        hash['I'] = 1;  hash['V'] = 5;   hash['X'] = 10;
        hash['L'] = 50; hash['C'] = 100; hash['D'] = 500;
        hash['M'] = 1000;
        
        int n = s.size();
        int res = 0;
        
        for(int i = 0; i < n; i++){
            if(i != n -1 && hash[s[i+1]] > hash[s[i]]){
                res = res + hash[s[i+1]] - hash[s[i]];
                i++; // 第一次忘了
            }
            else
                res = res + hash[s[i]];
        }
        
        return res;
    }
};

错误记录

  • 最小栈


    最小栈逻辑

    报错提示

  • 计算质数


    暴力法超时

    报错

你可能感兴趣的:(【daily leetcode】20191028)