【剑指offer】部分题目

1.一个整型数组里除了两个数字之外,其他的数字都出现了偶数次。请写程序找出这两个只出现一次的数字。

class Solution {
public:
    void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) {
        if(data.size()<2)return;
        int tem=0;
        for(int i=0;i<data.size();++i)
            tem^=data[i];
        if(tem==0)return;
        int x=1;
        while((x&tem)==0)x<<=1;
        *num1=0;
        *num2=0;
        for(int i=0;i<data.size();i++)
        {
            if(x&data[i])
                *num1^=data[i];
            else
                *num2^=data[i];
        }
        return ;
    }
};

2.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!

class Solution {
public:
    string LeftRotateString(string str, int n) {
        int len=str.length();
        if(len==0)return str;
        n%=len;
        for(int i=0,j=n-1;i<j;i++,j--)swap(str[i],str[j]);
        for(int i=n,j=len-1;i<j;i++,j--)swap(str[i],str[j]);
        for(int i=0,j=len-1;i<j;i++,j--)swap(str[i],str[j]);
        return str;
    }
};

3.约瑟夫问题

class Solution {
public:
    int LastRemaining_Solution(int n, int m)
    {
        return YSF(n,m);
    }
    int YSF(int n,int m)
    {
        if(n==0)return -1;
        if(n==1)return 0;
        return (YSF(n-1,m)+m)%n;
    }
};

4.写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。

class Solution {
public:
    int Add(int num1, int num2)
    {
         while(num2!=0)
         {
             int n1=num1^num2;
             int n2=num1&num2;
             n2<<=1;
             num1=n1;
             num2=n2;
         }
        return num1;
    }
};

5.请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。

class Solution {
public:
    bool isNumeric(char* str)
    {
        if(*str=='-'||*str=='+')
            str++;
        if(*str=='\0')return false;
        int num=0,dot=0,ee=0;
        while(*str!='\0')
        {
            if(*str<='9'&&*str>='0')
            {
                str++;
                num++;
            }
            else if(*str=='.')
            {
                if(dot>0||ee>0)return false;
                dot++;
                str++;
            }
            else if(*str=='E'||*str=='e')
            {
                if(ee>0||num==0)
                    return false;
                ee++;
                str++;
                if(*str=='-'||*str=='+')
                    str++;
                if(*str=='\0')
                    return false;
            }
            else 
                return false;
        }
        return true;
    }
   

};

6.给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
class Solution {
public:
    ListNode* EntryNodeOfLoop(ListNode* pHead)
    {
        if(!pHead||!pHead->next||!pHead->next->next)return NULL;
        ListNode*k1=pHead->next,*k2=pHead->next->next;
        while(k1!=k2)
        {
            if(!k2->next||!k2->next->next)
            {
                return NULL;
            }
            k1=k1->next;
            k2=k2->next->next;
        }
        k2=pHead;
        while(k2!=k1)
        {
            k1=k1->next;
            k2=k2->next;
        }
        return k1;
       
    }
};

7.给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。

/*
struct TreeLinkNode {
    int val;
    struct TreeLinkNode *left;
    struct TreeLinkNode *right;
    struct TreeLinkNode *next;
    TreeLinkNode(int x) :val(x), left(NULL), right(NULL), next(NULL) {
        
    }
};
*/
class Solution {
public:
    TreeLinkNode* GetNext(TreeLinkNode* pNode)
    {
        if(!pNode)return NULL;
        TreeLinkNode*p;
        if(pNode->right)
        {
            p=pNode->right;
            while(p->left)p=p->left;
            return p;
        }
        else if(pNode->next)
        {
            p=pNode;
            while(p->next&&p->next->right==p)
                p=p->next;
            if(p->next)
                return p->next;
            else
                return NULL;
        }
        return NULL;
    }
};

8.如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。我们使用Insert()方法读取数据流,使用GetMedian()方法获取当前读取数据的中位数。

class Solution {
private:
    vector<int> max;
    vector<int> min;
public:
    vector<int> que;
    void Insert(int num)
    {
        int siz=max.size()+min.size();
        if(siz==0)
        max.push_back(num);
        else if(siz&1)
        {
            if(num<max[0])
            {
                min.push_back(num);
                push_heap(min.begin(),min.end());
            }
            else
            {
                max.push_back(num);
                push_heap(max.begin(),max.end(),greater<int>());
                pop_heap(max.begin(),max.end(),greater<int>());
                
                min.push_back(max.back());
                max.pop_back();
                push_heap(min.begin(),min.end());
            }
        }
        else
        {
            if(num>min[0])
            {
                max.push_back(num);
                push_heap(max.begin(),max.end(),greater<int>());
            }
            else
            {
                min.push_back(num);
                push_heap(min.begin(),min.end());
                pop_heap(min.begin(),min.end());
                max.push_back(min.back());
                min.pop_back();
                push_heap(max.begin(),max.end(),greater<int>());
            }
        }
    }

    double GetMedian()
    { 
        int siz=min.size()+max.size();
        if(siz&1)
            return (double)max[0];
        else
        {
            return (double)(max[0]+min[0])/2;
        }
    }

};

 

9.给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}。

注意两个坑点    size为unsigned

class Solution {
public:
    vector<int> maxInWindows(const vector<int>& num, unsigned int size)
    {
        int n=num.size();
        int siz=size;
        if(!n||siz==0)return vector<int>();
        vector<int> ans;
        deque<pair<int,int> > que;
        for(int i=0;i<n;i++)
        {
            while(!que.empty()&&que.back().first<num[i])
            {
                que.pop_back();
            }
            que.push_back({num[i],i});
            if(que.front().second<i-siz+1)
            {
                que.pop_front();
            }
            if(i>=siz-1)
                ans.push_back(que.front().first);
        }
        return ans;
    }
};

10.请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配

class Solution {
public:
    bool match(char* s, char* p)
    {
        if(s[0]==0&&p[0]==0)return true;
        if(s[0]!=0&&p[0]==0)return false;
        if(p[0]!=0&&p[1]=='*')
        {
            if(match(s,p+2))
                return true;
            if((s[0]==p[0]||(s[0]!=0&&p[0]=='.'))&&match(s+1,p))
                return true;
        }
        else
        {
            if((s[0]==p[0]||(s[0]!=0&&p[0]=='.'))&&match(s+1,p+1))
                return true;
        }
        return false;
    }
};

11.在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

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

12.在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

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

13.输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    TreeNode* Convert(TreeNode* pRootOfTree)
    {
        TreeNode*p=pRootOfTree;
        if(!p)return NULL;
        stack<pair<TreeNode*,bool> >sta;
        
        TreeNode*t=NULL,*pRoot;
        
        sta.push({p,false});
        while(!sta.empty())
        {
            p=sta.top().first;
            bool vis=sta.top().second;
            sta.pop();
            if(!p)continue;
            if(vis)
            {
                p->left=t;
                if(t)t->right=p;
                else pRoot=p;
                t=p;
            }
            else
            {
                
                sta.push({p->right,false});
                sta.push({p,true});
                sta.push({p->left,false});
            }
        }
        return pRoot;
    }
};
class Solution {
public:
    TreeNode* Convert(TreeNode* pRootOfTree)
    {
        TreeNode*p=pRootOfTree;
        if(!p)return NULL;
        stack<TreeNode*> sta;
        TreeNode*t=NULL,*pRoot;
        while(!sta.empty()||p)
        {
            if(p)
            {
                sta.push(p);
                p=p->left;
            }
            else
            {
                p=sta.top();
                p->left=t;
                if(t)t->right=p;
                else pRoot=p;
                t=p;
                sta.pop();
                p=p->right;
            }
        }
        if(t)
        t->right=NULL;
        return pRoot;
    }
};

14.有重复的全排列

/*
例如:p=839647521是数字1~9的一个排列。下面生成下一个排列的步骤如下:
自右至左找出排列中第一个比右边数字小的数字4
在该数字后的数字中找出比4大的数中最小的一个5
将5与4交换,得到839657421
将7421反转,得到839651247。这就是排列p的下一个排列。
*/
class Solution {
private:
    bool check(string&s,int x,int y)
    {
        for(int i=x;i<y;i++)
            if(s[i]==s[y])return false;
            return true;
    }
    void dfs(vector<string>&v,int k,string& s,int n)
    {
        if(k==n)
        {
            v.push_back(s);
            return;
        }
        for(int i=k;i<n;i++)
        {
            if(check(s,k,i))
            {
                swap(s[k],s[i]);
                dfs(v,k+1,s,n);
                swap(s[k],s[i]);
            }
        }
    }
    bool nextp(string&s,int l,int r)
    {
        int k=-1;
        for(int i=r;i>l;i--)
        {
            if(s[i]>s[i-1])
            {
                k=i-1;

                break;
            }
        }
        if(k==-1)return false;
        int t=r;
        while(s[t]<=s[k])t--;
        swap(s[k],s[t]);
        for(int i=k+1,j=r;i<j;i++,j--)
        {
            swap(s[i],s[j]);
        }
        return true;
    }
public:
    vector<string> Permutation(string str) {
        sort(str.begin(),str.end());
        vector<string>v;
        int len=str.length();
        if(len==0)return v;
        do
        {
            v.push_back(str);
        }
        while(nextp(str,0,len-1));
        return v;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值