牛客刷题题解

数组中出现次数超过一半的数字

class Solution {
public:
    bool g_bInputInvalid = false;

    bool CheckInvalidArray(vector<int> numbers, int length)
    {
        g_bInputInvalid = false;
        if(length <= 0)
            g_bInputInvalid = true;

        return g_bInputInvalid;
    }

    bool CheckMoreThanHalf(vector<int>& numbers, int length, int number)
    {
        int times = 0;
        for(int i = 0; i < length; ++i)
        {
            if(numbers[i] == number)
                times++;
        }

        bool isMoreThanHalf = true;
        if(times * 2 <= length)
        {
            g_bInputInvalid = true;
            isMoreThanHalf = false;
        }

        return isMoreThanHalf;
    }
    int RandomInRange(int min, int max)
    {
        int random = rand() % (max - min + 1) + min;
        return random;
    }
    void Swap(int* num1, int* num2)
    {
        int temp = *num1;
        *num1 = *num2;
        *num2 = temp;
    }
    int Partition(vector<int> &data, int length, int start, int end)
    {
        int index = RandomInRange(start, end);
        Swap(&data[index], &data[end]);

        int small = start - 1;
        for(index = start; index < end; ++ index)
        {
            if(data[index] < data[end])
            {
                ++ small;
                if(small != index)
                    Swap(&data[index], &data[small]);
            }
        }

        ++ small;
        Swap(&data[small], &data[end]);

        return small;
}
    int MoreThanHalfNum_Solution(vector<int> numbers) {
        int length=numbers.size();
        if(CheckInvalidArray(numbers, length))
            return 0;
 
        int middle = length >> 1;
        int start = 0;
        int end = length - 1;
        int index = Partition(numbers, length, start, end);
        while(index != middle)
        {
            if(index > middle)
            {
                end = index - 1;
                index = Partition(numbers, length, start, end);
            }
            else
            {
                start = index + 1;
                index = Partition(numbers, length, start, end);
            }
        }

        int result = numbers[middle];
        if(!CheckMoreThanHalf(numbers, length, result))
            result = 0;

        return result;
    }
};

最小的k个数

#include<bits/stdc++.h>
using namespace std;
void swap(vector<int>& input,int start,int end){
        int temp=input[start];
        input[start]=input[end];
        input[end]=temp;
    }
int partition(vector<int>& input,int start,int end){
        int pivotkey=input[0];
        while(start<end){
            while(start<end&&input[end]>=pivotkey){
                end--;
            }
            swap(input,start,end);
            while(start<end&&input[start]<=pivotkey){
                start++;
            }
            swap(input,start,end);
        }
        return start;
}
vector<int> GetLeastNumbers_Solution(vector<int> &input, int k) {
        int len=input.size();
        int start=0,end=len-1;
        vector<int> output;
        int index=partition(input,start,end);
        while(index!=k-1){
            if(index>k-1){
                end=index-1;
                index=partition(input,start,end);
            }
            else{
                start=index+1;
                index=partition(input,start,end);
            }
        }
        for(int i=0;i<k;i++){
            output.push_back(input[i]);
        }
        return output;
}
int main(){
    vector<int> input,output;
    input.push_back(4);
    input.push_back(5);
    input.push_back(1);
    input.push_back(6);
    input.push_back(2);
    input.push_back(7);
    input.push_back(3);
    input.push_back(8);
    output=GetLeastNumbers_Solution(input,4);
    for(int i=0;i<4;i++){
        cout<<output[i]<<endl;
    }
}

对称链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool isPalindrome(ListNode* head) {
        if(head==NULL||head->next==NULL){
            return true;
        }
        ListNode* slow=head;
        ListNode* fast=head;
        ListNode* prev=NULL;
        ListNode* pnext=slow->next;
        while(fast!=NULL&&fast->next!=NULL){
            fast=fast->next->next;
            slow->next=prev;
            prev=slow;
            slow=pnext;
            pnext=pnext->next;
        }
        fast=fast==NULL?slow:slow->next;
        slow=prev;
        while(slow!=NULL){
            if(slow->val!=fast->val){
                return false;
            }
            fast=fast->next;
            slow=slow->next;
        }
        return true;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值