当前位置:网站首页>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;
}
}
边栏推荐
- 剑指offer专项突击版 --- 第 4 天
- Redis的初识
- sql语句之多表查询
- The interviewer asked me how to divide the database and the table?Fortunately, I summed up a set of eight-part essays
- <urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍
- MySQL (updating)
- C语言教程(三)-if和循环
- DVWA靶场环境搭建
- Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
- 账号或密码多次输入错误,进行账号封禁
猜你喜欢

Tapdata 与 Apache Doris 完成兼容性互认证,共建新一代数据架构

MySQL(更新中)

工作流编排引擎-Temporal

MySQL8--Windows下使用压缩包安装的方法

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

面试官,不要再问我三次握手和四次挥手

Refinement of the four major collection frameworks: Summary of List core knowledge

centos7安装mysql5.7

Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ

如何将项目部署到服务器上(全套教程)
随机推荐
SQL语句中对时间字段进行区间查询
面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
Flask 的初识
Why use Flink and how to get started with Flink?
剑指offer专项突击版 ---第 5 天
SQL injection of DVWA
【MySQL8入门到精通】基础篇- Linux系统静默安装MySQL,跨版本升级
快速掌握并发编程 --- 基础篇
C语言的文件操作(一)
Duplicate entry ‘XXX‘ for key ‘XXX.PRIMARY‘解决方案。
Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric
Summary of MySQL common interview questions (recommended collection!!!)
Redis的初识
Multiple table query of sql statement
Flink sink redis 写入Redis
MySQL-Explain详解
对list集合进行分页,并将数据显示在页面中
MySQL optimization slow log query
[mysql improves query efficiency] Mysql database query is slow to solve the problem
DVWA靶场环境搭建