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月刊,由于篇幅原因,略作删减,本文为完整版) ...
随机推荐
- Python模块 2
collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型: 1.namedtuple: 生成可以使用名字来访问 ...
- oracle sql生成临时递增数据
1.生成1至10记录 select rownum from dual connect 结果: 12345678910 2.生成最近12个月记录 ), 'yyyyMM') time from dual ...
- [转载]去除文件中的^M
用/bin/cat -v /etc/profile看到很多^M,是因为Windows下的换行符的原因. 现在在linux下面删了就行,我用的第一种方法. 第一种方法 (先yum install -y ...
- ELK6.6.0+filebeat6.6.0部署
elastic不能用root用户去启动,否则会报错,所以创建elastic用户ES集群部署 1.创建elastic用户 $ useradd elastic $ passwd elastic 2..部署 ...
- IOS Xcode编译项目-报错“ld: library not found for -XX”
一般是因为导入新项目的时候报错的.原因是引入的依赖库的问题.重新执行pod install应该可以解决.不过,有时候如果重新执行pod install无法执行,可以采用以下方法: 在终端中cd到项目所 ...
- 记录一下最近的解决的坑爹bug
最近解决的bug长得都很别致啊,记录一下 一 :天气插件引用报403 项目里有一个天气插件引用一直报403 后来确定原因是headers里缺少referer源,无法访问资源的服务器,再后来又发现项目引 ...
- Android 自定义类型文件与程序关联
0x01 功能 实现在其他应用中打开某个后缀名的文件 可以直接跳转到本应用中的某个activity进行处理 0x01 实现 首先创建一个activity ,然后在manifest里对该activity ...
- Jenkins 配置邮件通知步骤
Jenkins 配置邮件通知前言 可以在Jenkins 中配置邮件通知,比如在构建失败时发送邮件通知项目组来及时修复问题. Jenkins 邮件通知功能的插件主要包括: Mailer Plugin ( ...
- 基于百度通用翻译API的一个翻译小工具
前几天写了一个简单的翻译小工具,是基于有道翻译的,不过那个翻译接口有访问限制,超过一定次数后会提示访问过于频繁,偶然发现百度翻译API如果月翻译字符少于200万是不收取费用的,所以就注册了一个百度开发 ...
- NetSec2019 20165327 Exp2 后门原理与实践
NetSec2019 20165327 Exp2 后门原理与实践 快速找到重点: (1)使用netcat获取主机操作Shell,cron启动 (0.5分) (2)使用socat获取主机操作Shell, ...