zoukankan      html  css  js  c++  java
  • leetcode刷题收获

    引言

       到目前为止,在leetcode上刷了近二十题,如果就这么刷下去,以下好处自然会有:coding熟练度的提升、STL的使用、思考和解决问题的熟练度。
       然而,数据结构和算法的进一步学习、未接触过的新的c++语言特性的学习、改掉编程中的陋习等等很难在刷题中达成,因此,需要有目标的刷题。

    刷题过程

    1. 追求效率,如果时间复杂度足够优化,排名前10%应该是没问题的
    2. 时间复杂度优化不出来的题,评论区看大佬的解答,然后想明白,最好自己再实现一遍
    3. 尝试使用未实践过的数据结构、算法、C++语言特性。
    4. 发现自己coding中的陋习,或是可改进的地方,对自己coding 能力的提升大有裨益

    技巧收获汇总

    1. 边界情况要主动处理好,比在调试的时候去纠正省时的多

    for example: 19. Remove Nth Node From End of List
    Given a linked list, remove the n-th node from the end of list and return its head.

    //Solution in one pass
    class Solution {
    public:
        ListNode* removeNthFromEnd(ListNode* head, int n) {
            //if(head->next==NULL) return NULL;
            vector<ListNode*> vl;
            ListNode *p=head;
            while(p!=NULL)
            {
                vl.push_back(p);
                p=p->next;
            }
            int i=vl.size()-n;
            if(i==0) return head->next;
            else if(i==vl.size()-1) vl[i-1]->next=NULL;
            else vl[i-1]->next=vl[i+1];
            return head;
        }
    };
    

      题中要求删除的位置的边界情况是,首元素 head 和 末尾元素,head 的特征是没有前驱元素, 末尾元素的特征是没有后继元素;这两种情况的处理和中间元素是不一样的。

    2. 使用STL提供的容器有时会影响效率

    for example: 20. Valid Parentheses
    我首先用map做了一遍,耗时12ms

    class Solution {
    public:
        bool isValid(string s) {
            stack<char> st;
            map<char,char> zuo;
            zuo[')']='(';
            zuo[']']='[';
            zuo['}']='{';
            int i;
            for(i=0;i<s.length();i++)
            {
                if(s[i]=='(' || s[i]=='{' || s[i]=='[') st.push(s[i]);
                else if(!st.empty() && st.top()==zuo[s[i]]) st.pop();
                else break;
            }
            if(st.empty() && i==s.length()) return true;
            else return false;
        }
    };
    

      然后用数组做了一遍,耗时4ms

    class Solution {
    public:
        bool isValid(string s) {
            stack<char> st;
            char zuo[300];
            zuo[')']='(';
            zuo[']']='[';
            zuo['}']='{';
            int i;
            for(i=0;i<s.length();i++)
            {
                if(s[i]=='(' || s[i]=='{' || s[i]=='[') st.push(s[i]);
                else if(!st.empty() && st.top()==zuo[s[i]]) st.pop();
                else break;
            }
            if(st.empty() && i==s.length()) return true;
            else return false;
        }
    };
    
  • 相关阅读:
    oracle 11g ocp 笔记(7)-- DDL和模式对象
    oracle 11g ocp 笔记(6)-- oracle安全
    oracle 11g ocp 笔记(5)-- oracle存储结构
    oracle 11g ocp 笔记(4)-- 网络服务
    oracle 11g ocp 笔记(3)-- 实例管理
    oracle 11g ocp 笔记(2)-- 安装和创建数据库
    oracle 11g ocp 笔记(1)-- oracle 11g体系结构概述
    https://blog.csdn.net/gyming/article/details/46611369
    AWR管理
    3、实例管理
  • 原文地址:https://www.cnblogs.com/yhjd/p/10654786.html
Copyright © 2011-2022 走看看