Zheaoli / do-something-right

MIT License
37 stars 3 forks source link

2022-09-19 #365

Open Zheaoli opened 2 years ago

Zheaoli commented 2 years ago

2022-09-19

Dapeus commented 2 years ago

image

dreamhunter2333 commented 2 years ago
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
/*
 * @lc app=leetcode.cn id=1636 lang=cpp
 *
 * [1636] 按照频率将数组升序排序
 */

// @lc code=start
class Solution
{
public:
    vector<int> frequencySort(vector<int> &nums)
    {
        unordered_map<int, int> nums_counter;
        for (auto &&num : nums)
            nums_counter[num]++;

        sort(nums.begin(), nums.end(), [nums_counter](const int &a, const int &b)
             {
                if (nums_counter.at(a) == nums_counter.at(b)) return a > b;
                return nums_counter.at(a) < nums_counter.at(b); });
        return nums;
    }
};
// @lc code=end

微信id: 而我撑伞 来自 vscode 插件

gongpeione commented 2 years ago
/*
 * @lc app=leetcode id=567 lang=typescript
 *
 * [567] Permutation in String
 */

// @lc code=start
function checkInclusion(s1: string, s2: string): boolean {
    const s1Map = new Array(26).fill(0);
    const s2Map = new Array(26).fill(0);

    // count and map all letters in s1 and s2
    for (let i = 0; i < s1.length; i++) {
        s1[i] && (s1Map[s1.charCodeAt(i) - 'a'.charCodeAt(0)] += 1);
        s2[i] && (s2Map[s2.charCodeAt(i) - 'a'.charCodeAt(0)] += 1);
    }

    // count same letter in s1 and s2
    let sameLetterCount = 0;
    for (let i = 0; i < 26; i++) {
        if (s1Map[i] === s2Map[i]) {
            sameLetterCount++;
        }
    }

    if (sameLetterCount === 26) {
        return true;
    }

    let left = 0;
    for (let right = s1.length; right < s2.length; right++, left++) {
        const leftIndex = s2[left].charCodeAt(0) - 'a'.charCodeAt(0);
        // if we move one step, it means that the count of left letter in s2Map need to decrease
        s2Map[leftIndex] -= 1;
        // if current count of left letter in s2Map is equal to the count in s1Map
        // we need to increase sameLetterCount
        if (s1Map[leftIndex] === s2Map[leftIndex]) {
            sameLetterCount++;
        // if previouse count of left letter in s2Map is equal to the count in s1Map
        // we need to decrease sameLetterCount
        } else if (s1Map[leftIndex] === s2Map[leftIndex] + 1) {
            sameLetterCount--;
        }

        const rightIndex = s2[right].charCodeAt(0) - 'a'.charCodeAt(0);
        // if we move one step, it means that the count of right letter in s2Map need to be increase
        s2Map[rightIndex] += 1;
        // if current count of left letter in s2Map is equal to the count in s1Map
        // we need to increase sameLetterCount
        if (s1Map[rightIndex] === s2Map[rightIndex]) {
            sameLetterCount++;
        // if previouse count of left letter in s2Map is equal to the count in s1Map
        // we need to decrease sameLetterCount
        } else if (s1Map[rightIndex] === s2Map[rightIndex] - 1) {
            sameLetterCount--;
        }
        if (sameLetterCount === 26) {
            return true;
        }
    }

    return false;
};
// @lc code=end

微信id: 弘树 来自 vscode 插件