当前位置:网站首页>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;
}
}
边栏推荐
- 基于web3.0使用钱包Metamask的三方登陆
- Workflow番外篇
- Go中间件
- 基于flask的三方登陆的流程
- Linux的mysql报ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘ (using password NOYSE)
- Reference code series_1. Hello World in various languages
- Temporal介绍
- Mysql application cannot find my.ini file after installation
- 关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
- .NET-6.WinForm2.NanUI learning and summary
猜你喜欢

基于flask的三方登陆的流程

MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细

Summary of MySQL common interview questions (recommended collection!!!)

The interviewer asked me TCP three handshake and four wave, I really

Temporal介绍

MySQL_关于JSON数据的查询

MySQL window function

面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式

Mysql application cannot find my.ini file after installation

快速掌握并发编程 --- 基础篇
随机推荐
datagrip带参sql查询
SQL statement to range query time field
MySQL事务(transaction) (有这篇就足够了..)
SQL语句中对时间字段进行区间查询
Interviewer: If the order is not paid within 30 minutes, it will be automatically canceled. How to do this?
有了MVC,为什么还要DDD?
[Detailed explanation of ORACLE Explain]
Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
DVWA shooting range environment construction
[mysql improves query efficiency] Mysql database query is slow to solve the problem
2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。
面试官,不要再问我三次握手和四次挥手
一文了解大厂的DDD领域驱动设计
Duplicate entry 'XXX' for key 'XXX.PRIMARY' solution.
MySQL optimization slow log query
SQL injection of DVWA
Apache DButils使用注意事项--with modifiers “public“
Anaconda配置环境指令
MySQL (updating)
【LeetCode-SQL每日一练】——2. 第二高的薪水