当前位置:网站首页>剑指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;
}
}
边栏推荐
- Temporal介绍
- Duplicate entry ‘XXX‘ for key ‘XXX.PRIMARY‘解决方案。
- MySQL transaction isolation level, rounding
- MySQL-Explain详解
- Minio上传文件ssl证书不受信任
- 限流的原理
- Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric
- SQL statement to range query time field
- 面试官,不要再问我三次握手和四次挥手
- Why use Flink and how to get started with Flink?
猜你喜欢
随机推荐
MySQL事务(transaction) (有这篇就足够了..)
What are the advantages and disadvantages of Unity shader forge and the built-in shader graph?
Centos7 install mysql5.7
面试官竟然问我怎么分库分表?幸亏我总结了一套八股文
<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍
sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
.NET-6.WinForm2.NanUI learning and summary
Redis进阶 - 缓存问题:一致性、穿击、穿透、雪崩、污染等.
MySQL forgot password
【MySQL8入门到精通】基础篇- Linux系统静默安装MySQL,跨版本升级
pycharm专业版使用
Go language study notes - dealing with timeout problems - Context usage | Go language from scratch
C语言如何分辨大小端
12 reasons for MySQL slow query
tf.keras.utils.get_file()
a different object with the same identifier value was already associated with the session
[Detailed explanation of ORACLE Explain]
matlab abel变换图片处理
【一起学Rust】Rust的Hello Rust详细解析
ES 源码 API调用链路源码分析









