zoukankan      html  css  js  c++  java
  • 简直offer

    数组中重复的数字

    //O n O 1
    //元素值与角标值 有明显对应关系
    //维护一个指针
    class Solution {
    public:
        int duplicateInArray(vector<int>& nums) {
            int n = nums.size();
            for (auto x : nums)
                if (x < 0 || x >= n)
                    return -1;
    
            for (int i = 0; i < nums.size(); i ++ ) {
                while (nums[i] != i) {
                    if (nums[i] == nums[nums[i]])
                        return nums[i];
                    swap(nums[i], nums[nums[i]]);
                }
            }
    
            return -1;
        }
    };
    
    

    二维数组中的查找

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

    替换空格

    public String replaceSpace(StringBuffer str) {
        int P1 = str.length() - 1;
        for (int i = 0; i <= P1; i++)
            if (str.charAt(i) == ' ')
                str.append("  ");
    
        int P2 = str.length() - 1;
        while (P1 >= 0 && P2 > P1) {
            char c = str.charAt(P1--);
            if (c == ' ') {
                str.setCharAt(P2--, '0');
                str.setCharAt(P2--, '2');
                str.setCharAt(P2--, '%');
            } else {
                str.setCharAt(P2--, c);
            }
        }
        return str.toString();
    }
    

    顺时针打印矩阵

    public ArrayList<Integer> printMatrix(int[][] matrix) {
        ArrayList<Integer> ret = new ArrayList<>();
        int r1 = 0, r2 = matrix.length - 1, c1 = 0, c2 = matrix[0].length - 1;
        while (r1 <= r2 && c1 <= c2) {
            // 上
            for (int i = c1; i <= c2; i++)
                ret.add(matrix[r1][i]);
            // 右
            for (int i = r1 + 1; i <= r2; i++)
                ret.add(matrix[i][c2]);
            if (r1 != r2)
                // 下
                for (int i = c2 - 1; i >= c1; i--)
                    ret.add(matrix[r2][i]);
            if (c1 != c2)
                // 左
                for (int i = r2 - 1; i > r1; i--)
                    ret.add(matrix[i][c1]);
            r1++; r2--; c1++; c2--;
        }
        return ret;
    }
    

    重建二叉树

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    //前序遍历和中序遍历
    class Solution {
    public:
        unordered_map<int,int> pos;
    
        TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
            int n = preorder.size();
            for (int i = 0; i < n; i ++ )
                pos[inorder[i]] = i;  //定位根 在中序遍历中的位置
            return dfs(preorder, inorder, 0, n - 1, 0, n - 1);
        }
    
        TreeNode* dfs(vector<int>&pre, vector<int>&in, int pl, int pr, int il, int ir)
        {
            if (pl > pr) return NULL;
            int k = pos[pre[pl]] - il;
            TreeNode* root = new TreeNode(pre[pl]);
            root->left = dfs(pre, in, pl + 1, pl + k, il, il + k - 1);
            root->right = dfs(pre, in, pl + k + 1, pr, il + k + 1, ir);
            return root;
        }
    };
    
    

    二叉树的下一个节点 中序遍历

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode *father;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL), father(NULL) {}
     * };
     */
    class Solution {
    public:
        TreeNode* inorderSuccessor(TreeNode *p) {
            if (!p) return NULL;
            if (p->right) {
                p = p->right;
                while (p->left) p = p->left;
                return p;
            }
    
            while (p->father && p->father->right == p) {
                p = p->father;
            }
    
            if (p->father) return p->father;
            return NULL;
        }
    };
    
    

    矩阵中的路径

    class Solution {
    public:
        bool hasPath(vector<vector<char>>& matrix, string str) {
            for (int i = 0; i < matrix.size(); i ++ )
                for (int j = 0; j < matrix[i].size(); j ++ )
                    if (dfs(matrix, str, 0, i, j))
                        return true;
            return false;
        }
    
        bool dfs(vector<vector<char>> &matrix, string &str, int u, int x, int y) {
            if (matrix[x][y] != str[u]) return false;
            if (u == str.size() - 1) return true;
            int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
            char t = matrix[x][y];
            matrix[x][y] = '*';    //每个点只走一次
            for (int i = 0; i < 4; i ++ ) {
                int a = x + dx[i], b = y + dy[i];
                if (a >= 0 && a < matrix.size() && b >= 0 && b < matrix[a].size()) {
                    if (dfs(matrix, str, u + 1, a, b)) return true;
                }
            }
            matrix[x][y] = t;  //恢复
            return false;
        }
    };
    
    
  • 相关阅读:
    14.18 InnoDB Backup and Recovery 备份和恢复:
    14.18 InnoDB Backup and Recovery 备份和恢复:
    php使用 _before_index() 来实现访问页面前,判断登录
    php使用 _before_index() 来实现访问页面前,判断登录
    查询方式实例演示
    查询方式实例演示
    haproxy timeout server 46000 后台超时时间
    haproxy timeout server 46000 后台超时时间
    14.10.5 Reclaiming Disk Space with TRUNCATE TABLE 回收空间使用TRUNCATE TABLE
    14.10.5 Reclaiming Disk Space with TRUNCATE TABLE 回收空间使用TRUNCATE TABLE
  • 原文地址:https://www.cnblogs.com/CSE-kun/p/14682913.html
Copyright © 2011-2022 走看看