• Leetcode (OK)242 1 219 (思路)383 202 (*)205 290 49


    383. Ransom Note(想一下思路)

    1. class Solution {
    2. public:
    3. bool canConstruct(string ransomNote, string magazine) {
    4. int record[26]{0};
    5. for(auto num:magazine){
    6. record[num-'a']++;
    7. }
    8. for(auto note:ransomNote){
    9. record[note-'a']--;
    10. }
    11. for(int i=0; i<26; i++){
    12. if(record[i] < 0)
    13. return false;
    14. }
    15. return true;
    16. }
    17. };

    205. Isomorphic Strings(*)

    1. class Solution {
    2. public:
    3. bool isIsomorphic(string s, string t) {
    4. unordered_map<char, char> mp, mp2;
    5. for (int i=0; ilength(); ++i) {
    6. if(mp[s[i]] && mp[s[i]] != t[i]) return false;
    7. if(mp2[t[i]] && mp2[t[i]] != s[i]) return false;
    8. mp[s[i]] = t[i];
    9. mp2[t[i]] = s[i];
    10. }
    11. return true;
    12. }
    13. };

    290. Word Pattern(*)

    1. class Solution {
    2. public:
    3. bool wordPattern(string pattern, string s) {
    4. vector words;
    5. string sub_s;
    6. for(auto val:s){
    7. if(val == ' '){
    8. words.push_back(sub_s);
    9. sub_s = "";
    10. }else{
    11. sub_s.push_back(val);
    12. }
    13. }
    14. words.push_back(sub_s);
    15. if(pattern.size() != words.size()) return false;
    16. unordered_map<char, string> patternToWord;
    17. unordered_set wordSet;
    18. for(int i=0; isize(); ++i){
    19. string s = words[i];
    20. char ch = pattern[i];
    21. if(!patternToWord.count(ch)){
    22. if(wordSet.count(s)) return false;
    23. patternToWord[ch] = s;
    24. }else{
    25. if(patternToWord[ch] != s) return false;
    26. }
    27. wordSet.insert(s);
    28. }
    29. return true;
    30. }
    31. };

    1.注意如何存储单词

    2.要先判断两个大小是否一样 

    242. Valid Anagram

    1. class Solution {
    2. public:
    3. bool isAnagram(string s, string t) {
    4. int record[26]{0};
    5. for(auto ch:s){
    6. record[ch-'a']++;
    7. }
    8. for(auto ch:t){
    9. record[ch-'a']--;
    10. }
    11. for(int i=0; i<26; i++){
    12. if(record[i] != 0) return false;
    13. }
    14. return true;
    15. }
    16. };

    49. Group Anagrams(*)

    1. class Solution {
    2. private:
    3. string encode(string str){
    4. vector<int> count{26, 0};
    5. for(char ch:str){
    6. count[ch-'a'] ++;
    7. }
    8. string code(count.begin(), count.end());
    9. return code;
    10. }
    11. public:
    12. vector> groupAnagrams(vector& strs) {
    13. unordered_map> codeToGroup;
    14. for(string s:strs){
    15. string code = encode(s);
    16. codeToGroup[code].push_back(s);
    17. }
    18. vector> res;
    19. for(auto group:codeToGroup){
    20. res.push_back(group.second);
    21. }
    22. return res;
    23. }
    24. };

    也可以直接用sort

    1. Two Sum

    1. class Solution {
    2. public:
    3. vector<int> twoSum(vector<int>& nums, int target) {
    4. unordered_map<int, int> record;
    5. for(int i=0; isize(); i++){
    6. int need = target-nums[i];
    7. if(record.count(need)){
    8. return {record[need], i};
    9. }
    10. record.emplace(nums[i], i);
    11. }
    12. return {};
    13. }
    14. };

    202. Happy Number(想一下思路)

    1. class Solution {
    2. private:
    3. int getSum(int n){
    4. int sum = 0;
    5. while(n >= 1){
    6. sum += (n%10) * (n%10);
    7. n /= 10;
    8. }
    9. return sum;
    10. }
    11. public:
    12. bool isHappy(int n) {
    13. unordered_set<int> record;
    14. while(1){
    15. n= getSum(n);
    16. if(n == 1) return true;
    17. if(record.count(n)) return false;
    18. record.insert(n);
    19. }
    20. }
    21. };

    219. Contains Duplicate II(OK)

    1. class Solution {
    2. public:
    3. bool containsNearbyDuplicate(vector<int>& nums, int k) {
    4. unordered_map<int, int> record;
    5. for(int i=0; isize(); i++){
    6. if(record.count(nums[i])){
    7. int dif = i-record[nums[i]];
    8. if(dif <= k) return true;
    9. }
    10. record[nums[i]] = i;
    11. }
    12. return false;
    13. }
    14. };

  • 相关阅读:
    三层神经网络模型
    自从看了谷歌大神拼S强撸的Spring源码笔记,我从渣渣练成了钢铁
    驱动隐藏进程(eprocess断链)
    蓝桥每日一题2023.10.12
    【电商项目实战】用户注册(详细篇)
    【图像分割】距离正则化水平集演化及其在图像分割中的应用(Matlab代码实现)
    项目经验-查询现网调用情况的实践
    VueX
    Spring 如何使用JdbcTemplate类进行数据库操作呢?
    机器学习 - 机器学习理论基础
  • 原文地址:https://blog.csdn.net/Zoeyii/article/details/133151132