力扣数组与双指针题目精选
LeetCode 1.两数之和(简单)
题目如下:
双指针
这里先将数组排序好,再利用双指针法遍历一遍得到结果。
为了保存下标信息另开了一个数组
class Solution {
public int[] twoSum(int[] nums, int target) {
int m=0,n=0,k,board=0;
int[] res=new int[2];
int[] tmp1=new int[nums.length];
//从数组'nums'的索引0开始,将元素复制到从索引0开始的数组'tmp1'中,要复制的元素数量等于'nums'数组的长度
System.arraycopy(nums,0,tmp1,0,nums.length);
Arrays.sort(nums);
//双指针启动!!!
for(int i=0,j=nums.length-1;i<j;){
if(nums[i]+nums[j]<target)
i++;
else if(nums[i]+nums[j]>target)
j--;
else if(nums[i]+nums[j]==target){
m=i;
n=j;
break;
}
}
for(k=0;k<nums.length;k++){
if(tmp1[k]==nums[m]){
res[0]=k;
break;
}
}
for(int i=0;i<nums.length;i++){
if(tmp1[i]==nums[n]&&i!=k)
res[1]=i;
}
return res;
}
}
LeetCode 4. 寻找两个正序数组的中位数(困难)
题目如下:
参考网站二分查找解法 毕竟当时我是用sort做的 哭死
LeetCode 15.三数之后(中等)
题目如下:
双指针
本题的难点在于如何去除重复解
- 特判,对于数组长度 n,如果数组为 null 或者数组长度小于 3,返回 []。
- 对数组进行排序。
- 遍历排序后数组:
- 若 nums[i]>0:因为已经排序好,所以后面不可能有三个数加和等于0,直接返回结果。
- 对于重复元素:跳过,避免出现重复解
- 令左指针 L=i+1,右指针 R=n−1,当 L<R 时,执行循环:
- 当nums[i]+nums[L]+nums[R]==0,执行循环,判断左界和右界是否和下一位置重复,去除重复解。并同时将 L,R 移到下一位置,寻找新的解
- 若和大于 0,说明 nums[R]太大,R 左移
- 若和小于 0,说明 nums[L] 太小,L 右移
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums);
int n = nums.length;
if(n < 3 || nums==null){
return new ArrayList<>();
}
for(int i = 0;i < n;i++){
if(nums[i]>0){
return list;
}
if(i>0&&nums[i] == nums[i-1]){
continue;
}
int left = i+1;
int right = n-1;
while(left < right){
if(nums[i] + nums[left] + nums[right] == 0){
list.add(Arrays.asList(nums[i],nums[left],nums[right]));
while(left < right && nums[left] == nums[left+1]){
left+=1;
}
while(left < right && nums[right] == nums[right-1]){
right-=1;
}
left+=1;
right-=1;
}else if(nums[i] + nums[left] + nums[right] > 0){
right-=1;
}else{
left+=1;
}
}
}
return list;
}
}
LeetCode 75.颜色分类(中等)
题目如下:
双指针
这边直接截图官方解析
第二个说的是这种情况
这时候直接交换就错了
class Solution {
public void sortColors(int[] nums) {
int n = nums.length;
int p0 = 0, p1 = 0;
for (int i = 0; i < n; ++i) {
if (nums[i] == 1) {
int temp = nums[i];
nums[i] = nums[p1];
nums[p1] = temp;
++p1;
} else if (nums[i] == 0) {
int temp = nums[i];
nums[i] = nums[p0];
nums[p0] = temp;
if (p0 < p1) {
temp = nums[i];
nums[i] = nums[p1];
nums[p1] = temp;
}
++p0;
++p1;
}
}
}
}
简洁的双指针 参考力扣大佬解析
首先,所有数都≤2,那么索性把所有数组置为2,然后遇到所有≤1的,就再全部置为1,覆盖了错误的2,这时候所有2的位置已经正确,最后所有≤0的全部置为0,也就覆盖了一些错误的1,这时候,0和1的位置都正确。最重要的就是需要两个指针指向下一个1或0的位置
public void sortColors(int[] nums) {
int n0 = 0, n1 = 0;
for(int i = 0; i < nums.length; i++){
int num = nums[i];
nums[i] = 2;
if(num < 2){
nums[n1++] = 1;
}
if(num < 1){
nums[n0++] = 0;
}
}
}
LeetCode 76.最小覆盖子串(困难)
题目如下:
复杂 参考题解
LeetCode 142.环形链表II(中等)
题目如下:
双指针
根据:f=2s (快指针每次2步,路程刚好2倍)
f = s + nb (相遇时,刚好多走了n圈)
推出:s = nb
从head结点走到入环点需要走 : a + nb, 而slow已经走了nb,那么slow再走a步就是入环点了。
如何知道slow刚好走了a步? 从head开始,和slow指针一起走,相遇时刚好就是a步
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast = head, slow = head;
while (true) {
if (fast == null || fast.next == null) return null;
fast = fast.next.next;
slow = slow.next;
if (fast == slow) break;
}
fast = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return fast;
}
}
LeetCode 234.回文链表(简单)
题目如下:
直接快慢指针加栈判断 嗯java中这个stack(线程安全,效率低) 现在不推荐使用了 建议用ArrayDeque和LinkedList
class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null){
return true;
}
ListNode quick = head;
ListNode slow = head;
while(quick.next != null && quick.next.next != null){
slow = slow.next;
quick = quick.next.next;
}
Stack<ListNode> stack = new Stack<ListNode>();
while(slow != null){
stack.push(slow);
slow = slow.next;
}
while(!stack.isEmpty()){
if(head.val != stack.pop().val){
return false;
}
head = head.next;
}
return true;
}
}
LeetCode 283.移动零(简单)
题目如下:
直接上官方解析
class Solution {
public void moveZeroes(int[] nums) {
int n = nums.length, left = 0, right = 0;
while (right < n) {
if (nums[right] != 0) {
swap(nums, left, right);
left++;
}
right++;
}
}
public void swap(int[] nums, int left, int right) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
}