Leetcode进阶之路——Biweekly Contest 2

37 篇文章 0 订阅
  1. Sum of Digits in the Minimum Number

Given an array A of positive integers, let S be the sum of the digits of the minimal element of A.
Return 0 if S is odd, otherwise return 1.
Example 1:
Input: [34,23,1,24,75,33,54,8]
Output: 0
Explanation:
The minimal element is 1, and the sum of those digits is S = 1 which is odd, so the answer is 0.
Example 2:
Input: [99,77,33,66,55]
Output: 1
Explanation:
The minimal element is 33, and the sum of those digits is S = 3 + 3 = 6 which is even, so the answer is 1.

给定一个整型数组,从中选取最小的一个数,若该数的所有位之和为奇数,则返回0,否则返回1
直接排序,然后求和即可

class Solution {
public:
    int getnum(int a)
    {
        int sum = 0;
        while(a)
        {
            sum += (a % 10);
            a /= 10;
        }
        return sum;
    }
    int sumOfDigits(vector<int>& A) {
        sort(A.begin(), A.end());
        return getnum(A[0]) % 2 == 0;
    }
};
  1. High Five

Given a list of scores of different students, return the average score of each student’s top five scores in the order of each student’s id.
Each entry items[i] has items[i][0] the student’s id, and items[i][1] the student’s score. The average score is calculated using integer division.
Example 1:
Input: [[1,91],[1,92],[2,93],[2,97],[1,60],[2,77],[1,65],[1,87],[1,100],[2,100],[2,76]]
Output: [[1,87],[2,88]]
Explanation:
The average of the student with id = 1 is 87.
The average of the student with id = 2 is 88.6. But with integer division their average converts to 88.

给定一个数组,每个元素由[id, score]组成,表示对应id的学生取得的成绩(1个学生有n个成绩),返回每个学生最高的五个成绩的平均分
先按id排序,若id相同则按成绩高低排序,最后遍历一遍,每个id最多取5个成绩(可小于5)

class Solution {
public:
    vector<vector<int>> highFive(vector<vector<int>>& items) {
		sort(items.begin(), items.end(),
			[&](const vector<int> &a, const vector<int> &b)
		{
			// 成绩降序
			if (a[0] == b[0]) return a[1] > b[1];
			// id 升序
			else return a[0] < b[0];
		});
		
		vector<vector<int>> res;
		for (int i = 0; i < items.size();)
		{
			int id = items[i][0], cnt = 1, sum = items[i][1];
			for (int j = i + 1; j < items.size() && cnt < 5; ++j)
			{
				if (items[j][0] != id) break;
				sum += items[j][1];
				cnt++;
			}
			while (i < items.size() && items[i][0] == id)
				i++;
			vector<int> v = { id, int(sum / cnt )};
			res.emplace_back(v);
		}
		return res;
	}
};
  1. permutation-of-letters

该题的网页一直没刷出来,题意大致是,给定一个字符串,比如:{a, b}c{d, e}f
大括号中的字符只能选择其中一个,返回所有的排列组合
先根据规则,把原字符串分配到各数组中,然后dfs即可
在将原字符串分割的时候,我的方法很冗余,代码量很长,有更好的更简便的方法

class Solution {
public:
    vector<string> permute(string S) {
		vector<vector<string>> vv;
		int s = 0;
		for (int i = 0; i < S.length();)
		{
			if (S[i] == '{')
			{
				vector<string> vs;

				if (i - s > 0)
				{
					vs.emplace_back(S.substr(s, i - s));
					vv.emplace_back(vs);
					s = i + 1;
					vs.clear();
				}
				
				int st = i + 1;
				for (int j = i + 1; j < S.length(); ++j)
				{
					if (S[j] == ',')
					{
						vs.emplace_back(S.substr(st, j - st));
						st = j + 1;
					}
					else if (S[j] == '}')
					{
						vs.emplace_back(S.substr(st, j - st));
						i = j + 1;
						s = i;
						break;
					}
				}
				vv.emplace_back(vs);
			}
			else
			{
				i++;
			}
		}
		if (s != S.length())
		{
			vector<string> vs = { S.substr(s, S.length() - s) };
			vv.emplace_back(vs);
		}
		for (int i = 0; i < vv.size(); ++i)
			sort(vv[i].begin(), vv[i].end());
		
		vector<string> res;
		permuteHelper(vv, res, "", 0);
		
		return res;
	}

	void permuteHelper(vector<vector<string>> &vv, vector<string>& res, string s, int cur)
	{
		if (cur == vv.size())
		{
			res.emplace_back(s);
			return;
		}
		for (int i = 0; i < vv[cur].size(); ++i)
		{
			permuteHelper(vv, res, s + vv[cur][i], cur + 1);
		}
	}
};
  1. Confusing Number II

We can rotate digits by 180 degrees to form new digits. When 0, 1, 6, 8, 9 are rotated 180 degrees, they become 0, 1, 9, 8, 6 respectively. When 2, 3, 4, 5 and 7 are rotated 180 degrees, they become invalid.
A confusing number is a number that when rotated 180 degrees becomes a different number with each digit valid.(Note that the rotated number can be greater than the original number.)
Given a positive integer N, return the number of confusing numbers between 1 and N inclusive.
Example 1:
Input: 20
Output: 6
Explanation:
The confusing numbers are [6,9,10,16,18,19].
6 converts to 9.
9 converts to 6.
10 converts to 01 which is just 1.
16 converts to 91.
18 converts to 81.
19 converts to 61.
Example 2:
Input: 100
Output: 19
Explanation:
The confusing numbers are [6,9,10,16,18,19,60,61,66,68,80,81,86,89,90,91,98,99,100].

给定一个数N,求出1 ~ N之间所有的confusing number:一个数将其旋转之后与原数不同,则为confusing number
如16旋转后为91,169旋转后为691,两者均为confusing number
思路很清晰,dfs即可,但实际做的时候却一直TLE,特别是1000000000这个数,后来发现原本是用map来存变换后的数,这个在函数间传输时很耗时,因此将其用另一种方法之后很快就Accept:

class Solution {
private:
	int res;
	int n;
public:
	int confusingNumberII(int N) {
		res = 0;
		n = N;
		confusingNumHelper(0);
		return res;
	}

	void confusingNumHelper(long long int cur)
	{
		if (cur > n) return;
		if (confusingNum(cur)) res++;
		confusingNumHelper(cur * 10 + 1);
		confusingNumHelper(cur * 10 + 6);
		confusingNumHelper(cur * 10 + 8);
		confusingNumHelper(cur * 10 + 9);
		// 若cur=0,无需cur*10,否则会无限递归
		if(cur) confusingNumHelper(cur * 10);
	}

	bool confusingNum(long long int n)
	{
		long long int sum = 0;
		int t = n;
		while (n)
		{
		// 只有这里需要用到变换,用两个if else即可替换map
			int k = n % 10;
			if (k == 6) k = 9;
			else if (k == 9) k = 6;
			sum = sum * 10 + k;
			n /= 10;
		}
		return sum != t;
	}
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值