zoukankan      html  css  js  c++  java
  • 剑指

    01二维数组的查找

    class Solution {
    public:
        bool Find(int target, vector<vector<int> > array) {
            int n=array.size(),m=array[0].size();
            int a=0,b=m-1;
            while(a<n&&b>=0){
                if(array[a][b]==target)return 1;
                if(array[a][b]<target)a++;
                else --b;
            }
            return 0;
        }
    };
    

    02替换空格

    开辟新存储空间

    class Solution {
    public:
        void replaceSpace(char *str,int length) {
            string s="";
            for(int x=0;x<length;++x){
                if(str[x]!=' ')s+=str[x];
                else s+="%20";
            }
            strcpy(str,s.c_str());
        }
    };
    

    原字符串修改

    class Solution {
    public:
    	void replaceSpace(char *str,int length) {
                int len=0,j;
                for(int i=0;i<length;++i)if(str[i]==' ')++len;
                j=length+2*len-1;
                for(int i=length-1;i>=0;--i){
                    if(str[i]==' '){
                        str[j--]='0';str[j--]='2';str[j--]='%';
                    }else str[j--]=str[i];
                }
    	}
    };
    

    03从尾到头反转链表

    不能改变链表结构

    class Solution {
    public:
        vector<int> printListFromTailToHead(ListNode* head) {
            stack<int>s;
            vector<int>v;
            for(auto p=head;p!=NULL;p=p->next)s.push(p->val);
            while(!s.empty()){
                v.push_back(s.top());
                s.pop();
            }
            return v;
        }
    };
    

    可以改变

    class Solution {
    public:
        vector<int> printListFromTailToHead(ListNode* head) {
            vector<int>v;
            for(auto p=head;p!=NULL;p=p->next)v.push_back(p->val);
            reverse(v.begin(),v.end());
            return v;
        }
    };
    

    04重建二叉树

    class Solution {
    public:
        TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
            p=pre,in=vin;
            int n=pre.size();
            TreeNode* t=build(0,n-1,0,n-1);
            return t;
        }
    private:
        vector<int>p,in;
        TreeNode* build(int prel,int prer,int inl,int inr){
            if(prel>prer)return NULL;
            TreeNode* t=new TreeNode(p[prel]);int i;
            for(i=inl;i<=inr;++i)if(in[i]==p[prel])break;
            t->left=build(prel+1,prel+i-inl,inl,i-1);
            t->right=build(prel+i-inl+1,prer,i+1,inr);
            return t;
        }
    };
    

    12 Pow(x,n)

    快速幂+迭代

    class Solution {
    public:
        double myPow(double x, int n) {
            if(n==0)return 1;
            if(n<0)return 1.0/process(x,-(long long)n);
            else return process(x,n);
        }
    private:
        double process(double x,long long n){
            if(x==0||x==1)return x;
            double s=1.0,base=x;
            while(n!=0){
                if(n&1)s*=base;
                base*=base;
                n>>=1;
            }
            return s;
        }
    };
    

    15 反转链表

    策略:在使用递推前保存pnext

    class Solution {
    public:
        ListNode* ReverseList(ListNode* pHead) {
            ListNode* pnext=NULL;
            ListNode* b=NULL;
            for(auto p=pHead;p!=NULL;p=pnext){
                pnext=p->next;
                p->next=b;
                b=p;
            }
            return b;
        }
    };
    
  • 相关阅读:
    即时通讯之环信视频语音实时通话与单聊和群聊实现
    检测.net framework 版本
    Abp(.NetCore)开发与发布过程3-部署Ubuntu站点
    Abp(.NetCore)开发与发布过程2
    Abp(.NetCore)开发与发布过程
    Redis实战与 Session缓存
    Redis 安装与初体验
    [转]gluPerspective函数
    [转]gluLookAt 函数详解
    [转]unity3D游戏开发之GUI
  • 原文地址:https://www.cnblogs.com/chanceYu/p/12702197.html
Copyright © 2011-2022 走看看