33: 链接:https://leetcode.com/contest/leetcode-weekly-contest-33/

A.Longest Harmonious Subsequence

思路:hash加查找

 class Solution {
public:
int findLHS(vector<int>& nums) {
if (nums.empty())
return ; int res = ;
int len = nums.size();
unordered_map<int, int> hash;
for (int i = ; i < len; i++)
hash[nums[i]]++;
for (auto it: hash)
{
if (hash.count(it.first + ))
res = max(res, it.second + hash[it.first + ]);
} return res;
}
};

B.Valid Square

思路:怎么判断是正方形呢(输入四个点无序)

只要四边边长均相等然后两个对角线长为边长的根号2倍即可

(注意距离大于0不然就是同一个点了,WA了一次)

 class Solution {
public:
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
vector<long long> dis;
long long d;
d = (p2[] - p1[]) * (p2[] - p1[]) + (p2[] - p1[]) * (p2[] - p1[]);
dis.push_back(d);
d = (p3[] - p1[]) * (p3[] - p1[]) + (p3[] - p1[]) * (p3[] - p1[]);
dis.push_back(d);
d = (p2[] - p4[]) * (p2[] - p4[]) + (p2[] - p4[]) * (p2[] - p4[]);
dis.push_back(d);
d = (p4[] - p3[]) * (p4[] - p3[]) + (p4[] - p3[]) * (p4[] - p3[]);
dis.push_back(d);
d = (p4[] - p1[]) * (p4[] - p1[]) + (p4[] - p1[]) * (p4[] - p1[]);
dis.push_back(d);
d = (p2[] - p3[]) * (p2[] - p3[]) + (p2[] - p3[]) * (p2[] - p3[]);
dis.push_back(d);
sort(dis.begin(), dis.end()); if (dis[] && dis[] == dis[] && dis[] == dis[] && dis[] == dis[] && dis[] == * dis[] && dis[] == * dis[])
return true;
else
return false;
}
};

C.Fraction Addition and Subtraction

思路:分数加减法,先单独算出每个分数的值,用c/d表示,然后结果用a/b表示,注意正负号,初始化a = 0, b = 1

(注意c,d数字可能大于9,所以要用while进行判断,WA了一次,b初始化为1,因为0不能做分母,0与任何数的最大公约数就是这个数,最小公倍数 = x*y/最大公约数)

 class Solution {
public:
long long gcd(long long x, long long y)
{
return y == ? x : gcd(y, x % y);
}
string fractionAddition(string expression) {
long long p, q;
long long flag = ;
long long a = , b = , c = , d = ;
int len = expression.size();
string res; for (int i = ; i < len; i++)
{
if (expression[i] == '-')
{
flag = -;
}
else if (expression[i] == '+')
{
flag = ;
}
else if ( '' <= expression[i] && expression[i] <= '')
{
while ('' <= expression[i] && expression[i] <= '')
c = c * + (expression[i++] - '');
c *= flag;
i--;
}
else if (expression[i] == '/')
{
i++;
while ('' <= expression[i] && expression[i] <= '')
d = d * + (expression[i++] - '');
i--; q = gcd(b, d);
p = b * d / q;
a = a * d / q;
c = c * b / q; a = a + c;
q = gcd(abs(a), p);
a = a / q;
b = p / q;
c = d = ;
}
else
;
} res += to_string(a) + "/" + to_string(b); return res;
}
};

(总结:思路比较清晰,就是写起来还得注意细节方面,WA了好几次,特例特判还是得多做题啊,加油!)

34: 链接:https://leetcode.com/contest/leetcode-weekly-contest-34/

1.Range Addition II

思路:直接算出最小的那个区间相乘就是结果

(注意判断为空的情况)

 class Solution {
public:
int maxCount(int m, int n, vector<vector<int>>& ops) {
if (ops.empty())
return m * n; int a, b;
a = b = INT_MAX;
for (int i = ; i < ops.size(); i++)
{
a = min(a, ops[i][]);
b = min(b, ops[i][]);
}
return a * b;
}
};

下边这个写法比较巧妙:

 class Solution {
public:
int maxCount(int m, int n, vector<vector<int>>& ops) {
for (auto op: ops)
{
m = min(m, op[]);
n = min(n, op[]);
}
return m * n;
}
};

:

2.Minimum Index Sum of Two Lists

思路:hash下第一个列表,然后再第二个列表中查找相同的字符串,并且计算下标的和,如果小,则清空数组压进去此时的字符串,如果相等则直接压入,如果大于不处理

 class Solution {
public:
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
unordered_map<string, int> hash;
vector<string> res;
int sum, num = INT_MAX; if (list1.empty() || list2.empty())
return res; for (int i = ; i < list1.size(); i++)
hash[list1[i]] = i; for (int j = ; j < list2.size(); j++)
{
if (hash.count(list2[j]))
{
sum = hash[list2[j]] + j;
if (num == INT_MAX)
{
num = sum;
res.push_back(list2[j]);
}
else if (sum == num)
res.push_back(list2[j]);
else if (sum < num)
{
num = sum;
res.clear();
res.push_back(list2[j]);
}
else
;
}
}
return res;
}
};

稍简洁版本:

 class Solution {
public:
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
unordered_map<string, int> hash;
vector<string> res;
int sum;
int num = INT_MAX; if (list1.empty() || list2.empty())
return res; for (int i = ; i < list1.size(); i++)
hash[list1[i]] = i; for (int j = ; j < list2.size(); j++)
{
if (hash.count(list2[j]))
{
sum = hash[list2[j]] + j;
if (sum < num)
{
num = sum;
res.clear();
res.push_back(list2[j]);
}
else if (sum == num)
res.push_back(list2[j]);
else
;
}
}
return res;
}
};

3.Array Nesting

思路:这个是hihocoder做过的一道题,直接每个进行判断,判断完了之后去找值对应的id

 class Solution {
public:
int arrayNesting(vector<int>& nums) {
int n = nums.size();
int count = ;
int res = ;
vector<int> vis; vis.resize(n, );
int i = ;
while (i < n)
{
if (vis[i])
{
res = max(res, count);
count = ;
i++;
}
else
{
vis[i] = ;
count++;
i = nums[i];
}
}
return res;
}
};

4.Non-negative Integers without Consecutive Ones

思路:不连续的1,用dp描述就是:dp[i][0] = dp[i - 1][0] + dp[i - 1][1]; dp[i][1] = dp[i - 1][0];然后再处理每一位的数字即可

 class Solution {
public:
int dp[][];
void init()
{
dp[][] = dp[][] = ;
for (int i = ; i <= ; i++)
{
dp[i][] = dp[i - ][] + dp[i - ][];
dp[i][] = dp[i - ][];
}
}
int dfs(int len, int num)
{
if (len <= )
return ; int val = << (len - );
if (num >= val)
return dp[len - ][] + dfs(len - , num - val);
else
return dfs(len - , num); }
int findIntegers(int num) {
init();
int len = ;
int n = num; while (n)
{
len++;
n >>= ;
} return dfs(len, num);
}
};

LeetCode Weekly Contest的更多相关文章

  1. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  2. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  3. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  4. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  5. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  6. 【LeetCode Weekly Contest 26 Q3】Friend Circles

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...

  7. 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

  8. 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

  9. LeetCode Weekly Contest 47

    闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...

随机推荐

  1. Android kernel LOGO的更换方法

    [从制作logo到LCD显示或者VGA显示logo] 1.制作logo的方法: 首先选择一个自己喜欢的图片,然后通过GIMP软件将该图片保存为.png格式, 变换方式这个就不说了(very easy) ...

  2. IOS设备型号(原创)

    以下是我收集的ios目前为止移动设备型号,ipad air不知道,本人没有这款设备,求指导的给个回复,在这谢谢了 ///** ////////////////////   设备类型 字符串   /// ...

  3. Xcode8.3 添加iOS10.3以下旧版本模拟器

    问题起源 由于手边项目需要适配到iOS7, 但是手边的测试机都被更新到最新版本,所以有些潜在的bug,更不发现不了.最近就是有个用户提出一个bug,而且是致命的,app直接闪退.app闪退,最常见的无 ...

  4. stm32串口通讯问题

    stm32串口通讯问题 在串口试验中,串口通讯不正常,则可能会出现以下问题: 1. 配置完成后,串口没有任何消息打印. 原因:1,端口配置有问题,需要重新检查I/O口的配置 2,接线有问题,检查接线是 ...

  5. SpringCloud网关ZUUL集成consul

    最近一直在搞基于springcloud的微服务开发,为了不限定微服务开发语言,服务发现决定采用consul不多说上代码 pom文件 <project xmlns="http://mav ...

  6. List分组 用于客服对话分组场景

    工作用可能会用到会话分组: Message是消息实体对象,里面有toId和fromId 指明接收方ID和发送方Id,通过组合形式"12-22-" 为map的key public M ...

  7. 开源的C#实现WebSocket协议客户端和服务器websocket-sharp组件解析

    很久没有写博客了(至少自己感觉很长时间没有写了),没办法啊,楼主也是需要生活的人啊,这段一直都在找工作什么的.(整天催我代码的人,还望多多谅解啊,我会坚持写我们的项目的,还是需要相信我的,毕竟这是一个 ...

  8. 【react学习】关于react框架使用的一些细节要点的思考

    ( _(:3 」∠)_给园友们提个建议,无论是API文档还是书籍,一定要多看几遍!特别是隔一段时间后,会有意想不到的收获的)   这篇文章主要是写关于学习react中的一些自己的思考:   1.set ...

  9. tmux简要介绍

    什么是tmux tmux全称terminal multiplexer,是一个终端复用软件.它可以帮助我们方便地管理多个终端会话. 下面介绍tmux中涉及到的一些名词 window 窗口 pane 窗格 ...

  10. CF #299 div1 B. Tavas and Malekas KMP-next数组

    题目链接:http://codeforces.com/contest/536/problem/B 一个原始字符串,一个未知字符串,每一次从pos[i]开始覆盖未知字符串,问最后字符串的形式,以及判断过 ...