【牛客网刷题】(第三弹)链表与二分法oj

🧸🧸🧸各位大佬大家好,我是猪皮兄弟🧸🧸🧸
在这里插入图片描述


废话不多说,直接来做题!!

一、📖链表的奇偶重排(中等)

nowcoder链表的奇偶重排

思路:这道题要求的不是数值的奇偶,要求的是点位奇偶,那么用一个计数器就搞定了,创建两个新的头,分别存奇偶就ok

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    ListNode* oddEvenList(ListNode* head) {
        // write code here
        if(head==nullptr||head->next==nullptr)
            return head;
        ListNode* even = new ListNode(0);
        ListNode*eventail=even;
        ListNode* odd = new ListNode(0);
        ListNode*oddtail=odd;
        ListNode*cur=head;
        int count=1;
        while(cur)
        {
            if(count%2!=0)
            {
                eventail->next=cur;
                eventail=eventail->next;
            }
            if(count %2==0)
            {
                oddtail->next=cur;
                oddtail=oddtail->next;
            }
            count++;
            cur=cur->next;
        }
        eventail->next=odd->next;
        oddtail->next=nullptr;
        delete odd;
        head=even->next;
        delete even ;
        return head;
    }
};

二、📖删除有序链表中重复的元素(中等)

nowcoder删除有序链表中重复的元素
进阶要求时间复杂度O(N),空间复杂度O(1),那么就只能遍历链表了,不然的话可以用辅助数组来存储

class Solution {
public:
    /**
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    ListNode* deleteDuplicates(ListNode* head) {
        // write code here
        if(head==nullptr||head->next==nullptr)
            return head;
       ListNode*newhead=new ListNode(0);
        newhead->next=head;
        ListNode*prev=newhead,*cur=head;
        while(cur&&cur->next)
        {
            if(cur->val!=cur->next->val)
            {
                prev=cur;
                cur=cur->next;
            }
            else
            {
                while(cur->next!=nullptr&&cur->val==cur->next->val)
                {
                    cur=cur->next;
                }
                cur=cur->next;
                prev->next=cur;
                if(prev==newhead)
                    head=cur;
            }
        }
        delete newhead;
        return head;
    }
};

三、📖二分查找-Ⅰ

nowcoder二分查找-Ⅰ

int search(vector<int>& nums, int target) {
        // write code here
       int left=0;
        int right=nums.size()-1;
        while(left<=right)
        {
            int mid=left+(right-left)/2;
            if(nums[mid]<target)
            {
                left=mid+1;
            }
            else if(nums[mid]>target)
            {
                right=mid-1;
            }
            else
            {
                return mid;
            }
        }
        return -1;
    }
};

四、📖二维数组中的查找(中等)

nowcoder二维数组中的查找
暴力求解的话时间复杂度O(N*M)要求时间复杂度O(N+M),来看看下面的线性搜索方法吧

    class Solution {
public:
    bool Find(int target, vector<vector<int> > array) {
        if(array.size()==0)
            return false;
        int r=array.size();
        int c=array[0].size();
        int row=r-1;
        int col=0;
        while(row>=0&&col<=c-1)
        {
            int tmp=array[row][col];
            if(tmp<target)
            {
                col++;
            }
            else if(tmp>target)
            {
                row--;
            }
            else
                return true;
        }
        return false;
    }
};
    }

五、📖寻找峰值

nowcoder寻找峰值

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param nums int整型vector 
     * @return int整型
     */
    int findPeakElement(vector<int>& nums) {
        // write code here
        if(nums.size()==1)
            return 0;
        for(int i=0;i<nums.size();i++)
        {
            if(i==0&&nums[1]<nums[0])
                return 0;
            else if(i==nums.size()-1&&nums[nums.size()-1]>nums[nums.size()-2])
                return nums.size()-1;
            else if(nums[i]>nums[i-1]&&nums[i]>nums[i+1])
            {
                return i;
            }
        }
        return 0;
    }
};

六 、📖牛客oj总结

牛客网是个很不错的刷题软件,也希望大家能天天在上面刷刷题,大厂offer指日可待啊兄弟们。刷起来。

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猪皮兄弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值