atoi等各种小函数的用法需要学习,及时更新在这个贴子里


1:atoi string.c_str()函数的基本使用
string str = "10"; //string转数字
int res = atoi(str.c_str());
2:  链表快速排序和插入排序问题
    2.1 单链表快速排序
    //1216 leetcode 148
    //这个也是链表排序 但是要求 在 O(n log n) 时间复杂度和常数级空间复杂度下
    ListNode* sortList_Leetcode148(ListNode* head) {
        if((head == NULL) || (head != NULL && head->next == NULL)) return head;   
        quickSort_Leetcode148(head,NULL);
        return head;
    }

    ListNode* partitions_QuickSortListNodeLeetcode148(ListNode* start,ListNode* end)
    {
        ListNode* p = start;
        ListNode* q = p->next;
        int value = p->val;
        while (q != end)
        {
            if(q->val < value)
            {
                p = p->next;

                int tmp = p->val;
                p->val = q->val;
                q->val = tmp;
            }
            q = q->next;
        }

        int tmp = p->val;
        start->val = tmp;
        p->val = tmp;
        return p;
    }

    void quickSort_Leetcode147(ListNode* start,ListNode* end)
    {
        if(start != NULL && start->next != NULL && start != end)
        {
            ListNode* mid = partitions_QuickSortListNodeLeetcode148(start,end);
            quickSort_Leetcode148(start,mid);
            quickSort_Leetcode148(mid->next,end);
        }
    }
    2.2  单链表插入排序
    ListNode* sortList(ListNode* head) {
        if((head == NULL) || (head != NULL && head->next == NULL)) return head;   
        ListNode* dumy = new ListNode(0);
        dumy->next = new ListNode(head->val);
        ListNode* cur = dumy->next;
        ListNode* pre = dumy;
        while (head->next != NULL)
        {
            pre = dumy;
            cur = pre->next;
            ListNode* tmp = new ListNode(head->next->val);
            if(tmp->val < cur->val)
            {
                tmp->next = cur;
                pre->next = tmp;
            }
            else
            {
                while ((cur != NULL) && (cur->val < tmp->val))
                {
                    cur = cur->next;
                    pre = pre->next;
                }
                pre->next = tmp;
                tmp->next = cur;
            }
            head = head->next;
        }
        return dumy->next;

    }
3:
unordered_map迭代器失效
    unordered_map<int,int> unmap;
    unmap.insert(pair<int,int>(1,2));
    unmap.insert(pair<int,int>(3,6));
    unmap.insert(pair<int,int>(5,9));

    for(auto it = unmap.begin();it != unmap.end();it++)
    {
        cout<<it->first<<" "<<it->second<<endl;
    }

    for(auto it = unmap.begin();it != unmap.end();)
    {
        if(it->first == 3) unmap.erase(it++); //unordered_map 迭代器失效
        else
        {
            it++;
        }
        
    }
    cout<<"after erase"<<endl;
    for(auto it = unmap.begin();it != unmap.end();it++)
    {
        cout<<it->first<<" "<<it->second<<endl;
    }
4: LRU缓存机制 leetcod 146
先放测试用例
   /*
    cout<<"null ";
    LRUCache* lru = new LRUCache(10);
    lru->put(10,13);
    lru->put(3,17);
    lru->put(6,11);
    lru->put(10,5);
    lru->put(9,10);
    lru->get(13);
    lru->put(2,19);
    lru->get(2);
    lru->get(3);
    lru->put(5,25);
    lru->get(8);
    lru->put(9,22);
    lru->put(5,5);
    lru->put(1,30);
    lru->get(11);
    lru->put(9,12);
    lru->get(7);
    lru->get(5);
    lru->get(8);
    lru->get(9);
    lru->put(4,30);
    lru->put(9,3);
    lru->get(9);
    lru->get(10);
    lru->get(10);
    lru->put(6,14);
    lru->put(3,1);
    lru->get(3);
    lru->put(10,11);
    lru->get(8);
    lru->put(2,14);
    lru->get(1);
    lru->get(5);
    lru->get(4);
    lru->put(11,4);
    lru->put(12,24);
    lru->put(5,18);
    lru->get(13);
    lru->put(7,23);
    lru->get(8);
    lru->get(12);
    lru->put(3,27);
    lru->put(2,12);
    lru->get(5);
    lru->put(2,9);
    lru->put(13,4);
    lru->put(8,18);
    lru->put(1,7);
    lru->get(6);
    lru->put(9,29);
    lru->put(8,21);
    lru->get(5);
    lru->put(6,30);
    lru->put(1,12);
    lru->get(10);
    lru->put(4,15);
    lru->put(7,22);
    lru->put(11,26);
    lru->put(8,17);
    lru->put(9,29);
    lru->get(5);
    lru->put(3,4);
    lru->put(11,30);
    lru->get(12);
    lru->put(4,29);
    lru->get(3); //从这个地方开始错的
    lru->get(9);
    lru->get(6);
    lru->put(3,4);
    lru->get(1);
    lru->get(10);
    lru->put(3,29);
    lru->put(10,28);
    lru->put(1,20);
    lru->put(11,13);
    lru->get(3);
    lru->put(3,12);
    lru->put(3,8);
    lru->put(10,9);
    lru->put(3,26);
    lru->get(8);
    lru->get(7);
    lru->get(5);
    lru->put(13,17);
    lru->put(2,27);
    lru->put(11,15);
    lru->get(12);
    lru->put(9,19);
    lru->put(2,15);
    lru->put(3,16);
    lru->get(1);
    lru->put(12,17);
    lru->put(9,1);
    lru->put(6,19);
    lru->get(4);
    lru->get(5);
    lru->get(5);
    lru->put(8,1);
    lru->put(11,7);
    lru->put(5,2);
    lru->put(9,28);
    lru->get(1);
    lru->put(2,2);
    lru->put(7,4);
    lru->put(4,22);
    lru->put(7,24);
    lru->put(9,26);
    lru->put(13,28);
    lru->put(11,26);
    */

再放源代码,这个我这个超时了。
//20_1216 LRU
class LRUCache {
public:
    // 这个超时了。/
    LRUCache(int capacity) {
        capacitys = capacity;
        count = 0;
        matches.clear();
    }
    
    int get(int key) {
        if(matches.find(key) == matches.end()) 
        {
            cout<<-1<<" ";
            return -1;
        }
        queue<int> q1;

        int cur = key;
        while (!q.empty())
        {
            int tmp = q.front();
            q.pop();
            if(tmp == cur) continue;
            else
            {
                q1.push(tmp);
            }
            
        }
        q1.push(cur);
        q = q1;
        
        cout<<matches[key]<<" ";
        return matches[key]; 
    }
    
    void put(int key, int value) {
        queue<int> q1;
        int cur = key;

        if(matches.find(key) == matches.end())
        {
            count++;
            if(count > capacitys) count = capacitys+1;
        }

        if(count > capacitys)
        {
            int tmp = q.front();
            q.pop();
            q.push(key);
            matches[key] = value;
            for(auto it = matches.begin(); it != matches.end();)
            {
                if(it->first == tmp) matches.erase(it++);
                else it++;
            }
            count--;
        }
        else
        {
            matches[key] = value;
            
            while (!q.empty())
            {
                int tmp = q.front();
                q.pop();
                if(tmp == cur) continue;
                else
                {
                    q1.push(tmp);
                }
                
            }
            q1.push(cur);
            q = q1;

        }
        cout<<"null "; 
    }

    int count;
    int capacitys;
    unordered_map<int,int> matches;
    queue<int> q;

};

5:非递归遍历二叉树,前序遍历和中序,非递归后序有点难,暂时不写
先放用例
    Solution solution;
    TreeNode* tree10 = new TreeNode(7);
    TreeNode* tree12 = new TreeNode(10);
    TreeNode* tree11 = new TreeNode(9,NULL,tree12);

    TreeNode* tree1 = new TreeNode(4,tree10,tree11);
    TreeNode* tree2 = new TreeNode(5);
    TreeNode* tree3 = new TreeNode(6);
    TreeNode* tree4 = new TreeNode(2,tree1,tree2);
    TreeNode* tree5 = new TreeNode(3,NULL,tree3);
    TreeNode* tree6 = new TreeNode(1,tree4,tree5);
    TreeNode* root = tree6;

    

    vector<vector<int>> travelFloorRes = solution.travelTreeNodeByFloor(root);
    for(int i =0;i<travelFloorRes.size();i++)
    {
        for(int j =0;j<travelFloorRes[i].size();j++)
        {
            cout<<travelFloorRes[i][j]<<" ";
        }
        cout<<endl;
    }

    cout<<"middle travel TreeNode by stack is "<<endl;
    vector<int> middleTravelRes = solution.middleTravel(root);
    for_each(middleTravelRes.begin(),middleTravelRes.end(),[](auto x){cout<<x<<" ";});
    cout<<endl;

    cout<<"pre travel TreeNode by stack is "<<endl;
    vector<int> preTravelRes = solution.preorderTraversal(root);
    for_each(preTravelRes.begin(),preTravelRes.end(),[](auto x){cout<<x<<" ";});
    cout<<endl; 
然后是代码
    //1218 非递归解法前序遍历
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        if(root == NULL) return res;
        stack<TreeNode*> s;
        TreeNode* p = root;
        while (!s.empty() || p)
        {
            while (p)
            {
                //cout<<"cur val is "<<p->val<<endl;
                res.push_back(p->val);
                s.push(p);
                p = p->left;
            }
            if(!s.empty())
            {
                TreeNode* tmp = s.top();
                s.pop();
                p = tmp->right;
            }
            
        }
        
        return res;
    }

    vector<int> middleTravel(TreeNode* root)
    {
        vector<int> res;
        if(root == NULL) return res;
        stack<TreeNode*> s;
        TreeNode* p = root;
        while (!s.empty() || p != NULL)
        {
            while (p != NULL)
            {
                s.push(p);
                p = p->left;
            }

            if(!s.empty())
            {
                TreeNode* tmp = s.top();
                s.pop();
                res.push_back(tmp->val);
                p = tmp->right;
            }
        }
        return res;
    }

3. STL巧妙小函数使用
    判断vector等容器是否含有某个数,代码如下
    
    cout<<"In main test "<<endl;
    vector<int> v2 = {10,12,14,16};
    auto pos = std::find(v2.begin(),v2.end(),100);
    if(pos != v2.end()) cout<<"v2 find yes"<<endl;
    else cout<<"v2 no"<<endl;
4. 

map的key如果是结构体需要注意什么问题

map中的key默认是以less<>升序对元素排序(排序准则也可以修改),也就是说key必须具备operator<对元素排序,而平常我们的用的基本上都是基本类型元素作为key,所以就不存在这个问题了

所以要对结构体中<号进行重载操作才行

重载运算符应该注意重载小于号要有2个const,注意后面的const。










全部评论

相关推荐

避坑恶心到我了大家好,今天我想跟大家聊聊我在成都千子成智能科技有限公司(以下简称千子成)的求职经历,希望能给大家一些参考。千子成的母公司是“同创主悦”,主要经营各种产品,比如菜刀、POS机、电话卡等等。听起来是不是有点像地推销售公司?没错,就是那种类型的公司。我当时刚毕业,急需一份临时工作,所以在BOSS上看到了千子成的招聘信息。他们承诺无责底薪5000元,还包住宿,这吸引了我。面试的时候,HR也说了同样的话,感觉挺靠谱的。于是,我满怀期待地等待结果。结果出来后,我通过了面试,第二天就收到了试岗通知。试岗的内容就是地推销售,公司划定一个区域,然后你就得见人就问,问店铺、问路人,一直问到他们有意向为止。如果他们有兴趣,你就得摇同事帮忙推动,促进成交。说说一天的工作安排吧。工作时间是从早上8:30到晚上18:30。早上7点有人叫你起床,收拾后去公司,然后唱歌跳舞(销售公司都这样),7:55早课(类似宣誓),8:05同事间联系销售话术,8:15分享销售技巧,8:30经理训话。9:20左右从公司下市场,公交、地铁、自行车自费。到了市场大概10点左右,开始地推工作。中午吃饭时间大约是12:00,公司附近的路边盖饭面馆店自费AA,吃饭时间大约40分钟左右。吃完饭后继续地推工作,没有所谓的固定中午午休时间。下午6点下班后返回公司,不能直接下班,需要与同事交流话术,经理讲话洗脑。正常情况下9点下班。整个上班的一天中,早上到公司就是站着的,到晚上下班前都是站着。每天步数2万步以上。公司员工没有自己的工位,百来号人挤在一个20平方米的空间里听经理洗脑。白天就在市场上奔波,公司的投入成本几乎只有租金和工资,没有中央空调。早上2小时,晚上加班2小时,纯蒸桑拿。没有任何福利,节假日也没有3倍工资之类的。偶尔会有冲的酸梅汤和西瓜什么的。公司的晋升路径也很有意思:新人—组长—领队—主管—副经理—经理。要求是业绩和团队人数,类似传销模式,把人留下来。新人不能加微信、不能吐槽公司、不能有负面情绪、不能谈恋爱、不能说累。在公司没有任何坐的地方,不能依墙而坐。早上吃早饭在公司外面的安全通道,未到上班时间还会让你吃快些不能磨蹭。总之就是想榨干你。复试的时候,带你的师傅会给你营造一个钱多事少离家近的工作氛围,吹嘘工资有多高、还能吹自己毕业于好大学。然后让你早点来公司、无偿加班、抓住你可能不会走的心思进一步压榨你。总之,大家在找工作的时候一定要擦亮眼睛,避免踩坑!———来自网友
qq乃乃好喝到咩噗茶:不要做没有专业门槛的工作
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务