牛客网编程题1

1

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

思路:本题最初想简单的求模算出等于1的位数,但是发现负数不行。其实简单的做法应该是与1相与,如果为1,则原来为1,循环移位判断。但注意,要左移,要是右移,对于负数会一直在高位补1,导致结果不准确。

class Solution {

public:

     int  NumberOf1(int n) {

        int count=0;

        int flag=1;

         while(flag!=0){

             if(flag&n){

                 count++;

             }

             flag=flag<<1;

         }

         return count;

     }

};

2

给定一个double类型的浮点数baseint类型的整数exponent。求baseexponent次方。

递归最简单了。。。迭代比较麻烦。

class Solution {

public:

    double Power(double base, int exponent) {

     if(exponent == 0) return 1;

        if(base == 0) return 0;

        if(exponent == 1)

            return base;

        else if(exponent == -1)

            return 1/base;

        return Power(base,exponent/2) * Power(base,exponent/2) * Power(base,exponent%2);

 

    }

};

3

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

因为要求保证相对位置,所以只能相邻的交换,不能用快排这种不稳定的算法。

 

//两个思路吧,第一个思路:类似冒泡算法,前偶后奇数就交换:

class Solution {

public:

    void reOrderArray(vector<int> &array) {

 

         

        for (int i = 0; i < array.size();i++)

        {

            for (int j = array.size() - 1; j>i;j--)

            {

                if (array[j] % 2 == 1 && array[j - 1]%2 == 0) //前偶后奇交换

                {

                    swap(array[j], array[j-1]);

                }

            }

        }

    }

};

 

//第二个思路:再创建一个数组

class Solution{

public:

    void reOrderArray(vector<int> &array) {

 

        vector<int> array_temp;

        vector<int>::iterator ib1, ie1;

        ib1 = array.begin();

 

 

        for (; ib1 != array.end();){            //遇见偶数,就保存到新数组,同时从原数组中删除

            if (*ib1 % 2 == 0) {

                array_temp.push_back(*ib1);

                ib1 = array.erase(ib1);

            }

            else{

                ib1++;

            }

 

        }

        vector<int>::iterator ib2, ie2;

        ib2 = array_temp.begin();

        ie2 = array_temp.end();

 

        for (; ib2 != ie2; ib2++)             //将新数组的数添加到老数组

        {

            array.push_back(*ib2);

        }

    }

};

 

4

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

本题土办法就是先遍历一遍链表,找到一共多少个,倒数第K个就是正数第count-k+1个,从头再遍历一遍,注意k=0k>count的情况的处理,都返回NULL

但是有个非常好的思路2.用两个指针,1个跑了K-1个时,第2 个再开跑,则1跑到底时,第2个指针指针所指就是要找的倒数第k个。

思路1

class Solution {

public:

    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {

        if(k==0)

            return NULL;

    int count=0;

        ListNode* p=pListHead;

        while(p!=NULL){

            count++;

            p=p->next;

        }

        if(k>count)

            return NULL;

        int find=count-k+1;

        

        int x=1;

        p=pListHead;

        while(x<find){

            p=p->next;

            x++;

        }

        return p;

    }

};

思路二

时间复杂度O(n),一次遍历即可

public class Solution {

    public ListNode FindKthToTail(ListNode head,int k) {

        ListNode pre=null,p=null;

        //两个指针都指向头结点

        p=head;

        pre=head;

        //记录k

        int a=k;

        //记录节点的个数

        int count=0;

        //p指针先跑,并且记录节点数,当p指针跑了k-1个节点后,pre指针开始跑,

        //p指针跑到最后时,pre所指指针就是倒数第k个节点

        while(p!=null){

            p=p.next;

            count++;

            if(k<1){

                pre=pre.next;

            }

            k--;

        }

        //如果节点个数小于所求的倒数第k个节点,则返回空

        if(count<a) return null;

        return pre;

            

    }

}

 

5

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

class Solution {

public:

    ListNode* Merge(ListNode* pHead1, ListNode* pHead2)

    {

         ListNode* result = NULL;

        ListNode* current = NULL;

         

        if(pHead1 == NULL)

            return pHead2;

        if(pHead2 == NULL)

            return pHead1;

        /*

        if(pHead1->val <= pHead2->val){

            result = pHead1;

            result->next = Merge(pHead1->next, pHead2);

        } else {

            result = pHead2;

            result->next = Merge(pHead1, pHead2->next);

        }

        */

        while(pHead1 != NULL && pHead2 != NULL){

            if(pHead1->val <= pHead2->val){

                if(result == NULL){

                    current = result = pHead1;

                } else {

                    current->next = pHead1;

                    current = current->next;

                }

                pHead1 = pHead1->next;

            } else {

                if(result == NULL){

                    current = result = pHead2;

                } else {

                    current->next = pHead2;

                    current = current->next;

                }

                pHead2 = pHead2->next;

            }

        }

         

        if(pHead1 == NULL){

            current->next = pHead2;

        }

        if(pHead2 == NULL){

            current->next = pHead1;

        }

         

        return result;

                

            }

};

6

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

本题要注意,B可能在A的左子树或者A,或者A的右子树中,注意比较方法。

class Solution {

public:

    bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)

    {

        if(pRoot2 == NULL || pRoot1 == NULL )

            return false;

        return isSubtree(pRoot1, pRoot2)|| HasSubtree(pRoot1->left,pRoot2) || HasSubtree(pRoot1->right,pRoot2);

    }

      

    bool isSubtree(TreeNode* pRoot1 , TreeNode* pRoot2){

        if(pRoot2 == NULL)

            return true;

        if(pRoot1 == NULL)

            return false;

        return pRoot1->val == pRoot2->val && isSubtree(pRoot1->left,pRoot2->left) && isSubtree(pRoot1->right,pRoot2->right);

    }

};

7

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

class Solution {

public:

    stack<int> stack1,stack2;

     

    void push(int value) {

        stack1.push(value);

        if(stack2.empty())

            stack2.push(value);

        else if(value<=stack2.top())

        {

            stack2.push(value);

        }

    }

     

    void pop() {

        if(stack1.top()==stack2.top())

            stack2.pop();

        stack1.pop();

         

    }

     

    int top() {

        return stack1.top();       

    }

     

    int min() {

        return stack2.top();

    }

};

发布了47 篇原创文章 · 获赞 8 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/nanchengyu/article/details/52904439