题目:

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)]

题解:

  此类题目首先想到的是数组先排序(数组元素位置不影响结果,且题目与元素大小有关),最简单的是暴力搜索,三层循环。首先k从 2 遍历到 length(S)-1,内循环则为i和j枚举满足条件的情况。这里有个小技巧,因为我们首先对数组进行排序(从小到大),如例1,当S[k] = 7 时, i初始化为0,j初始化为k-1,那么有S[i] + S[j] > S[k],此时不需要再检查4+6>7的情况,因为3已经满足条件了,那么3以后的元素肯定也满足。这样就会减少大量的运算时间。一次遍历即可。

Solution 1

class Solution {
public:
int triangleCount(vector<int> &S) {
int ans = ;
int len = S.size();
if (len < ) {
return ;
}
sort(S.begin(), S.end());
for (int k = ; k < len; ++k) {
int i = ;
int j = k - ;
while (i < j) {
if (S[i] + S[j] > S[k]) {
ans += j - i;
j--;
} else {
i++;
}
}
}
return ans;
}
};

【Lintcode】382.Triangle Count的更多相关文章

  1. 【动态规划】The Triangle

    问题 E: [动态规划]The Triangle 时间限制: 1 Sec  内存限制: 128 MB提交: 24  解决: 24[提交][状态][讨论版] 题目描述 73 88 1 02 7 4 44 ...

  2. 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA

    [BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...

  3. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  4. 【lintcode】 二分法总结 I

     二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...

  5. 【BZOJ】1833 [ZJOI2010]count 数字计数

    [算法]数位DP [题解] 记忆化搜索 #include<cstdio> #include<algorithm> #include<cstring> #define ...

  6. 【POJ】2954 Triangle(pick定理)

    http://poj.org/problem?id=2954 表示我交了20+次... 为什么呢?因为多组数据我是这样判断的:da=sum{a[i].x+a[i].y},然后!da就表示没有数据了QA ...

  7. 【leetcode】Valid Triangle Number

    题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...

  8. 【LintCode】判断一个字符串是否包含另一个字符串的所有字符

    问题描述: 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母. 样例 给出 A = "ABCD" B = "ACD",返 ...

  9. 【Python】Django 聚合 Count与Sum用法,注意点

    代码示例: from django.db.models import Sum, Count #alarm_sum_group_items = models.FILE_PROTECT_ALARM.obj ...

随机推荐

  1. 使用css counter来美化代码片段的样式

    博客园默认的代码片段样式不太美观,特别是复制代码时会把前面的行号也复制下来,操作起来比较麻烦.最近看到一种使用CSS计数器来美化代码片段的方法,于是研究了一下计数器的使用,在此做个笔记. 这是官网的例 ...

  2. implode 函数 把数组拼接成字符串

    $array( '0'=>1, '1'=>5, '2'=>5 ); $str=imploade(',',$array); echo str;//输出1,5,3

  3. testVC.modalPresentationStyle = UIModalPresentationFormSheet; 更改 VC大小

    本文转载至 http://www.cocoachina.com/bbs/simple/?t31199.html TestViewController *testVC = [[TestViewContr ...

  4. EasyNVR无插件直播服务如何配合EasyBMS使用以及实现流媒体管理功能概述

    本文转自:https://blog.csdn.net/black_3717/article/details/79769195 功能概要: 1.摄像机的无插件直播: 2.摄像机的低延时直播: 3.摄像机 ...

  5. android菜鸟学习笔记16----Android项目打包安装过程(Run as Android Application)

    右击项目名称,Run as Android Appication之后,Android项目打包安装过程: 1.打包生成.apk文件: 1)把源码中的.java文件编译生成.class文件 2)将所有的. ...

  6. css jquery 实现轮播效果

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 怎么查看自己的IP地址?

    https://jingyan.baidu.com/article/63f2362816d56c0208ab3dd5.html 1.通过自己的电脑查看的是内部局域网的IP地址 2.通过网上查看的IP地 ...

  8. 九度OJ 1144:Freckles(斑点) (最小生成树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1538 解决:760 题目描述: In an episode of the Dick Van Dyke show, little Richi ...

  9. 创建laravel项目

    下载项目到本地 git clone https://github.com/251068550/LaraBlog.git compoer安装 cd LaraBlog composer install 如 ...

  10. 解决win7打印机共享出现“无法保存打印机设置(错误0x000006d9)的问题

    最新解决win7打印机共享出现“无法保存打印机设置(错误0x000006d9)的问题,由系统下载吧率先分享: 有些用户在使用Windows7系统过程中,碰到到win7打印机共享出现“无法保存打印机设置 ...