算法题(314)

合并排序链表

不改变原链表

#include<iostream>
using namespace std;

struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :val(x), next(NULL) {}
};

class Solution {
public:
    ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
    {
        if(pHead1==NULL)return pHead2;
        if(pHead2==NULL)return pHead1;
        ListNode *r, *p1=pHead1, *p2 = pHead2, *tail;
        if(p1->val>p2->val){
            r=new ListNode(p2->val);
            tail = r;
            p2=p2->next;
        }
        else{
            r=new ListNode(p1->val);
            tail = r;
            p1=p1->next;
        }
        while((p1!=NULL)&&(p2!=NULL)){
            if(p1->val>p2->val){
                ListNode* tmp = new ListNode(p2->val);
                tail->next = tmp;
                p2 = p2->next;
            }
            else{
                ListNode* tmp = new ListNode(p1->val);
                tail->next = tmp;
                p1 = p1->next;
            }
            tail = tail->next;
        }
        while(p1!=NULL){
            ListNode* tmp = new ListNode(p1->val);
            tail->next = tmp;
            tail = tail->next;
            p1 = p1->next;
        }
        while(p2!=NULL){
            ListNode* tmp = new ListNode(p2->val);
            tail->next = tmp;
            tail = tail->next;
            p2 = p2->next;
        }
        return r;
    }
};

int main()
{
    Solution x;
    ListNode *l1=new ListNode(1);
    ListNode *l2=new ListNode(2);
    ListNode *l3=new ListNode(3);
    ListNode *l4=new ListNode(4);
    ListNode *l5=new ListNode(5);
    ListNode *l6=new ListNode(6);
    l5->next=l6;l4->next=l5;l3->next=l4;l2->next=l3;l1->next=l2;
    ListNode *r1=new ListNode(1);
    ListNode *r2=new ListNode(2);
    ListNode *r3=new ListNode(3);
    ListNode *r4=new ListNode(4);
    ListNode *r5=new ListNode(5);
    ListNode *r6=new ListNode(6);
    r5->next=l6;r4->next=r5;r3->next=r4;r2->next=r3;r1->next=r2;
    while(l2!=NULL){
        cout<<l2->val;
        l2=l2->next;
    }cout<<endl;
    while(r2!=NULL){
        cout<<r2->val;
        r2=r2->next;
    }cout<<endl;
    ListNode *r = x.Merge(l1,r1);
    ListNode *display=r;
    while(display!=NULL){
        cout<<display->val;
        display=display->next;
    }cout<<endl;
    return 0;
}

直接操作原链表

class Solution {
public:
    ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
    {
        if(pHead1==NULL)return pHead2;
        if(pHead2==NULL)return pHead1;
        ListNode *r, *tail;
        if(pHead1->val>pHead2->val){
            r=pHead2;
            pHead2=pHead2->next;
        }
        else{
            r=pHead1;
            pHead1=pHead1->next;
        }
        tail=r;
        while((pHead1!=NULL)&&(pHead2!=NULL)){
            if(pHead1->val>pHead2->val){
                tail->next=pHead2;
                pHead2=pHead2->next;
            }
            else{
                tail->next=pHead1;
                pHead1=pHead1->next;
            }
            tail=tail->next;
        }
        if(pHead1!=NULL)tail->next=pHead1;
        if(pHead2!=NULL)tail->next=pHead2;
        return r;
    }
};
链表中倒数第k个节点

要点:双指针

class Solution {
public:
    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
        if(pListHead==NULL||k==0)return NULL;
        ListNode *pl=pListHead, *pr=pListHead;
        for(int i=0;i<k;i++)pr=pr->next;
        while(pr!=NULL){
            pr=pr->next;
            pl=pl->next;
        }
        return pl;
    }
};
反转链表
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        if(pHead==NULL)return NULL;
        ListNode *r=NULL, *tmp;
        while(pHead!=NULL){
            tmp=pHead;
            pHead=pHead->next;
            tmp->next=r;
            r=tmp;
        }
        return r;
    }
};
数值的整数次方

要点:考虑负数指数

class Solution {
public:
    double Power(double base, int exponent) {
        if(exponent==0)return 1;
        int pos;
        if(exponent>0)pos=1;
        else {
            pos=-1;
            exponent=-exponent;
        }
        double r=1;
        for(int i=0;i<exponent;i++)r*=base;
        if(pos==1)return r;
        else return 1/r;
    }
};
包含min函数的栈

要点:两个栈,同入同出

class Solution {
private:
    stack<int> s1,s2;
public:
    void push(int value) {
        s1.push(value);
        if(s2.empty())s2.push(value);
        else if(s2.top()<value)s2.push(s2.top());
        else s2.push(value);
    }
    void pop() {
        s1.pop();
        s2.pop();
    }
    int top() {
        return s1.top();
    }
    int min() {
        return s2.top();
    }
};
两个转轮密码锁,求最少转动次数使匹配
#include <iostream>
#include<vector>
#include<string>
using namespace std;

int main()
{
	int n;
	vector<int> n1, n2;
	cin >> n;
	string input;
	cin >> input;
	for (int i = 0;i < n;i++) {
		n1.push_back(input[i] - '0');
	}
	cin >> input;
	for (int i = 0;i < n;i++) {
		n2.push_back(input[i] - '0');
	}
	int r = 0;
	for (int i = 0;i < n;i++) {
		int once = n1[i] - n2[i];
		if (once < 0)once = - once;
		if (once > 5)once = 10 - once;
		r += once;
	}
	cout << r;
	return 0;
}
a,b二人各自有两个等长数组,二人轮流执行一下操作之一:从对方的数组中删去1个数;从自己的数组中删去一个数,并累加到自己的得分。求两个数组删完时a比b最多多多少分(a先操作)
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool comp(const int &a, const int &b)
{
	return a > b;
}

int main()
{
	int n;
	cin >> n;
	vector<int>n1, n2;
	int p;
	for (int i = 0;i < n;i++) {
		cin >> p;
		n1.push_back(p);
	}
	for (int i = 0;i < n;i++) {
		cin >> p;
		n2.push_back(p);
	}
	sort(n1.begin(), n1.end(), comp);
	sort(n2.begin(), n2.end(), comp);
	cout << endl;
	for (int i = 0;i < n;i++)cout << n1[i];
	for (int i = 0;i < n;i++)cout << n2[i];
	cout << endl;

	int p1 = 0, p2 = 0;
	int i1 = 0, i2 = 0;
	int turn = 0;
	while ((i1 < n) && (i2 < n)) {
		if (turn == 0) {
			if (n1[i1] == n2[i2]) {
				i1++;
				i2++;
				continue;
			}
			else if (n1[i1] > n2[i2]) {
				turn = 1;
				p1 += n1[i1];
				i1++;
			}
			else if (n1[i1] < n2[i2]) {
				turn = 1;
				i2++;
			}
		}
		else {
			if (n1[i1] == n2[i2]) {
				i1++;
				i2++;
				continue;
			}
			else if (n1[i1] > n2[i2]) {
				turn = 0;
				i1++;
			}
			else if (n1[i1] < n2[i2]) {
				turn = 0;
				p2 += n2[i2];
				i2++;
			}
		}
	}
	while (i1 < n) {
		if (turn == 0) {
			p1 += n1[i1];
			i1 += 2;
		}
		else {
			i1++;
			turn = 0;
		}
	}
	while (i2 < n) {
		if (turn == 1) {
			p2 += n2[i2];
			i2 += 2;
		}
		else {
			i2++;
			turn = 1;
		}
	}
	cout << p1 - p2;
	return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值