当前位置:网站首页>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;
}
}
边栏推荐
- MySQL optimization: from ten seconds to three hundred milliseconds
- 信息系统项目管理师核心考点(五十五)配置管理员(CMO)的工作
- mysql uses on duplicate key update to update data in batches
- tf.keras.utils.get_file()
- 运用flask框架发送短信验证码的流程及具体代码
- sql statement - how to query data in another table based on the data in one table
- CentOS7 —— yum安装mysql
- 面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
- 【一起学Rust】Rust学习前准备——注释和格式化输出
- DVWA shooting range environment construction
猜你喜欢

Centos7 install mysql5.7

Quickly master concurrent programming --- the basics

MYSQL一站式学习,看完即学完

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

110 MySQL interview questions and answers (continuously updated)

快速掌握并发编程 --- 基础篇

剑指offer专项突击版 --- 第 4 天

DVWA shooting range environment construction

面试官,不要再问我三次握手和四次挥手

pycharm专业版使用
随机推荐
STM32 - DMA
MySQL transaction isolation level, rounding
Redis Advanced - Cache Issues: Consistency, Penetration, Penetration, Avalanche, Pollution, etc.
Linux系统安装mysql(rpm方式安装)
A complete introduction to JSqlParse of Sql parsing and conversion
sql语句之多表查询
Workflow番外篇
The interviewer asked me TCP three handshake and four wave, I really
MySQL8.0.26安装配置教程(windows 64位)
CentOS7 安装MySQL 图文详细教程
【LeetCode-SQL每日一练】——2. 第二高的薪水
centos7安装mysql5.7步骤(图解版)
MySQL(更新中)
关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
Interviewer, don't ask me to shake hands three times and wave four times again
面试官,不要再问我三次握手和四次挥手
Duplicate entry 'XXX' for key 'XXX.PRIMARY' solution.
matlab abel变换图片处理
The interviewer asked me how to divide the database and the table?Fortunately, I summed up a set of eight-part essays
Pytorch教程Introduction中的神经网络实现示例