当前位置:网站首页>剑指offer专项突击版 --- 第 3 天
剑指offer专项突击版 --- 第 3 天
2022-07-31 05:09:00 【米兰的小红黑】

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;
}
}
边栏推荐
猜你喜欢

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

MySQL忘记密码怎么办

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

sql语句之多表查询

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

精解四大集合框架:List 核心知识总结

【MySQL8入门到精通】基础篇- Linux系统静默安装MySQL,跨版本升级

ES source code API call link source code analysis

基于flask的三方登陆的流程

面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
随机推荐
mysql uses on duplicate key update to update data in batches
Numpy中np.meshgrid的简单用法示例
ES source code API call link source code analysis
基于flask的三方登陆的流程
MySQL-如何分库分表?一看就懂
SQL行列转换
Blockbuster | foundation for platinum, gold, silver gave nameboards donors
sql语句之多表查询
MySQL8--Windows下使用压缩包安装的方法
【MySQL8入门到精通】基础篇- Linux系统静默安装MySQL,跨版本升级
Mysql application cannot find my.ini file after installation
MySQL事务隔离级别详解
[Detailed explanation of ORACLE Explain]
MySQL(更新中)
SQL statement to range query time field
【mysql 提高查询效率】Mysql 数据库查询好慢问题解决
质量小议12 -- 以测代评
Redis进阶 - 缓存问题:一致性、穿击、穿透、雪崩、污染等.
Duplicate entry ‘XXX‘ for key ‘XXX.PRIMARY‘解决方案。
对list集合进行分页,并将数据显示在页面中