当前位置:网站首页>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;
}
}
边栏推荐
- matlab abel变换图片处理
- mysql存储过程
- 一文了解大厂的DDD领域驱动设计
- Mysql application cannot find my.ini file after installation
- ES source code API call link source code analysis
- MySQL8.0.26安装配置教程(windows 64位)
- 面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
- Centos7 install mysql5.7
- Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
- Go中间件
猜你喜欢

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

mysql uses on duplicate key update to update data in batches

Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric

Centos7 install mysql5.7

DVWA之SQL注入

SQL row-column conversion

MySQL事务隔离级别详解

面试官:生成订单30分钟未支付,则自动取消,该怎么实现?

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

matlab abel变换图片处理
随机推荐
DVWA安装教程(懂你的不懂·详细)
numpy和pytorch中的元素拼接操作:stack,concatenat,cat
关于superset集成到自己的项目中
剑指offer专项突击版 ---- 第2天
sql statement - how to query data in another table based on the data in one table
面试官问我TCP三次握手和四次挥手,我真的是
可点击也可直接复制指定内容js
tf.keras.utils.pad_sequences()
DVWA之SQL注入
Paginate the list collection and display the data on the page
.NET-9. A mess of theoretical notes (concepts, ideas)
Unity resources management series: Unity framework how to resource management
Tapdata 与 Apache Doris 完成兼容性互认证,共建新一代数据架构
Linux系统安装mysql(rpm方式安装)
Interviewer, don't ask me to shake hands three times and wave four times again
DVWA installation tutorial (understand what you don't understand · in detail)
pytorch中的一维、二维、三维卷积操作
运用flask框架发送短信验证码的流程及具体代码
三次握手与四次挥手
Kubernetes 证书可用年限修改