leetcode 27 移除元素
给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。
不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。
元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
class Solution {
public int removeElement(int[] nums, int val) {
if(nums.length==0) return 0;
//双指针,slow为慢指针,quick为快指针
int slow=0;
for(int quick=0;quick<nums.length;quick++){
if(nums[quick]!=val){
nums[slow++] = nums[quick];
}
}
return slow;
}
}
leetcode209 长度最小的子数组
给定一个含有 n 个正整数的数组和一个正整数 target 。
找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl+1, …, numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。
class Solution {
public int minSubArrayLen(int target, int[] nums) {
//滑动窗口的思想,前一指针先行遍历数组并累加,满足target值后,后一指针开始移动
int minValue = Integer.MAX_VALUE;
int i=0;
int sum=0;
for(int j=0;j<nums.length;j++){
sum += nums[j];
while(sum>=target){
minValue = Math.min(minValue,j-i+1);
sum-=nums[i];
i++;
}
}
return minValue==Integer.MAX_VALUE? 0:minValue;
}
}
leetcode141 环形链表
给定一个链表,判断链表中是否有环。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
如果链表中存在环,则返回 true 。 否则,返回 false 。
public class Solution {
public boolean hasCycle(ListNode head) {
//双指针,若链表中有环,则快指针能追上慢指针
if(head==null) return false;
ListNode sNode = head;
ListNode qNode = head.next;
while(sNode!=qNode){
if(qNode==null||qNode.next==null){
return false;
}
qNode=qNode.next.next;
sNode=sNode.next;
}
return true;
}
}
leetcode15 三数之和
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> lists = new ArrayList<List<Integer>>();
if(nums.length<3) return lists;
Arrays.sort(nums);
for(int a=0;a<nums.length-2;a++){
if(nums[a]>0) break;
if(a>0&&nums[a-1]==nums[a]) continue;
int b = a+1;
int c = nums.length-1;
int target = -nums[a];
while(b<c){
if(nums[b]+nums[c]-target>0){
c--;
}else if(nums[b]+nums[c]-target<0){
b++;
}else{
lists.add(new ArrayList<>(Arrays.asList(nums[a],nums[b],nums[c])));
b++;c--;
while(b<c&&nums[b]==nums[b-1]) b++;
while(b<c&&nums[c]==nums[c+1]) c--;
}
if(b==c) break;
}
}
return lists;
}
}
leetcode18 四数之和
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
int len = nums.length;
List<List<Integer>> res = new ArrayList<>();
if(nums==null||len<4) return res;
Arrays.sort(nums);
for(int a=0;a<len-3;a++){
if(a>0&&nums[a]==nums[a-1]) continue;
for(int b=a+1;b<len-2;b++){
if(b>a+1&&nums[b]==nums[b-1]) continue;
int tar = target-nums[a]-nums[b];
int c = b+1;
int d = len-1;
while(c<d){
if(nums[c]+nums[d]-tar>0){
d--;
}else if(nums[c]+nums[d]-tar<0){
c++;
}else{
res.add(new ArrayList<Integer>(Arrays.asList(nums[a],nums[b],nums[c],nums[d])));
c++;d--;
while(c<d&&nums[c]==nums[c-1]) c++;
while(c<d&&nums[d]==nums[d+1]) d--;
}
}
}
}
return res;
}
}
leetcode142 环形链表II
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
//首先用快慢指针判断是否有环,快指针一次两步,慢指针一次一步
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode quick = dummy;
ListNode low = dummy;
boolean hasCircle = false;
while(quick.next!=null&&quick.next.next!=null){
quick = quick.next.next;
low = low.next;
if(low == quick){
hasCircle = true;
break;
}
}
//若有环,则进行接下来操作
//因为快指针的步长是2,慢指针的步长为1,因此可设慢指针与快指针相遇时慢指针总步长为a,快指针总步长为b
//设环的节点总数为m,快指针与慢指针相遇时刚好比他多走了一个圆环的长度,即m,因此可得出a=m,b=2m
//最关键部分:设入口处为第x个节点,慢指针与快指针相遇时慢指针走到离入口m-x距离的节点处,再走x个步长即可,因此可让他跟一个从头节点出发的节点同时走,相遇时,即走到入口处
if(hasCircle){
ListNode temp = dummy;
while(true){
temp = temp.next;
low = low.next;
if(temp==low){
return temp;
}
}
}
return null;
}
}
leetcode 11 盛最多水的容器
// v1.0 暴力解法,超时
class Solution {
public int maxArea(int[] height) {
int len = height.length;
int area = 0;
int tmp = 0;
int tmpArea = 0;
for(int i=0;i<len;i++){
for(int j=i+1;j<len;j++){
tmp = Math.min(height[i], height[j]);
tmpArea = tmp*(j-i);
area = Math.max(area, tmpArea);
}
}
return area;
}
}
// v2.0
class Solution {
public int maxArea(int[] height) {
// 双指针,两个指针分别指向开始和结尾,然后向内搜索
int i=0,j=height.length-1;
int area = 0;
while(i<j){
area = Math.max(Math.min(height[i],height[j])*(j-i), area);
// height[i]<height[j]则说明i要向右移动,寻找更高的height
if(height[i]<height[j]){
i++;
}else{
j--;
}
}
return area;
}
}