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月刊,由于篇幅原因,略作删减,本文为完整版) ...
随机推荐
- 不能最为IF判断条件的属性
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...
- CF685B Kay and Snowflake 贪心
CF685B Kay and Snowflake 链接 CF 题目大意 给你一颗树,询问子树的重心 思路 贪心? 重心肯定是向上走的,所以直接向上跳就好了. 不优秀的时候就不要跳了 ,因为以后也不能更 ...
- 一键git push脚本(python版)
#!/usr/bin/env python import os import subprocess import sys import time gitconfig = { 'cwd': './blo ...
- Python游戏编程入门
<Python游戏编程入门>这些文章负责整理在这本书中的知识点.注意事项和课后习题的尝试实现.并且对每一个章节给出的最终实例进行分析和注释. 初识pygame:pie游戏pygame游戏库 ...
- (转)AutoML for Data Augmentation
AutoML for Data Augmentation 2019-04-01 09:26:19 This blog is copied from: https://blog.insightdatas ...
- LeetCode120-Triangle-数组,动态规划
题目描述 Problem Description: Given a triangle, find the minimum path sum from top to bottom. Each ste ...
- C# 如何获取可执行文件路径的上上级目录
1. DirectoryInfo di = new DirectoryInfo(string.Format(@"{0}..\..\", Application.StartupPat ...
- canvas 模拟时钟
<meta charset="utf-8"> <canvas width="1000" height="1000" id= ...
- Android CmakeList
https://www.cnblogs.com/chenxibobo/p/7678389.html
- python3 LDA主题模型以及TFIDF实现
import codecs #主题模型 from gensim import corpora from gensim.models import LdaModel from gensim import ...