当前位置:网站首页>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;
}
}
边栏推荐
- mysql使用on duplicate key update批量更新数据
- SQL行列转换
- Sql解析转换之JSqlParse完整介绍
- MySQL优化:从十几秒优化到三百毫秒
- Flink sink redis 写入Redis
- MySQL优化之慢日志查询
- Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
- 12 reasons for MySQL slow query
- 为什么要用Flink,怎么入门使用Flink?
- 110 MySQL interview questions and answers (continuously updated)
猜你喜欢

Unity Framework Design Series: How Unity Designs Network Frameworks

On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels

Why use Flink and how to get started with Flink?

DVWA shooting range environment construction
![<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍](/img/a4/8c75fab6a9858c5ddec25f6a8300fb.png)
<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍

Unity mobile game performance optimization series: performance tuning for the CPU side

centos7安装mysql5.7步骤(图解版)

pycharm专业版使用

Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work

MySQL (updating)
随机推荐
A complete introduction to JSqlParse of Sql parsing and conversion
ES source code API call link source code analysis
分布式事务处理方案大 PK!
docker安装postgresSQL和设置自定义数据目录
SQL行列转换
Go中间件
Flask 的初识
C语言教程(三)-if和循环
MySQL优化之慢日志查询
面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
MYSQL一站式学习,看完即学完
Redis Advanced - Cache Issues: Consistency, Penetration, Penetration, Avalanche, Pollution, etc.
sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
mysql 的简单运用命令
CentOS7 - yum install mysql
Unity Framework Design Series: How Unity Designs Network Frameworks
有了MVC,为什么还要DDD?
目标检测学习笔记
MySQL window function
Distributed transaction processing solution big PK!