首页 > 其他分享 >Leetcode_exercise_01

Leetcode_exercise_01

时间:2024-10-14 21:11:19浏览次数:1  
标签:01 target nums int vector 哈希 字符串 Leetcode exercise

题目

两数之和

  1. 枚举所有可能的两个不同的数字之和,与 target 做比较。
  2. 哈希表查询
   // 方法一:
   class Solution {
   public:
       vector<int> twoSum(vector<int>& nums, int target) {
           int n = nums.size();
           for(int i = 0; i < n; ++i){
               for(int j = i + 1; j < n; ++j){
                   if(nums[i] + nums[j] == target){
                       return {i, j};
                   }
               }
           }
           return {};
   
       }
   };
   // 方法 2: 
   ```

```cpp
   class Solution {
   public:
       vector<int> twoSum(vector<int>& nums, int target) {
           unordered_map<int, int> hashtable;
           // 依次把 (target - nums[i]) : i 键值对放入哈希表中
           // 如果当前查询到哈希表里存在 target - nums[i] 这个 key 
           // 那么就已经存爱一对数字的和为 target 了
           // 返回 {it->second, i} 即可
           for(int i = 0; i < nums.size(); ++i){
               auto it = hashtable.find(target - nums[i]);
               if(it != hashtable.end()){ 
                   return {it->second, i};
               }
               hashtable[nums[i]] = i;
           }
           // 时间复杂度 O(n), 空间复杂度 O(n), 空间换时间
           return {};
       }
   };

用到了 unordered_map 数据结构做无序哈希表。

字母异位词分组

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        // 思考:  关键在于 如何判定两个字符串是字母异位词
        // sort 后为同一字符串 (思路一)
        // 26个字母中每个字母出现的次数都相同 (思路二)

        // 思路 1 : 将每个字符串排序后得到新的字符串 s
        // 把 s : pre_s 看作一个键值对, 用哈希表存起来
        // 创建一个 vector 里面存 vetcor<string>, 一个一个 push 进去就好 

        // 时间复杂度 : O (nklogk (k 为 strs 字符串的最大长度
        // sort 底层是用快速排序实现的时间复杂度为 O (nlogn)[n 为元素的个数, 这里就是字符串的长度]))
        // n 个字符串, sort n 遍, 插入一下哈希表 O(n)
        // 取大头 O(nklogk)

        // 空间复杂度 : O(nk) n 是字符串的数量, k 是 strs 中字符串的最大长度
        unordered_map<string, vector<string>> mp;
        for(auto &s : strs){
            string pre_s = s;
            sort(s.begin(), s.end());
            mp[s].push_back(pre_s);
        }
        vector<vector<string>> ans;
        for(auto &[_ , key] : mp){
            ans.push_back(key);
        }
        return ans;
    }
};

补充知识

unordered_map<>容器

https://deepinout.com/cpp/cpp-tutorials/g_unordered_map-in-cpp-stl.html

标签:01,target,nums,int,vector,哈希,字符串,Leetcode,exercise
From: https://www.cnblogs.com/syfhh/p/18466149/hhhh

相关文章

  • 01.单例模式设计思想
    01.单例模式设计思想目录介绍01.单例模式基础介绍1.1模式的动机1.2单例模式特点1.3单例模式定义1.4单例使用场景1.5单例模式思考02.单例模式设计思考2.1为何要用单例2.2处理资源访问冲突2.3表示全局唯一类03.如何实现单例模式3.1如何实现一个单例3......
  • [20241013]sqlplus spool与文件覆盖.txt
    [20241013]sqlplusspool与文件覆盖.txt--//这个问题在8月份遇到的问题,我发现在sqlplus下spoola.sql文件,并没有在当前目录产生a.sql文件,后来我发现建立在环境变量--//ORACLE_PATH定义的目录下,当时以为自己打开多个会话,没有注意自己工作的当前目录。事后我测试,问题视乎消失了,我再......
  • [TJOI2019] 甲苯先生的字符串
    有点水了……考虑相邻的不能放在一起,不相邻的可以,那么很容易想到转移方程:\[dp_{i,j}=\sum_{k=0}^{25}dp_{i-1,k}[j,k不相邻]\]其中\(dp_{i,j}\)表示填了\(i\)位,最后一位填\(j\)。那结合数据范围,显然矩阵快速幂。时间复杂度\(O(26^3\logn)\),可以通过#include<bits/std......
  • 【2024潇湘夜雨】WIN10_Ent-G_22H2.19045.5011软件选装纯净特别版10.14
    【系统简介】=============================================================1.本次更新母盘来自WIN10_Ent-G_22H2.19045.5011.进桌面后稍等片刻,等待后续部分优化完成。2.全程离线精简、无人值守调用优化处理制作。部分优化适配系统可能要重启几次,即使显示适配失败也不要在意,可能......
  • P4774 [NOI2018] 屠龙勇士
    典题。题目不难,注意细节就行。#include<iostream>#include<iomanip>#include<cstdio>#include<vector>#include<stack>#include<queue>#include<bitset>#include<map>#include<set>#include<unordered_map&......
  • 微服务01 ZooKeeper, Kafka
    1.4微服务1.4.6SpringCloudJAVA微服务技术Dubbo是2014年之前阿里退出的分布式系统的技术(不属于微服务)。现在主流是SpringCloudSpringCloud 官网地址:https://spring.io/projects/spring-cloud官网上实现方法有很多种,目前主流是阿里巴巴实现的方法Sprin......
  • (nice!!!)(LeetCode) 1884. 鸡蛋掉落-两枚鸡蛋(动态规划 dfs递归和递推 || 数学)
    题目:1884.鸡蛋掉落-两枚鸡蛋方法一:动态规划dp+递归dfs+记忆化搜索。时间复杂度0(n^2)。C++版本:classSolution{public: //状态sta[i]表示:i层找到f所需要的最小操作次数intsta[1010];inttwoEggDrop(intn){ //层数为0时,直接返回0if(n==0......
  • 01《程序员修炼之道》读后感
    《程序员修炼之道》是一本经典的编程书籍,作者是安德斯·哈特尔和大卫·哈特尔。这本书通过丰富的案例和深入的分析,详细探讨了程序员在职业生涯中的成长与修炼,强调了技术能力与职业素养的双重提升。以下是我对这本书的一些读后感。首先,书中首先提出了“修炼”这个概念,不论是文章还......
  • 闯关leetcode——94. Binary Tree Inorder Traversal
    大纲题目地址内容解题代码地址题目地址https://leetcode.com/problems/binary-tree-inorder-traversal/description/内容Giventherootofabinarytree,returntheinordertraversalofitsnodes’values.Example1:Input:root=[1,null,2,3]Outpu......
  • 闯关leetcode——100. Same Tree
    大纲题目地址内容解题代码地址题目地址https://leetcode.com/problems/same-tree/description/内容Giventherootsoftwobinarytreespandq,writeafunctiontocheckiftheyarethesameornot.Twobinarytreesareconsideredthesameifthey......