当前位置:网站首页>剑指offer专项突击版 --- 第 3 天
剑指offer专项突击版 --- 第 3 天
2022-07-31 05:09:00 【米兰的小红黑】
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;
}
}
边栏推荐
- 1. Get data - requests.get()
- Input length must be multiple of 8 when decrypting with padded cipher
- 分布式事务——分布式事务简介、分布式事务框架 Seata(AT模式、Tcc模式、Tcc Vs AT)、分布式事务—MQ
- 关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
- 【MQ我可以讲一个小时】
- 关于superset集成到自己的项目中
- MYSQL下载及安装完整教程
- Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric
- MySQL optimization: from ten seconds to three hundred milliseconds
- Centos7 install mysql5.7 steps (graphical version)
猜你喜欢
Distributed transaction processing solution big PK!
matlab simulink欠驱动水面船舶航迹自抗扰控制研究
【MQ我可以讲一个小时】
MySQL optimization slow log query
DVWA installation tutorial (understand what you don't understand · in detail)
Mysql应用安装后找不到my.ini文件
DVWA shooting range environment construction
目标检测学习笔记
<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍
【一起学Rust】Rust的Hello Rust详细解析
随机推荐
[Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade
Unity resources management series: Unity framework how to resource management
ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)
SQL行列转换
Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
一文了解大厂的DDD领域驱动设计
MySQL-如何分库分表?一看就懂
MySQL优化之慢日志查询
MySQL transaction (transaction) (this is enough..)
Minio upload file ssl certificate is not trusted
MySQL (updating)
面试官问我TCP三次握手和四次挥手,我真的是
Summary of MySQL common interview questions (recommended collection!!!)
mysql使用on duplicate key update批量更新数据
【一起学Rust】Rust学习前准备——注释和格式化输出
信息系统项目管理师核心考点(五十五)配置管理员(CMO)的工作
Minesweeper game (written in c language)
tf.keras.utils.get_file()
The interviewer asked me TCP three handshake and four wave, I really
Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric