当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
信息系统项目管理师核心考点(五十五)配置管理员(CMO)的工作
Unity resources management series: Unity framework how to resource management
面试官竟然问我怎么分库分表?幸亏我总结了一套八股文
MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细
剑指offer专项突击版 --- 第 3 天
[MQ I can speak for an hour]
MySQL transaction isolation level, rounding
面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
12个MySQL慢查询的原因分析
Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work
随机推荐
账号或密码多次输入错误,进行账号封禁
STM32 - DMA
【mysql 提高查询效率】Mysql 数据库查询好慢问题解决
ABC D - Distinct Trio (Number of k-tuples
Multiple table query of sql statement
质量小议12 -- 以测代评
Why use Flink and how to get started with Flink?
MySQL transaction isolation level, rounding
分布式事务——分布式事务简介、分布式事务框架 Seata(AT模式、Tcc模式、Tcc Vs AT)、分布式事务—MQ
.NET-9. A mess of theoretical notes (concepts, ideas)
On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels
Temporal客户端模型
centos7安装mysql5.7步骤(图解版)
Minio upload file ssl certificate is not trusted
matlab abel变换图片处理
Centos7 install mysql5.7 steps (graphical version)
MySQL优化之慢日志查询
Flask 的初识
MySQL forgot password
快速掌握并发编程 --- 基础篇