数组中出现次数超过一半的数字
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;
}
}
对称链表
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;
}
};