C++ 刷题记录 No.5 (tree, graph, bit manipulation)

11 篇文章 0 订阅
6 篇文章 0 订阅
本文记录了C++编程在LeetCode中的解题心得,重点涉及位操作题目,如190、338、191和1356题,要求按1比特的数量排序整数。同时,还探讨了数学与逻辑谜题,包括罗马数字转换、消除游戏和字符串中的排列等挑战,其中2166题虽非位操作但颇具技巧性。
摘要由CSDN通过智能技术生成

Bit Manipulation

190, 338, 191

1356. Sort Integers by The Number of 1 Bits

class Solution {
public:
    static int cnt1(int num) {
        int cnt = 0;
        while (num) {
            cnt += num % 2;
            num = num / 2;
        }
        return cnt;
    }
    
    static bool wayToSort(int i, int j) {
        int cntA = cnt1(i);
        int cntB = cnt1(j);
        if (cntA == cntB) {
            return i < j;
        }
        return cntA < cntB;
    }
    vector<int> sortByBits(vector<int>& arr) {
        sort(arr.begin(), arr.end(), wayToSort);
        return arr;
    }
};

Math & Logic Puzzles

Rotation Matrix

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        
        reverse(matrix.begin(), matrix.end());
        for (int i = 0; i < matrix.size(); i++) {
            for (int j = i + 1; j < matrix.size(); j++) {
                swap(matrix[i][j], matrix[j][i]);
            }
        }
    }
};

general solution

13. Roman to Integer

390. Elimination Game

class Solution {
public:
    int lastRemaining(int n) {
        if (n == 1) {
            return 1;
        }
        return 2*(1 + n / 2 - lastRemaining(n / 2));
    }
};

2166. Design Bitset

这个根本不是位操作,但是很 tricky

class Bitset {
public:
    int num;
    string bitset;
    int cnt1;
    bool flag;
    
    Bitset(int size) {
        num = size;
        cnt1 = 0;
        bitset = string(num, '0');
        flag = false;
    }
    
    void fix(int idx) {
        if (flag == false) {
            if (bitset[idx] == '0') {
                bitset[idx] = '1';
                cnt1++;
            }
        }
        else {
            if (bitset[idx] == '1') {
                bitset[idx] = '0';
                cnt1++;
            }
        }
    }
    
    void unfix(int idx) {
        if (flag == false) {
            if (bitset[idx] == '1') {
                bitset[idx] = '0';
                cnt1--;
            }
        }
        else {
            if (bitset[idx] == '0') {
                bitset[idx] = '1';
                cnt1--;
            }
        }
    }
    
    void flip() {
        if (flag == false) {
            flag = true;
        } else {
            flag = false;
        }
        cnt1 = num - cnt1;
    }
    
    bool all() {
        return (cnt1 == num);
    }
    
    bool one() {
        return (cnt1 != 0);
    }
    
    int count() {  
        return cnt1;
    }
    
    string toString() {
        string result;
        if (flag == false)
            result = bitset;
        else {
            for (int i = 0; i < num; i++) {
                char c = (bitset[i] == '0') ? '1' : '0';
                result.push_back(c);
            }
        }
        return result;
    }
};

/**
 * Your Bitset object will be instantiated and called as such:
 * Bitset* obj = new Bitset(size);
 * obj->fix(idx);
 * obj->unfix(idx);
 * obj->flip();
 * bool param_4 = obj->all();
 * bool param_5 = obj->one();
 * int param_6 = obj->count();
 * string param_7 = obj->toString();
 */

567. Permutation in String

这道题非常有趣,key idea 是 s1 每个字符的 count 在 一个 sliding window 中数量一致。注意字符种类的限制。

先统计 pattern 中各个字符的 frequency,然后一边 slide 一边 judge。

class Solution {
public:
    bool checkInclusion(string s1, string s2) {
        vector<int> goal(26), cur(26);
        for (auto c: s1) {
            goal[c - 'a']++;
        }
        for (int i = 0; i < s2.size(); i++) {
            cur[s2[i] - 'a']++;
            if (i >= s1.size()) {
                cur[s2[i - s1.size()] - 'a']--;
            }
            if (cur == goal) {
                return true;
            }
        }
        return false;
    }
};

438. Find All Anagrams in a String

Almost the same problem.

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int> pattern(26), cur(26);
        vector<int> result;
        for (auto c: p) {
            pattern[c - 'a']++;
        }
        for (int i = 0; i < s.size(); i++) {
            cur[s[i] - 'a']++;
            if (i >= p.size()) {
                cur[s[i - p.size()] - 'a']--;
            }
            if (pattern == cur) {
                result.push_back(i - p.size() + 1);
            }
        }
        return result;
    }
};

1220. Count Vowels Permutation

Graph!

class Solution {
public:
    int countVowelPermutation(int n) {
        vector<vector<long>> dp(n, vector<long>(5, 0));
        for (int i = 0; i < 5; i++) {
            dp[0][i] = 1;
        }
        int MOD = 1e9 + 7;
        for (int i = 1; i < n; i++) {
            dp[i][0] = (dp[i - 1][1] + dp[i - 1][2] + dp[i - 1][4]) % MOD;
            dp[i][1] = (dp[i - 1][0] + dp[i - 1][2]) % MOD;
            dp[i][2] = (dp[i - 1][1] + dp[i - 1][3]) % MOD;
            dp[i][3] = (dp[i - 1][2]) % MOD;
            dp[i][4] = (dp[i - 1][2] + dp[i - 1][3]) % MOD;
        }
        long res = 0;
        for (int i = 0; i < 5; i++) {
            res = (res + dp[n - 1][i]) % MOD;
        }
        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值