【LeetCode刷题-双指针】--259.较小的三数之和

259.较小的三数之和

方法:排序+双指针

java 复制代码
class Solution {
    public int threeSumSmaller(int[] nums, int target) {
        Arrays.sort(nums);
        int k = 0;
        for(int i = 0;i<nums.length;i++){
            int start = i + 1,end = nums.length - 1;
            while(start < end){
                int sum = nums[start] + nums[end] + nums[i];
                if(sum < target){
                    k += (end - start);  //因为数组排好序了,所以start到end中的数都小于target
                    start++;
                }
                if(sum >= target){
                    end--;
                }
            }
        }
        return k;
    }
}
相关推荐
橘子汽水1686 分钟前
Leetcode 128,49最长连续序列,字母异位词分组
java·算法·leetcode
圣保罗的大教堂8 分钟前
leetcode 1140. 石子游戏 II 中等
leetcode
mmmmath_38 分钟前
LeetCode.438.找到字符串中所有字母异位词
数据结构·算法·leetcode
Nil20813 分钟前
leetcode 22括号生成
算法·leetcode·深度优先
Navigator_Z17 分钟前
LeetCode //C - 1237. Find Positive Integer Solution for a Given Equation
c语言·算法·leetcode
码少女27 分钟前
数据结构——堆排序
数据结构
政企项目老覃11 小时前
大模型幻觉治理与自动评测:金融风控场景的落地实践
人工智能·算法·机器学习
淡海水12 小时前
08-03-不可变-ImmutableDictionary-TKey-TValue-与ImmutableHashSet-T-持久化哈希树
数据结构·算法·c#·哈希算法·dictionary·immutable
hansang_IR12 小时前
【题解】[APIO2023] 赛博乐园 / cyberland
c++·算法·图论