剑指 Offer day01

 剑指 Offer 03. 数组中重复的数字

class Solution {
public:
    int findRepeatNumber(vector<int>& nums) {
        int n = nums.size();
        if(n == 0) return 0;
        vector<int> hash(n, 0);
        for(int i = 0; i < n; ++i) {
            hash[nums[i]]++;
            if(hash[nums[i]] > 1) return nums[i];
        }
        return 0;
    }
};

剑指 Offer 04. 二维数组中的查找 

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

 剑指 Offer 05. 替换空格

class Solution {
public:
    //c++ 字符串string自带函数 erase insert等,
    string replaceSpace(string s) {
        for(int i = 0; i < s.size(); ++i) {
            if(s[i] == ' ') {
                s.erase(i, 1);//从第i个位置开始删除k个字符
                s.insert(i, "%20");//从第i个位置插入string字符
            }
        }
        return s;
    }
};

剑指 Offer 06. 从尾到头打印链表 

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> reversePrint(ListNode* head) {
        stack<int> mystack;
        while(head != NULL) {
            mystack.push(head -> val);
            head = head -> next;
        }
        vector<int> ans;
        while(!mystack.empty()) {
            int x = mystack.top();
            mystack.pop();
            ans.push_back(x);
        }
        return ans;
    }
};
//递归流批

class Solution {
public:
    vector<int> ans;
    void recur(ListNode* head) {
        if(head == NULL) return;
        recur(head -> next);
        ans.push_back(head -> val);
    }
    vector<int> reversePrint(ListNode* head) {
        recur(head);
        return ans;
    }
};

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值