码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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. };

  • 相关阅读:
    【C++11】shared_ptr
    现代C++(Modern C++)基本用法实践:三、移动语义
    【NOIP2018模拟10.30】Idioms 题解
    2022-6-29 最大二叉树,根据前序和后序遍历构造二叉树,将子数组重新排序得到同一个二叉查找树的方案数
    从git主分支创建新分支进行开发的流程及需要注意的问题
    Apriori算法(原理步骤、Python实现、apyori库实现)
    【Matlab】状态空间模型的极点配置法 place() 函数
    组件化开发之如何封装组件-react
    线性代数学习笔记3-3:逆矩阵的理解
    解析mfc100u.dll文件丢失的修复方法,快速解决mfc100u.dll问题
  • 原文地址:https://blog.csdn.net/Zoeyii/article/details/133151132
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号