题目地址:

https://www.lintcode.com/problem/valid-triangle-number/description

给定一个包含非负整数的数组AAA,问从其能挑出多少三元组可以作为三角形的三个边长。

如果lA<3l_A<3lA​<3直接返回000。接着将AAA由小到大排序,然后从A[2]A[2]A[2]开始向后遍历,每次遍历到A[i]A[i]A[i]的时候相当于求一下A[0:i−1]A[0:i-1]A[0:i−1]中和大于A[i]A[i]A[i]的数对个数,这可以用对撞双指针算法解决。开两个指针lll和rrr分别从000和i−1i-1i−1出发,如果A[l]+A[r]>A[i]A[l]+A[r]>A[i]A[l]+A[r]>A[i],那么可以知道A[l:r−1]A[l:r-1]A[l:r−1]的所有数字与A[r]A[r]A[r]的和都大于A[i]A[i]A[i],累加个数r−lr-lr−l并左移rrr;否则右移lll。代码如下:

import java.util.Arrays;public class Solution {/*** @param nums: the given array* @return:  the number of triplets chosen from the array that can make triangles*/public int triangleNumber(int[] nums) {// Write your code hereif (nums.length < 3) {return 0;}int res = 0;Arrays.sort(nums);for (int i = 2; i < nums.length; i++) {int l = 0, r = i - 1;while (l < r) {int sum = nums[l] + nums[r];if (sum > nums[i]) {res += r - l;r--;} else {l++;}}}return res;}
}

时间复杂度O(n2)O(n^2)O(n2),空间O(1)O(1)O(1)。

【Lintcode】1132. Valid Triangle Number相关推荐

  1. 【LintCode】算法题 1443. 最长AB子串

    描述 给你一个只由字母'A'和'B'组成的字符串s,找一个最长的子串,要求这个子串里面'A'和'B'的数目相等,输出该子串的长度. 这个子串可以为空. s的长度n满足 2<=n<=1000 ...

  2. 【Lintcode】444. Graph Valid Tree II

    题目地址: https://www.lintcode.com/problem/444/ 要求设计一个数据结构,可以做如下两个操作: 1.void addEdge(int a, int b)在aaa与b ...

  3. 【Lintcode】1354. Pascal‘s Triangle II

    题目地址: https://www.lintcode.com/problem/pascals-triangle-ii/description 计算杨辉三角的第nnn行,从000开始计数.用两个list ...

  4. 【Lintcode】046.Majority Number

    题目: Given an array of integers, the majority number is the number that occurs more than half of the ...

  5. [Swift]LeetCode611. 有效三角形的个数 | Valid Triangle Number

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  6. 【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连

    解析 max ksubarray sum:  最大和 of 连续子序列 =>   最大和 of  k份连续子序列 属于dp,30行代码搞定,注意一些边界. substr diff:  无queu ...

  7. 【转】POJ-2104(K-th Number 划分树)

    [题目描述]有n个数字排成一列,有m个询问,格式为:left right k 即问在区间[left,right]第k大的数据为多少?建图: 建树的过程比较简单,对于区间[l,r],首先通过对原数组的排 ...

  8. 【Lintcode】1645. Least Subsequences

    题目地址: https://www.lintcode.com/problem/1645/ 给定一个长nnn的数组AAA,问AAA最少能分解为多少个严格递减的子序列之并. 在AAA上定义偏序关系< ...

  9. 【Lintcode】1375. Substring With At Least K Distinct Characters

    题目地址: https://www.lintcode.com/problem/substring-with-at-least-k-distinct-characters/description 给定一 ...

  10. 【Lintcode】1647. Path Search

    题目地址: https://www.lintcode.com/problem/path-search/description 给定一个无向图,再给定两个点SSS和TTT,求所有SSS到TTT的简单路径 ...

最新文章

  1. laydate兼容bootstrap
  2. Oracle数据库监听配置|转|
  3. 语言条件语序心得_考研分享 | 王远新语言学教程要点总结(第五章)
  4. 百合数c语言360问答,《百合花》
  5. Linux开启文件共享服务
  6. AVR 工具指南(一)
  7. R_circlize包_和弦图(一)
  8. Cglib 代码生成库使用快速入门
  9. .NetCore控制台应用程序
  10. 2022年JDK-8下载方法
  11. matlab张正友程序,张正友标定程序—MATLAB
  12. 手机当台式电脑摄像头
  13. 三菱PlC计数器与定时的使用
  14. C++ std::string::substr()
  15. C++字符,字符串,数字,小写,大写的相互转化
  16. 论文笔记-对话系统综述
  17. 骑士周游 探索访问
  18. 南京师范大学计算机专业考研分数,南京师范大学2021考研分数线已公布
  19. VS2017 + Qt设置窗口置顶与不置顶的注意问题
  20. Pygame 教程(4)拓展:使用 subsurface 方法

热门文章

  1. 华为智慧屏鸿蒙应用,华为智慧屏S Pro体验:告诉你鸿蒙OS有多优秀
  2. 【File类、递归】
  3. 基于VUE和Node.js的医院挂号预约管理系统
  4. Warning: Attempt to present ... on … which is already presenting null
  5. 如何给PDF加密码保护?这3种方法总有一个能用上
  6. HTML的基本标签及属性
  7. Java 基础接口——fly
  8. linux编辑文本(vim)时跳转到最后一行和第一行及相关指令 CentOS------编辑、修改文件命令
  9. 差分法求一阶导数二阶导数,matlab
  10. 特征锦囊:特征无量纲化的常见操作方法