当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
随机推荐
.NET-9. A mess of theoretical notes (concepts, ideas)
Why use Flink and how to get started with Flink?
2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。
Mysql——字符串函数
tf.keras.utils.get_file()
What are the advantages and disadvantages of Unity shader forge and the built-in shader graph?
The monitoring of Doris study notes
Three oj questions on leetcode
MySQL优化之慢日志查询
pytorch中的一维、二维、三维卷积操作
质量小议12 -- 以测代评
Temporal介绍
sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
MySQL8--Windows下使用压缩包安装的方法
.NET-6.WinForm2.NanUI learning and summary
MySQL transaction (transaction) (this is enough..)
Minesweeper game (written in c language)
面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
Numpy中np.meshgrid的简单用法示例








