Poker straight, the orphanage children playing games, median, and continuous positive

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

int LastRemaining_Solution(int n, int m)
{
	if (n < 1)
		return -1;
	vector<int> vec;
	vec.resize(n);
	int count = n;
	for (int i = 0, j = 0;; ++j)
	{
		if (vec[j] != 1)
		{
			if (i == m - 1)
			{
				vec[j] = 1;
				count--;
				i = -1;
			}
			++i;
			if (count == 1)
			{
				for (int k = 0; k < vec.size(); ++k)
				{
					if (vec[k] == 0)
					{
						return k;
					}
				}
			}
		}
		if (j == n - 1)
			j = -1;
	}
}

Here Insert Picture Description

class Solution {
public:
    vector<int> vec;
    void Insert(int num)
    {
        vec.push_back(num);
    }

    double GetMedian()
    { 
        sort(vec.begin(),vec.end());
        if(vec.size() % 2 == 1)
        {
            return vec[(vec.size() / 2)];
        }
        else
        {
            return (double)((double)vec[(vec.size() / 2) - 1] + (double)vec[(vec.size() / 2)]) / 2;
        }
    }

};

Here Insert Picture Description

class Solution {
public:
vector<vector<int> > FindContinuousSequence(int sum) {
	vector<vector<int>> vec1;
	if (sum <= 2)
		return vec1;
	int count = 0;
	for (int i = 1; i <= (sum / 2) + 1; ++i)
	{
		int j = i;
		vector<int> vec2;
		while (count < sum && j <= (sum / 2) + 1)
		{
			vec2.push_back(j);
			count += j;
			j++;
		}
		if (count == sum)
		{
			vec1.push_back(vec2);
		}
		count = 0;
	}
	return vec1;
}
};

Here Insert Picture Description

class Solution {
public:
    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
        int count = 0;
        ListNode* s = pListHead;
        while(s != nullptr)
        {
            count++;
            s = s->next;
        }
        if(k > count)
            return nullptr;
        ListNode* p = pListHead;
        while(k--)
        {
            p = p->next;
        }
        ListNode* cur = pListHead;
        while(p != nullptr)
        {
            p = p->next;
            cur = cur->next;
        }
        return cur;
    }
};
Published 230 original articles · won praise 28 · views 9322

Guess you like

Origin blog.csdn.net/weixin_43767691/article/details/103463964