第一题:

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. Linux下批量修改后缀名

    1.用find和xargs添加后缀名 [root@node99 yum.repos.d]# ls -ltr total 32 -rw-r--r--. 1 root root 5701 Nov 23 2 ...

  2. Ubuntu-1604-LTS在虚拟机设置分辨率

    在虚拟机中安装ubuntu系统时,有时系统的界面并不同虚拟机展示的匹配,需要我们进行调整.不用那么多废话,直接看图:

  3. 必备Linux命令和C语言基础

    每一个学习嵌入式单片机的伙伴我相信对于这两个都不陌生,这毕竟是嵌入式单片机的生存之道    所有基础还是要打牢的   有句老话说的好基础不牢地动山摇    下面看下系统的资料吧   希望能对大家有所帮 ...

  4. poj1164

    #include<iostream> using namespace std; ][]; ][]; int roomnum; int maxroom; int m,n; typedef s ...

  5. 神贴真开眼界:为什么很多人倡导重视能力和素质,但同时对学历有严格要求?——代表了上一场比赛的输赢,招聘成本很重要。如果上一场游戏失败了,尽量让自己成为当前群体的尖子。学历只是其中的一个作品而已,但学历代表了学生时代为之做出的牺牲。人群自有偏向集中性 good

    对于软件工程师职位,没学历没关系,如果真觉得自己才高八斗,请在简历里附上 github项目链接或者 appstore/google play上你的作品.如果学历比别人低,那么想必是把时间和精力用在了其 ...

  6. 【转】mysql保存图片技术决定:保存二进制文件还是只保存图片相对路径,图片放在硬盘上面?

    最近遇到上面这个问题,一开始我就果断否决了数据库保存图片的策略,主要是太蠢!事实上我的决定是正确的,我仅仅理解为mysql读写性能提高的境界,具体为什么可以提高?很模糊,知道我看到了这里: 大佬做的实 ...

  7. allegro17.2 错误记录

    1.网表导入PCB时出现如下错误::error with pin number 'P11'in device ' lfbga176'::Unable to find pin name in adfnc ...

  8. Java 数据库程序设计

    数据库基础 目前,大多数数据库系统都是关系数据库系统(relational database system).该数据库系统是基于关系数据模型的,这个模型有三个要素:结构.完整性和语言 结构(struc ...

  9. Oracle 10046

    [10046 SQL]conn username/passwordalter session set tracefile_identifier = 'id_10046';alter session s ...

  10. 利用AMPScript获取Uber用户数据的访问权限

    现代项目开发和资产管理方法正在不停地快速变化.在这场创新和扩张的竞赛中,新资产被迅速部署并暴露于公共互联网,已有资产也在不断发展. 要跟上这个不断变化的攻击面是很难的,更不用说保护这些应用程序和系统了 ...