当前位置:网站首页>剑指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;
}
}
边栏推荐
猜你喜欢
随机推荐
110道 MySQL面试题及答案 (持续更新)
Kubernetes加入集群的TOKEN值过期
Go中间件
Redis的初识
12个MySQL慢查询的原因分析
SQL语句中对时间字段进行区间查询
PCL calculates the point cloud coordinate maximum and its index
2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。
CentOS7 —— yum安装mysql
面试官,不要再问我三次握手和四次挥手
Reference code series_1. Hello World in various languages
MySQL (updating)
CentOS7 安装MySQL 图文详细教程
快速掌握并发编程 --- 基础篇
[Detailed explanation of ORACLE Explain]
sql statement - how to query data in another table based on the data in one table
MySQL window function
.NET-9. A mess of theoretical notes (concepts, ideas)
[Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade
为什么要用Flink,怎么入门使用Flink?