第一题:

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. 【LookLook文档】通过less 定制自己的Bootstrap 样式

    闲话 学习还是看文档最快,看文档要仔细点,不可走马观花.感谢 “Bootstrap中文网” 思路 1.早在2015开始接触,由于公司中使用的是easyui 固一直没完全使用Bootstrap,开始接触 ...

  2. 如何在云服务器创建maven私有仓库

    参考链接:https://blog.csdn.net/silence_jjj/article/details/77531916 nexus3创建maven私有仓库(windows) 1.官网:http ...

  3. shell 爬取图片下载到本地

    #!/bin/bash #ddmm // #if [ -z $string ] 如果string 为空 #-z STRING the length of STRING is zero read -p ...

  4. 程序员的沟通之痛https://blog.csdn.net/qq_35230695/article/details/80283720

    个人理解: 一般刚工作的程序员总觉得技术最重要.但是当工作年限超过3年.或者岗位需要涉及汇报.需求对接等就会发现沟通非常重要.也许在大公司还不那么明显,但是在小公司.小团队或者创业,沟通甚至可以说是第 ...

  5. 包的初识和进阶&异常处理

    包 包是一种通过使用‘.模块名’来组织python模块名称空间的方式. 1. 无论是import形式还是from...import形式,凡是在导入语句中(而不是在使用时)遇到带点的,都要第一时间提高警 ...

  6. ADB——查看手机设备信息

    查看设备信息 查看手机型号 adb shell getprop ro.product.model 查看电池状况 adb shell dumpsys battery ''' Current Batter ...

  7. XMPP技术之Smack库的自定义消息扩展

    写此文是为了纪念我耗时两天的Smack库应用开发. 太苦恼了,网上找了一堆材料,关于XMPP的消息扩展方面的资料感觉都是出于同一个源头,基本问题还是基于的库版本都是低于4.1版本的讲解. 我是在Sma ...

  8. python可以提高程序执行速度N倍你知道吗?

    1.1.Numba的约5分钟指南 Numba是Python的即时编译器,它最适用于使用NumPy数组和函数以及循环的代码.使用Numba的最常用方法是通过其装饰器集合,可以应用于您的函数来指示Numb ...

  9. 用php写一个99乘法表

    运行结果如下

  10. Sublime text 替换成对标签 首尾匹配的HTML 标签

    按Cmd-Shift-K (Win: Ctrl-Shift-') 就能选中这组标签