第一题:

973. K Closest Points to Origin

 We have a list of points on the plane.  Find the K closest points to the origin (0, 0).

(Here, the distance between two points on a plane is the Euclidean distance.)

You may return the answer in any order.  The answer is guaranteed to be unique (except for the order that it is in.)

Example 1:

Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].

Example 2:

Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)

Note:

  1. 1 <= K <= points.length <= 10000
  2. -10000 < points[i][0] < 10000
  3. -10000 < points[i][1] < 10000

题目大意:给你一些点,让你找离远点最近的K个点。主要考的是二维数组排序。

class Solution {
public:
vector<vector<int> > kClosest(vector<vector<int> >& points, int K) {
vector<vector<int> > ans;
int len = points.size();
for(int i=; i<len; i++) {
int x = points[i][];
int y = points[i][];
points[i].push_back(x*x+y*y);
}
sort(points.begin(), points.end(), [](const vector<int> &a, const vector<int> &b) { return a[] < b[]; });
for(int i=; i<K; i++) {
vector<int> res;
res.push_back(points[i][]);
res.push_back(points[i][]);
ans.push_back(res);
}
return ans;
}
};

第二题:

976. Largest Perimeter Triangle

Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3 of these lengths.

If it is impossible to form any triangle of non-zero area, return 0.

Example 1:

Input: [2,1,2]
Output: 5

Example 2:

Input: [1,2,1]
Output: 0

Example 3:

Input: [3,2,3,4]
Output: 10

Example 4:

Input: [3,6,2,3]
Output: 8

Note:

  1. 3 <= A.length <= 10000
  2. 1 <= A[i] <= 10^6

题目大意:从数组中,找出三个点组成一个周长最大的三角形,然后输出周长。

数据比较小,直接暴力的。

class Solution {
public:
bool ok(int a, int b, int c) {
return a-b<c;
} int largestPerimeter(vector<int>& A) {
int len = A.size();
sort(A.begin(), A.end());
for(int i=len-; i>=; i--) {
for(int j=i-; j>=; j--) {
if( ok(A[i], A[j], A[j-])) {
return A[i]+A[j]+A[j-];
}
}
}
return ;
}
};

第三题:

974. Subarray Sums Divisible by K

Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.

Example 1:

Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]

Note:

  1. 1 <= A.length <= 30000
  2. -10000 <= A[i] <= 10000
  3. 2 <= K <= 10000

题目大意:统计有多少条满足条件的子序列。条件1:连续,必须是连续子序列,条件2:序列和可以整除K。

思路:求出前缀和,想要找到连续子序列之和是可以整除K的话,那么前缀和只差模K也就是为0,也就是说统计模K相等的前缀和之差的个数,求一个等差数列求和。

做题的时候开始考虑错了,后面想到了解决方法但是没时间了,对照rank写出了一个我自己都认为是错的程序,提交AC。想不清楚,等过几天想清楚了,在仔细写下吧。

class Solution {
public:
int subarraysDivByK(vector<int>& A, int K) {
int a[K];
memset(a, , sizeof(a));
int len = A.size(); a[] = ;
int res = ;
for(int i=; i<len; i++) {
A[i] += A[i-];
int cnt = (A[i]%K+K)%K;
a[cnt] ++;
}
for(int i=; i<K; i++) {
res += (a[i]-)*a[i]/;
}
return res;
}
};

Weekly Contest 119的更多相关文章

  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 86

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

  3. leetcode weekly contest 43

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

  4. LeetCode Weekly Contest 23

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

  5. LeetCode之Weekly Contest 91

    第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10  ...

  6. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  7. LeetCode Weekly Contest 47

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

  8. 75th LeetCode Weekly Contest Champagne Tower

    We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...

  9. LeetCode之Weekly Contest 102

    第一题:905. 按奇偶校验排序数组 问题: 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...

随机推荐

  1. 对于EMC DAE、DPE、SPE、SPS的解释

    对于EMC DAE.DPE.SPE.SPS的解释: DAE: Disk Array Enclosure 单一的磁盘扩展单元 CX200/CX300/CX400/CX500CX600/CX700 DPE ...

  2. 关于redis分布式锁实现原理

    具体详情 http://www.cnblogs.com/SUNSHINEC/p/8302540.html

  3. Vue.js的简介

    vue.js简介 Vue.js读音 /vjuː/, 类似于 view   Vue.js是前端三大新框架:Angular.js.React.js.Vue.js之一,Vue.js目前的使用和关注程度在三大 ...

  4. ide phpStorm使用git的命令行工具

    1.点击phpStorm左下角,点击terminal 2.发现是windows自带的cmd.exe,可以将其改为git的sh.exe 3.打开设置(File -> Settings -> ...

  5. SIM7600C读写本机号码

    1 先发送AT+CPBS = "ON",表示要操作的是本机号码区域,而不是SIM的电话本 2 写入命令AT+CPBW=1,"1234567" 3 读取命令AT+ ...

  6. java web 读取数据库数据写入Excel返回浏览器下载

    @RequestMapping(value = "/download", method = RequestMethod.GET) public void downstudents( ...

  7. Word图片、表格添加题注

    1.首先为图片.表格添加题注: 2.通过交叉引用,如图**和图片.表的编号相关联起来:

  8. 多线程深入:乐观锁与悲观锁以及乐观锁的一种实现方式-CAS(转)

    原文:https://www.cnblogs.com/qjjazry/p/6581568.html 首先介绍一些乐观锁与悲观锁: 悲观锁:总是假设最坏的情况,每次去拿数据的时候都认为别人会修改,所以每 ...

  9. Python3学习之路~7.5 异常处理

    1.异常基础 在编程过程中为了增加友好性,在程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面,通俗来说就是不让用户看见大黄页!!! try: pass except Excepti ...

  10. vue页面无操作10分钟内调转到登录页面

    https://blog.csdn.net/lbPro0412/article/details/83864454 页面在设定时间内无任何操作(鼠标的点击.滑动.路由的切换.是否请求接口等),跳转到登录 ...