算法题(312)

给定一个链表和一个节点,删除该节点。

要点:O(1)时间复杂度;防止野指针。

#include<iostream>
using namespace std;(

struct node{
    int value;
    node *next;
    node(int i){
        value = i;
        next = NULL;
    }
};

node* deleteNode(node *root, node *d){
    if(root->next==NULL){
        delete root;
        root=NULL;
        return root;
    }
    if(d->next==NULL){
        node *n=root;
        while(n->next->next!=NULL)n=n->next;
        delete n->next;
        n->next=NULL;
        return root;
    }
    d->value=d->next->value;
    node *tmp=d->next;
    d->next=d->next->next;
    delete tmp;
    return root;
}

int main()
{
    node *n = new node(-1);
    node *root = n;
    node *n1 = new node(0);
    n->next=n1;
    node *n2 = new node(1);
    n1->next=n2;
    node *n3=new node(2);
    n2->next=n3;
    
    root = deleteNode(root, n3);
    node *r=root;
    while(r->next!=NULL){
        cout<<r->value<<' ';
        r=r->next;
    }
    cout<<r->value;
    return 0;
}
二分查找
int binarysearch(vector<int>& a, int k)
{
    int left = 0;
    int right = a.size() - 1;
    int mid;
    while(left <= right)
    {
        mid = (left+right) / 2;
        if(a[mid] < k) left = mid + 1;
        else if(a[mid] > k) right = mid - 1;
        else return mid;
    }
    return -1; //找不到时返回-1
}
给定排序数组,删除其中某个元素

要点:二分:O( log ⁡ n \log n logn),停止条件,边界条件。

#include<iostream>
#include<vector>
using namespace std;

int searchSmall(vector<int>& v, int t)
{
    int left = 0;
    int right = v.size() - 1;
    int mid,min;
    while(left <= right)
    {
        mid = (left+right)>>1;
        if(v[mid] < t) left = mid + 1;
        else if(v[mid] > t) right = mid - 1;
        else {
            min=mid;
            right=mid-1;
            if(left>right){
                if(v[right]!=t)break;
            }
        }
    }
    return min;
}

int searchLarge(vector<int>& v, int t)
{
    int left = 0;
    int right = v.size() - 1;
    int mid,max;
    while(left <= right)
    {
        mid = (left+right)>>1;
        if(v[mid] < t) left = mid + 1;
        else if(v[mid] > t) right = mid - 1;
        else {
            max = mid;
            left = mid+1;
            if(left>right){
                if(v[left]!=t)break;
            }
        }
    }
    return max;
}

int deleteDup(vector<int>& v, int t)
{
    int max = searchLarge(v,t);
    int min = searchSmall(v,t);
    return max-min+1;
}

int main()
{
    vector<int> v;
    v.push_back(0);
    v.push_back(1);
    v.push_back(1);
    v.push_back(1);
    v.push_back(1);
    v.push_back(2);
    v.push_back(4);
    cout<<deleteDup(v, 1)<<endl;
    return 0;
}
快排

要点:最坏O( n 2 n^2 n2),平均O( n log ⁡ n n\log n nlogn)。

//快排
#include<iostream>
#include<vector>
using namespace std;

void quick_sort(vector<int> &v, int l, int r)
{
	if (l < r)
	{
		int i = l, j = r, x = v[l];
		while (i < j)
		{
			while (i < j && v[j] >= x) j--;
			if (i < j) v[i++] = v[j];
			while (i < j && v[i] < x) i++;
			if (i < j) v[j--] = v[i];
		}
		v[i] = x;
		quick_sort(v, l, i - 1);
		quick_sort(v, i + 1, r);
	}
}


int main()
{
	vector<int> v;
	v.push_back(7);
	v.push_back(1);
	v.push_back(3);
	v.push_back(1);
	v.push_back(6);
	v.push_back(2);
	v.push_back(4);
	quick_sort(v, 0, 6);
	for (int i = 0;i < 7;i++)cout << v[i] << endl;

	return 0;
}
第k小(大)的数

要点:o( n n n),快排魔改

#include<iostream>
#include<vector>
using namespace std;

int quick_sort(vector<int> &v, int l, int r, int k)
{
	if (l < r)
	{
		int i = l, j = r, x = v[l];
		while (i < j)
		{
			while (i < j && v[j] >= x) j--;
			if (i < j) v[i++] = v[j];
			while (i < j && v[i] < x) i++;
			if (i < j) v[j--] = v[i];
		}
		v[i] = x;
		if (i == k-1)return x;
		if(i>k-1) return quick_sort(v, l, i - 1, k);
		if(i<k-1) return quick_sort(v, i + 1, r, k);
	}
}


int main()
{
	vector<int> v;
	v.push_back(7);
	v.push_back(1);
	v.push_back(3);
	v.push_back(1);
	v.push_back(6);
	v.push_back(2);
	v.push_back(4);
	cout << quick_sort(v, 0, 6, 2);

	return 0;
}
不含重复字符的最长字串
class Solution {
public:
    int bigger(int a, int b){
        if(a>b)return a;
        else return b;
    }
    int lengthOfLongestSubstring(string s) {
        if(s.size()<2)return s.size();
        unordered_map<char, int> m;
        for(int i=0; i<s.size(); i++){
            m[s[i]] = 0;
        }
        int start = 0;
        int end = 1;
        int max = 1;
        m[s[0]] = 1;
        while(end<s.size() && start<s.size()){
            if(m[s[end]]==0){
                m[s[end]]=1;
                end++;
                max=bigger(max, end-start);
            }
            else{
                max=bigger(max, end-start);
                while(s[start]!=s[end]){
                    m[s[start]]=0;
                    start++;
                }
                start++;
                m[s[start]]=1;
            }
        }
        return max;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值