标签:rip 提示 while com highlight div public 没有 关于
最近忙于论文和实习,好久没有刷题了,之前立下的关于triplet loss的flag也莫得了。
某公司压力太大,已离职,希望利用好学生生涯最后一点时间,认认真真找个工作,扎扎实实提高自己技术水平。
其实这两道题都特别简单,均是自己在没有提示的情况下做出来的。
第一道题一个排序再一个遍历就结束了,我从没见过这么简单的medium,是因为我的方法太笨了吗?
bool compare(const int a, const int b) {return b < a;}
class Solution {
public:
int hIndex(vector<int>& citations) {
int len = citations.size();
auto it = citations.begin();
while (it != citations.end())
if (*it == 0) it = citations.erase(it);
else ++it;
len = citations.size();
if (len == 0) return 0;
sort(citations.begin(), citations.end(), compare);
for (int i = 0; i < len; ++i) {
if (i+1 > citations[i])
return i;
else if (i+1 == len) return i+1;
}
return 0;
}
};
第二道题也没什么复杂的,甚至某种意义上更简单,只需要一次O(n)的遍历就结束了,唯一需要注意的就是开始和结束的条件。
class Solution {
public:
int totalFruit(vector<int>& tree) {
int first = tree[0], second = -1;
int i = 1, len = tree.size(), sameLen = 1, maxLen = 1, res = 1;
while (i < len) {
if (tree[i] == first || tree[i] == second) {
if (tree[i-1] == tree[i]) ++sameLen;
else sameLen = 1;
++maxLen;
++i;
}
else if (tree[i] != first && second < 0) {
sameLen = 1;
second = tree[i];
++maxLen;
++i;
}
else {
if (res < maxLen) res = maxLen;
maxLen = sameLen+1;
sameLen = 1;
first = tree[i-1];
second = tree[i];
++i;
}
if (i == len && res < maxLen) res = maxLen;
}
return res;
}
};
asdf
标签:rip 提示 while com highlight div public 没有 关于
原文地址:https://www.cnblogs.com/left4back/p/10561413.html