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的更多相关文章

  1. 【Lintcode】382.Triangle Count

    题目: Given an array of integers, how many three numbers can be found in the array, so that we can bui ...

  2. LintCode "Triangle Count"

    Should be "Medium" or even "Easy".. Just with a little Greedy. class Solution { ...

  3. [Swift]LeetCode120. 三角形最小路径和 | Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  4. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  5. 7九章算法强化班全解--------Hadoop跃爷Spark

    ------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...

  6. GraphX的三大图算法

    1. PageRank http://blog.csdn.net/hguisu/article/details/7996185 2. Connected Components 3. Triangle ...

  7. LeetCode 3Sum Smaller

    原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...

  8. 边工作边刷题:70天一遍leetcode: day 79

    3Sum Smaller 要点:类似的题还有lintcode的triangle count:https://github.com/delbao/onlineJudge/blob/master/lint ...

  9. 明风:分布式图计算的平台Spark GraphX 在淘宝的实践

    快刀初试:Spark GraphX在淘宝的实践 作者:明风 (本文由团队中梧苇和我一起撰写,并由团队中的林岳,岩岫,世仪等多人Review,发表于程序员的8月刊,由于篇幅原因,略作删减,本文为完整版) ...

随机推荐

  1. Java类的加载和对象创建流程的详细分析

    相信我们在面试Java的时候总会有一些公司要做笔试题目的,而Java类的加载和对象创建流程的知识点也是常见的题目之一.接下来通过实例详细的分析一下: package com.test; public ...

  2. C# base64编码、解码

    public class TransferCode { #region base-64编码.解码 /// <summary> /// BASE64编码 /// </summary&g ...

  3. Python 开发 项目《外星人入侵》

    2019-02-05 本篇心路历程: 本篇是打算记录自己的第一个python项目,也是众人皆知的<外星人入侵项目>,本项目大概500多行.趁着寒假,大概耗时3天吧,把完整代码敲了出来,当然 ...

  4. 人脸识别-arcface损失函数

    参考博客: L-margin softmax loss:https://blog.csdn.net/u014380165/article/details/76864572 A-softmax loss ...

  5. dedecmsV5.7织梦后台更新文章,发布时间不自动更新

    问题:dedecmsV5.7后台修改文章的时候,会更新发布时间,需求是不自动更新时间,还是当时的发布时间 解决: 1.修改后台文件夹/templets/archives_edit.htm,articl ...

  6. 记录下用axios遇到的问题

    这两天用axios做页面登录.遇到了N多问题,.首先是报Response to preflight request doesn't pass access control check: No 'Acc ...

  7. tortoisegit里的cleanup坑

    tortoisegit的clean up功能(以下红框部分)真的不要去点啊,你所有被ignore(忽略)的本地文件会被全部删除,而且是无法恢复的,因为远程仓库里根本就没有这些文件. 血的教训啊,本以为 ...

  8. subline 建立服务器

    http://blog.csdn.net/csdn_chenli/article/details/53173953

  9. PHP反射学习总结

    反射(Reflection) PHP的反射机制提供了一套反射API,用来访问和使用类.方法.属性.参数和注释等,比如可以通过一个对象知道这个对象所属的类,这个类包含哪些方法,这些方法需要传入什么参数, ...

  10. 生成SQL Server数据字典

    1.表信息 Select * FROM INFORMATION_SCHEMA.COLUMNS order by Table_name; select * from INFORMATION_SCHEMA ...