写在前面:
LeetCode这个网站相比不必多说了吧,凡是IT圈子的人应该都知道这个网站,最近开始准备找工作,当然也免不了上去刷刷题,做一做比较经典的编程题,刚好看到LeetCode有个周练,便报名参加。
进入正题:
概要: 总共4个题,一个半小时的时间安排。题目分级为,两个easy,一个medium,一个hard。
第一题 598. Range Addition II
题目描述:
Given an m * n matrix M initialized with all 0's and several update operations.
Operations are represented by a 2D array, and each operation is represented by an array with two positive integers a and b, which means M[i][j] should be added by one for all 0 <= i < a and 0 <= j < b.
You need to count and return the number of maximum integers in the matrix after performing all the operations.
其实就是一道开胃菜的题目而已,统计下最大值的个数,早上刚起来做着题的时候迷迷糊糊的,就选了个最傻逼的方法来做,直接构建了数组,然后按每一个操作去加一,结果可想而知,先是MLE,超过了限制大小的内存,后面在看一遍题目,醒悟过来,其实就是求a和b的最小值,然后框定的范围内的值一定都是最大的,个数就是n*m。
class Solution {
public:
int maxCount(int m, int n, vector<vector<int>>& ops) {
int lens = ops.size();
if(lens == 0) return n*m;
int min_a = 0x7fffffff, min_b = 0x7fffffff;
for(int i=0;i<lens;i++){
int a = ops[i][0], b = ops[i][1];
min_a = min(min_a, a);
min_b = min(min_b, b);
}
int count = min_a * min_b;
return count;
}
};
第二题 599. Minimum Index Sum of Two Lists
题目描述:
Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings.You need to help them find out their common interest with the least list index sum. If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer.
也是一道开胃菜,用一个unordered_map来维护即可。
class Solution {
public:
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
unordered_map<string, int> table;
int lens_1 = list1.size();
for(int i=0;i<lens_1;i++) table[list1[i]] = i;
int lens_2 = list2.size();
int min_val = 0x7fffffff;
vector<string> ret;
for(int i=0;i<lens_2;i++){
if(table.find(list2[i])!=table.end()){
table[list2[i]] += i;
if(table[list2[i]] == min_val) ret.push_back(list2[i]);
else if(table[list2[i]] < min_val){
ret.clear(); ret.push_back(list2[i]);
min_val = table[list2[i]];
}
}
}
return ret;
}
};
第三题 565. Array Nesting
题目描述:
A zero-indexed array A consisting of N different integers is given. The array contains all integers in the range [0, N - 1].
Sets S[K] for 0 <= K < N are defined as follows:
S[K] = { A[K], A[A[K]], A[A[A[K]]], ... }.
Sets S[K] are finite for each K and should NOT contain duplicates.
Write a function that given an array A consisting of N integers, return the size of the largest set S[K] for this array.
同样是一个开胃菜的题目,不过这一次LeetCode的评定这个题是medium难度,维护一个同样大小的数组来表示有哪些数被循环访问过,然后顺序遍历所有的值,统计一下最大值即可
//Simple Solution
class Solution {
public:
int arrayNesting(vector<int>& nums) {
int lens = nums.size();
if(lens == 1) return 1;
vector<bool> marks(lens, false);
int longest = 0;
for(int i=0;i<lens;i++){
int k = i, count = 0;
while(!marks[k]){
++count; marks[k] = !marks[k];
k = nums[k];
}
longest = max(longest, count);
}
return longest;
}
};
第四题 600. Non-negative Integers without Consecutive Ones
题目描述:
Given a positive integer n, find the number of non-negative integers less than or equal to n, whose binary representations do NOT contain consecutive ones.
这道题判断一个数的二进制表达是否有连续的1,可以用原始值和原始值向左循环移动一位的结果做一个与运算,如果结果为0即满足条件,反正则不满足;
第一次用了一个顺序统计的方法来做,结果显然会超时,因为n的取值是10的9次方级别的。所以后面考虑统计每一个以每一位开头为1,结尾为任意值的集合,其中不满足条件的有多少个数,最后用总数去减即可得到结果(代码暂时还未完善)
//Other Solution
class Solution {
private:
//对于一个n为的二进制数,找到其中1开头,n-1个任意数结尾的数中不满足条件的数
//比如说n = 3,以1开头的话,不满足条件的数为:
/*
**110, 111
*/
//公式如下(x为n-1):
//f(x) = (1+x)*x/2 - (x - 1)
int func(int num){
//TODO
}
public:
int findIntegers(int num) {
//TODO
}
};
总结:
找工作路漫漫,但求不忘初心,回首对得起走过的路。