• 剑指offer(11-20)编程题


    1. 二进制中1的个数
    2. 数值的整数次方
    3. 调整数组顺序使奇数位于偶数前面
    4. 链表中倒数第k个结点
    5. 反转链表
    6. 合并两个排序的链表
    7. 树的子结构
    8. 二叉树的镜像
    9. 顺时针打印矩阵
    10. 包含min函数的栈

    11.输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。

    class Solution {
    public:
         int  NumberOf1(int n) {
             int count = 0;
             while(n){
                 count++;
                 n = n & (n-1);
             }
             return count;
         }
    };

    12.给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。

    class Solution {
    public:
    
        double Power(double base, int exponent) {
            if(exponent == 0) return 1;
            if(exponent == 1) return base;
    
            int sign = 1;
            if(exponent < 0) {
                sign = -1;
                exponent = - exponent;
            }
    
            double res;
            res = Power(base,exponent >> 1);
            res *= res;
            if(exponent >> 1 & 1){
                res *= base;
            }
            if(sign == -1){
                res = 1.0 / res;
            }
    
            return res;
    
        }
    };

     13.输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。

    class Solution {
    public:
        void reOrderArray(vector<int> &array) {
            int n = array.size();
            int pEven = 0; //第一个偶数的位置
            for (int i = 0; i < n; i++) {
                if (array[i] % 2 == 0) {
                    pEven = i;
                    break;
                }
            }
            for (int i = pEven+1; i < n; i++) {
                //发现奇数,从第一个偶数开始,后移
                if (array[i] % 2 == 1) {
                    int temp = array[i];
                    for (int j = i; j >= pEven; j--) {
                        array[j] = array[j - 1];
                    }
                    array[pEven] = temp;
                    pEven++;
                }
            }
        }
    };

     14.输入一个链表,输出该链表中倒数第k个结点。

    /*
    struct ListNode {
        int val;
        struct ListNode *next;
        ListNode(int x) :
                val(x), next(NULL) {
        }
    };*/
    class Solution {
    public:
        ListNode* FindKthToTail(ListNode*pListHead,unsigned int k){
    
            ListNode* p = pListHead;
            ListNode* q =  p;
            while(k--){
                if(p){
                    p = p->next;
                }else{
                    return nullptr;
                }
            }
    
            while(p){
                if(p){
                    p = p->next;
                    q = q->next;
                }
            }
            return q;
        }
    };

    15.输入一个链表,反转链表后,输出链表的所有元素。

    /*
    struct ListNode {
        int val;
        struct ListNode *next;
        ListNode(int x) :
                val(x), next(NULL) {
        }
    };*/
    class Solution {
    public:
        ListNode* ReverseList(ListNode* pHead) {
            if(pHead == nullptr) return pHead;
            ListNode* fakeHead = new ListNode(-1);
    
            ListNode* p = pHead;
            while (p) {
                ListNode* fakeHead_next = fakeHead->next;
                ListNode* p_next = p->next;
                fakeHead->next = p;
                p->next = fakeHead_next;
                p = p_next;
            }
            pHead = fakeHead->next;
            return pHead;
        }
    };

     16.输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

    class Solution {
    public:
        ListNode* Merge(ListNode* pHead1, ListNode* pHead2){
            ListNode* fakeHead = new ListNode(-1);
            ListNode* p = fakeHead;
            ListNode* p1 = pHead1;
            ListNode* p2 = pHead2;
            while(p1 && p2){
                if(p1->val <= p2->val){
                    p->next = p1;
                    p1 = p1->next;
                }else{
                    p->next = p2;
                    p2= p2->next;
                }
                p = p->next;
            }
            if(p1)
                p->next = p1;
            if(p2)
                p->next =p2;
    
            return fakeHead->next;
        }
    };

     17.输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

    /*
    struct TreeNode {
        int val;
        struct TreeNode *left;
        struct TreeNode *right;
        TreeNode(int x) :
                val(x), left(NULL), right(NULL) {
        }
    };*/
    class Solution {
    private:
        bool doHasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
            if (pRoot2 == nullptr) return true;
            if (pRoot1 == nullptr) return false;
    
            return ((pRoot1->val == pRoot2->val)
                    && doHasSubtree(pRoot1->left, pRoot2->left)
                    && doHasSubtree(pRoot1->right, pRoot2->right))
                    || doHasSubtree(pRoot1->left, pRoot2)
                    || doHasSubtree(pRoot1->right, pRoot2);
        }
    public:
        bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
            if (pRoot2 == nullptr)  //空树不是任意一个子树的子结构
                return false;
            return doHasSubtree(pRoot1,pRoot2);
    
        }
    };

     18.操作给定的二叉树,将其变换为源二叉树的镜像。

    /*
    struct TreeNode {
        int val;
        struct TreeNode *left;
        struct TreeNode *right;
        TreeNode(int x) :
                val(x), left(NULL), right(NULL) {
        }
    };*/
    class Solution {
    public:
        void Mirror(TreeNode* pRoot) {
            if(pRoot == nullptr) return;
    
            TreeNode* temp = pRoot->left;
            pRoot->left = pRoot->right;
            pRoot->right = temp;
    
            Mirror(pRoot->left);
            Mirror(pRoot->right);
        }
    };

     19.输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

    class Solution {
    private:
        void printMatrix(vector<vector<int>>& matrix, vector<int>& res,
                int circle) {
            int endRow = matrix.size() - 1 - circle;
            int endCol = matrix[0].size() - 1 - circle;
            //从左到右
            for (int i = circle; i <= endCol; i++) {
                res.push_back(matrix[circle][i]);
            }
            //从上往下
            for (int i = circle + 1; i <= endRow; i++) {
                res.push_back(matrix[i][endCol]);
            }
            //从右往左
            if (circle < endCol && circle < endRow) {
                for (int i = endCol - 1; i >= circle; i--) {
                    res.push_back(matrix[endRow][i]);
                }
            }
            //从下往上
            if (circle < endCol && circle < endRow) {
                for (int i = endRow - 1; i > circle; i--) {
                    res.push_back(matrix[i][circle]);
                }
            }
        }
    public:
        vector<int> printMatrix(vector<vector<int>>& matrix) {
            vector<int> res;
            if (matrix.empty()) return res;
            int rows = matrix.size();
            int cols = matrix[0].size();
            int circle = 0;
            while (circle * 2 < rows && circle * 2 < cols) {
                printMatrix(matrix, res, circle);
                circle++;
            }
            return res;
        }
    };

     20.定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。

    #include<cassert>
    class Solution {
    private:
        stack<int> m_data;
        stack<int> m_minVal;
    public:
        void push(int value) {
            m_data.push(value);
            if (m_minVal.empty() || value < m_minVal.top()) {
                m_minVal.push(value);
            } else {
                m_minVal.push(m_minVal.top());
            }
        }
    
        void pop() {
            assert(m_data.size() > 0);
            m_data.pop();
            m_minVal.pop();
        }
    
        int top() {
            assert(m_data.size() > 0);
            return m_data.top();
        }
    
        int min() {
            assert(m_minVal.size() > 0);
            return m_minVal.top();
        }
    };
  • 相关阅读:
    Strange RadioButton group behavior with ToolBar
    在XAML中为ItemsControl定义分组,适合mvvm绑定
    如何编写无法维护的代码 让自己稳拿铁饭碗 ;-)
    WPF 应用程序资源、内容和数据文件
    XNA+WPF solution worked
    object转List<XXX>的问题
    VS2013 执行Enable-Migrations,产生错误的解决办法
    WPF 为 PasswordBox 控件添加水印,最低级版
    为 ItemsControl 类型的控件提供行号,mvvm模式 绑定集合
    把父窗体设置为桌面,显示桌面时程序仍然能显示
  • 原文地址:https://www.cnblogs.com/wxquare/p/6638607.html
Copyright © 2020-2023  润新知