备战春招——12.3 算法

哈希表

哈希表主要是使用 map、unordered_map、set、unorerdered_set、multi_,完成映射操作,主要是相应的函数。map和set是有序的,使用的是树的形式,unordered_map和unordered_set使用的是散列比表的,无序。

相应函数

set multiset

相应的地址

map multimap

相应地址

unordered_map unordered_multimap

相应位置

unordered_set unordered_multiset

相应地址

刷题

无重复字符的最长子串

暴力的哈希操作,最露比的方法,肯定可以优化的,emm,标志位方法,用一个记录上一个出现的位置,从那里开始新的暴力操作。

cpp 复制代码
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
       int  n = s.length();
       int m = 0;
       set<char> mp;
       //最露的方法,相当于暴力哈哈
        for(int i=0;i<n;i++){   
            mp.clear();
            int index = 0;
            for(int j=i;j<n;j++){
                if(mp.find(s[j])==mp.end()){
                    mp.insert(s[j]);
                    index++;
                }else{
                    break;
                }
            }
            if(index>m) m=index;
        }
        return m;
    }
};

环形链表

cpp 复制代码
class Solution {
public:
    bool hasCycle(ListNode *head) {
        set<ListNode*> s;
        while(head!=NULL){
            if(s.find(head)!=s.end()){
                return true;
            }
            s.insert(head);
            head = head->next;
        } 
        return false;
    }
};

相交链表

cpp 复制代码
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {

       set<ListNode*> d;

        while(headA!=NULL){
            d.insert(headA);
            headA=headA->next;
        }
        while(headB){
            if(d.find(headB)!=d.end()){
                return headB;
            }
            headB=headB->next;
        }
        return NULL;
    }
};

快乐数

cpp 复制代码
class Solution {
public:
    bool isHappy(int n) {
        set<int> d;
     
        while(true){
            d.insert(n);
            if(n==1)break;
            int sum = 0;
            while(n){
                int d = n%10;
                n/=10;
                sum +=d*d;
            }
            n=sum;
            if(d.find(n)!=d.end()){
                return false;
            }

        }
        return true;
    }
};
相关推荐
剪一朵云爱着19 小时前
力扣81. 搜索旋转排序数组 II
算法·leetcode·职场和发展
报错小能手1 天前
刷题日常 5 二叉树最大深度
算法
Greedy Alg1 天前
LeetCode 84. 柱状图中最大的矩形(困难)
算法
im_AMBER1 天前
Leetcode 52
笔记·学习·算法·leetcode
小欣加油1 天前
leetcode 946 验证栈序列
c++·算法·leetcode·职场和发展
包饭厅咸鱼1 天前
PaddleOCR----制作数据集,模型训练,验证 QT部署(未完成)
算法
无敌最俊朗@1 天前
C++ 并发与同步速查笔记(整理版)
开发语言·c++·算法
王哈哈^_^1 天前
【完整源码+数据集】课堂行为数据集,yolo课堂行为检测数据集 2090 张,学生课堂行为识别数据集,目标检测课堂行为识别系统实战教程
人工智能·算法·yolo·目标检测·计算机视觉·视觉检测·毕业设计
夏鹏今天学习了吗1 天前
【LeetCode热题100(66/100)】寻找两个正序数组的中位数
算法·leetcode·职场和发展