当前位置:网站首页>Swordsman Offer Special Assault Edition --- Day 3
Swordsman Offer Special Assault Edition --- Day 3
2022-07-31 05:31:00 【Milan's little red and black】
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(nums);
for(int i = 0; i < nums.length - 2; i++){
if(nums[i] > 0){
break;
}
if (i > 0 && nums[i] == nums[i - 1]) continue;
int left = i + 1;
int right = nums.length - 1;
while(left < right){
if(nums[i] + nums[left] + nums[right] == 0){
List<Integer> list = new ArrayList<>();
list.add(nums[i]);
list.add(nums[left]);
list.add(nums[right]);
ans.add(list);
while (left < right && nums[left] == nums[left +1]){
left++;
}
left++;
while (left < right && nums[right] == nums[right -1]){
right--;
}
right--;
}else if(nums[i] + nums[left] + nums[right] > 0){
right--;
}else{
left++;
}
}
}
return ans;
}
}
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int n = nums.length;
if(n <= 0){
return 0;
}
int ans = Integer.MAX_VALUE;
int end = 0;
int start = 0;
int sum = 0;
while(end < n){
sum += nums[end];
while(sum >= target){
ans = Math.min(ans, end - start + 1);
sum -= nums[start];
start++;
}
end++;
}
return ans == Integer.MAX_VALUE ? 0 : ans;
}
}
class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
int left = 0;
int ret = 0;
int total = 1;
for (int right = 0; right < nums.length; right++) {
total *= nums[right];
while (left <= right && total >= k) {
total /= nums[left];
left++;
}
if (left <= right) {
ret += right - left + 1;
}
}
return ret;
}
}
边栏推荐
- Redis的初识
- 限流的原理
- TOGAF之架构标准规范(一)
- DVWA installation tutorial (understand what you don't understand · in detail)
- CentOS7 - yum install mysql
- C语言教程(二)-printf及c自带的数据类型
- Why use Flink and how to get started with Flink?
- Input length must be multiple of 8 when decrypting with padded cipher
- Sql解析转换之JSqlParse完整介绍
- 账号或密码多次输入错误,进行账号封禁
猜你喜欢
DVWA installation tutorial (understand what you don't understand · in detail)
With MVC, why DDD?
剑指offer专项突击版 --- 第 4 天
MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细
Numpy中np.meshgrid的简单用法示例
【LeetCode-SQL每日一练】——2. 第二高的薪水
[mysql improves query efficiency] Mysql database query is slow to solve the problem
pycharm专业版使用
【MQ我可以讲一个小时】
MYSQL一站式学习,看完即学完
随机推荐
Redis进阶 - 缓存问题:一致性、穿击、穿透、雪崩、污染等.
About the problems encountered by Xiaobai installing nodejs (npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
剑指offer专项突击版 ---- 第1天
MySQL window function
110 MySQL interview questions and answers (continuously updated)
目标检测学习笔记
MySQL optimization slow log query
MySQL-如何分库分表?一看就懂
Anaconda配置环境指令
MySQL (updating)
剑指offer专项突击版 ---- 第2天
MySQL transaction (transaction) (this is enough..)
MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细
为什么要用Flink,怎么入门使用Flink?
Redis的初识
剑指offer专项突击版 --- 第 3 天
【一起学Rust】Rust的Hello Rust详细解析
2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。
Go中间件
MySQL事务隔离级别详解