当前位置:网站首页>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;
}
}
边栏推荐
- CentOS7 install MySQL graphic detailed tutorial
- Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work
- 面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
- Why use Flink and how to get started with Flink?
- MySQL-如何分库分表?一看就懂
- C语言教程(一)-准备
- 关于LocalDateTime的全局返回时间带“T“的时间格式处理
- MySQL (updating)
- Unity resources management series: Unity framework how to resource management
- The interviewer asked me TCP three handshake and four wave, I really
猜你喜欢
随机推荐
CentOS7 安装MySQL 图文详细教程
关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
Duplicate entry ‘XXX‘ for key ‘XXX.PRIMARY‘解决方案。
Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work
SQL injection of DVWA
Refinement of the four major collection frameworks: Summary of List core knowledge
With MVC, why DDD?
剑指offer专项突击版 --- 第 3 天
Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
A complete introduction to JSqlParse of Sql parsing and conversion
MySQL-如何分库分表?一看就懂
面试官,不要再问我三次握手和四次挥手
Shell重油常压塔模拟仿真与控制
Unity resources management series: Unity framework how to resource management
【MySQL8入门到精通】基础篇- Linux系统静默安装MySQL,跨版本升级
MySQL forgot password
Anaconda配置环境指令
Flink sink redis 写入Redis
Lock wait timeout exceeded解决方案
有了MVC,为什么还要DDD?









