二维数组中的查找

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

时间限制:1秒 空间限制:32768K 热度指数:617731
本题知识点: 查找
 
思路:直接暴力查找即可,也可以二分。
class Solution {
public:
    bool Find(int target, vector<vector<int> > array) {
        for(int i=0;i<array.size();i++){
            for(int j=0;j<array[i].size();j++)
                if(target==array[i][j]) return true;
        }
        return false;
    }
};

替换空格

请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

时间限制:1秒 空间限制:32768K 热度指数:530285
本题知识点:字符串
 
思路:从后往前插入,先查找空格的个数,然后新字符串的长度即为原长度+2*空格个数。
class Solution {
public:
	void replaceSpace(char *str,int length) {
        char *ptr=str;///str所代表的即是字符串数组
        int newlength=0,oldlength=strlen(str),numofblock=0;
        while(*ptr){
            if(*ptr==' ') numofblock++;
            ptr++;
        }
        newlength=oldlength+2*numofblock;
        while(oldlength<newlength){
            if(str[oldlength]!=' ') str[newlength--]=str[oldlength--];
            else{
                oldlength--;
                str[newlength--]='0';
                str[newlength--]='2';
                str[newlength--]='%';
            }
        }
	}
};

从尾到头打印链表

输入一个链表,从尾到头打印链表每个节点的值

思路:使用栈来存储从头到尾的值,然后将栈中的值存入结果数组返回即可。
 
/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        ListNode *p=head;
        stack<int> temp;
        vector <int > ans;
        ans.clear();
        while(p!=NULL){
            temp.push(p->val);
            p=p->next;
        }
        while(!temp.empty()){
            ans.push_back(temp.top());
            temp.pop();
        }
        return ans;
    }
};

重建二叉树

输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

思路:二叉树的前序遍历是访问根结点,前序遍历左子树,前序遍历右子树,因而前序遍历的第一个元素即为根结点,然后后面接着左子树,最后即为右子树;而中序遍历是先遍历左子树,再访问根结点,最后遍历右子树,即数组中根节点往前全部为左子树,往后全部为右子树。因而可以通过前序遍历确定根结点,通过已经确定的根结点来统计中序遍历里面左子树和右子树的数量,然后找出前序遍历中对应的左右子树。使用递归思想求解即可。

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
        if (pre.empty() || vin.empty()) return NULL;
        vector<int > pre_left,pre_right,vin_left,vin_right;
        int val=pre[0],pos=0;
        TreeNode *root=new TreeNode(val);
        while(vin[pos]!=val) pos++;//找出根结点的位置
        if(pos>vin.size()) return NULL;
        for(int i=0;i<pos;i++){
            vin_left.push_back(vin[i]);
            pre_left.push_back(pre[i+1]);//存储左子树,由于前序遍历是先遍历左子树的,所以根结点后面跟着的即是左子树,数量跟中序遍历中的左子树相同,位置+1是因为第一个结点是根结点,故需要向后推一位
        }
        for(int i=pos+1;i<vin.size();i++){
            vin_right.push_back(vin[i]);
            pre_right.push_back(pre[i]);//存储右子树
        }
        root->left=reConstructBinaryTree(pre_left,vin_left);
        root->right=reConstructBinaryTree(pre_right,vin_right);
        return root;
    }
};

用两个栈实现队列

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

思路:使用第一个栈存储,然后需要pop时将第一个栈的元素全部转入第二个栈中,此时第二个栈的栈顶元素即为所需元素,弹出后再将第二个栈中的元素全部转回第一个栈中即可。

class Solution
{
public:
    void push(int node) {
         stack1.push(node);
    }

    int pop() {
        while(!stack1.empty()){
            int tmp=stack1.top();
            stack1.pop();
            stack2.push(tmp);
        }
        int ans=stack2.top();
        stack2.pop();
        while(!stack2.empty()){
            int tmp=stack2.top();
            stack2.pop();
            stack1.push(tmp);
        }
        return ans;
    }

private:
    stack<int> stack1;
    stack<int> stack2;
};

旋转数组的最小数字

把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。

思路:即找出输入数组中第一个比前一个数小的元素即可,如不存在比前一个元素小的元素,即数组中所有元素相同。

class Solution {
public:
    int minNumberInRotateArray(vector<int> rotateArray) {
        if(rotateArray.size()==0) return 0;
        int ans=rotateArray[0];
        for(int i=1;i<rotateArray.size();i++){
            if(rotateArray[i]<rotateArray[i-1]) ans=rotateArray[i];
        }
        return ans;
    }
};