Triangle Count
Given an array of integers, how many three numbers can be found in the array, so that we can build an triangle whose three edges length is the three numbers that we find?
Example
Given array S = [3,4,6,7], return 3. They are:
[3,4,6]
[3,6,7]
[4,6,7]
Given array S = [4,4,4,4], return 4. They are:
[4(1),4(2),4(3)]
[4(1),4(2),4(4)]
[4(1),4(3),4(4)]
[4(2),4(3),4(4)]
先sort 然后对于每一个元素i 利用左右双index扫面0到i-1 找出符合条件的元素对
public class Solution {
/**
* @param S: A list of integers
* @return: An integer
*/
public int triangleCount(int S[]) {
Arrays.sort(S);
int count = 0;
for (int i = 0; i < S.length; i++) {
int left = 0;
int right = i - 1;
while (left < right) {
if (S[left] + S[right] > S[i]) {
// The edge from S[left] to S[right - 1] will also form a triangle
count += right - left;
right--;
} else {
left++;
}
}
}
return count;
}
}
Triangle Count的更多相关文章
- 【Lintcode】382.Triangle Count
题目: Given an array of integers, how many three numbers can be found in the array, so that we can bui ...
- LintCode "Triangle Count"
Should be "Medium" or even "Easy".. Just with a little Greedy. class Solution { ...
- [Swift]LeetCode120. 三角形最小路径和 | Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 7九章算法强化班全解--------Hadoop跃爷Spark
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
- GraphX的三大图算法
1. PageRank http://blog.csdn.net/hguisu/article/details/7996185 2. Connected Components 3. Triangle ...
- LeetCode 3Sum Smaller
原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...
- 边工作边刷题:70天一遍leetcode: day 79
3Sum Smaller 要点:类似的题还有lintcode的triangle count:https://github.com/delbao/onlineJudge/blob/master/lint ...
- 明风:分布式图计算的平台Spark GraphX 在淘宝的实践
快刀初试:Spark GraphX在淘宝的实践 作者:明风 (本文由团队中梧苇和我一起撰写,并由团队中的林岳,岩岫,世仪等多人Review,发表于程序员的8月刊,由于篇幅原因,略作删减,本文为完整版) ...
随机推荐
- mysql基操
创建数据表: create table tt1( id int, name varchar(20), age int,sex boolean ); insert into tt1 values(1,& ...
- 【TYVJ 1056】能量项链
[题目链接]传送门 [题解大意] 这题好水,可我还是调了一会,以下为调试中出现过的错误: 1.更新取值时弄清楚区间范围是[l,k][k+1,r]还是[l,k][k,r] 2.对于环形处理时左端点的取值 ...
- Spark机器学习基础一
特征工程 对连续值处理 0.binarizer/二值化 from __future__ import print_function from pyspark.sql import SparkSessi ...
- HTTP请求与接收get/post方式
//get方式 public string HttpGet(string Url, string postDataStr) { HttpWebRequest request = (HttpWebReq ...
- ssm框架如果想要跨域请求,cors跨域
<!-- 跨域 --> <mvc:cors> <mvc:mapping path="/**"/> </mvc:cors> 在spri ...
- 【安卓基础】ImageView与EditText联动实现隐藏与显示密码
项目中经常会有这样的需求,在密码输入框的右边有一个小图标,点击就切换显示和隐藏密码. 其实这里需求实现起来是比较容易的,主要考虑是复用问题,因为登陆.注册.修改密码界面都会有这样的情景,如果每个界面都 ...
- 金蝶K3常用数据表
金蝶K3WISE常用数据表 K3Wise 14.2 清空密码update t_User set FSID=') F ", ,P T #8 *P!D &D 80!N &@ &l ...
- NetSec2019 20165327 Exp3 免杀原理与实践
NetSec2019 20165327 Exp3 免杀原理与实践 pre基础问题回答 一.免杀原理 一般是对恶意软件做处理,让它不被杀毒软件所检测.也是渗透测试中需要使用到的技术. 要做好免杀,就时清 ...
- yii2常用查询
两表连查 $model = Article::find()->joinWith(['type'])->select('new,t_name,article.t_id')->asArr ...
- 思科模拟器PacketTracer7--利用一台交换机和2台pc互连构成小型局域网
实验二—2 实验工具:思科模拟器PacketTracer7(可在思科官网下载,免费) 实验设备: 交换机一台,PC两台,直连线或选择自动匹配 实验步骤: 一.配置网络拓扑图 连线可选择连通线或闪电符号 ...