Leetcode刷题

打算跟着代码随想录的顺序刷一个暑期,看看效果如何,发个帖记录一下

6.25

704 二分查找

class Solution {
    public int search(int[] nums, int target) {
        int len = nums.length;
        int left = 0; // 初始为数组0位置
        int right = len-1; // 初始为数组最后一个数
        int mid  = len / 2;
        
        while(nums[mid] != target){
            if(left == right){
                mid = -1;
                break;
            }
            if(nums[mid] > target){
                right = mid - 1;
                mid = (left + right) / 2;
                continue;
            }
            else{
                left = mid+1;
                mid = (left + right) / 2;
                continue;
            }
        }
        return mid;
    }
}

27 移除元素

class Solution {
    public int removeElement(int[] nums, int val) {
        int fast = 0; // 定义快指针
        int slow = 0; // 定义慢指针
        int len = nums.length;
        int count = 0;
        for(fast = 0; fast < len; fast++){
            if(nums[fast] != val){
                nums[slow] = nums[fast];
                slow++;
            }
            else {
                continue;
            }
        }
        return slow;
    }
}

6.26

977、有序数组的平方

class Solution {
    public int[] sortedSquares(int[] nums) {
        int i=0;
        int len = nums.length;
        int j = len-1;
        int k = len-1;
        int[] result = new int[len];
        while(i<=j){
            if(nums[i]*nums[i] > nums[j] * nums[j]){
                result[k--] = nums[i]*nums[i];
                i++;
            }
            else{
                result[k--] = nums[j] * nums[j];
                j--;
            }
        }
        return result;
    }
}

209、拿下滑动窗口

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int i=0;
        int j=0;
        int len = nums.length;
        int sum = 0;
        int result = Integer.MAX_VALUE;
        for(j=0; j<len; j++){
            sum += nums[j];
            while(sum >= target){
                sum = sum - nums[i];
                result = Math.min(result,j-i+1);
                i++;
            }
        }
        if(result == Integer.MAX_VALUE)
            return 0;
        return result; 
    }
}

6.27

59、螺旋矩阵II

class Solution {
    public int[][] generateMatrix(int n) {

        int[][] mat = new int[n][n];

        if(n == 1){
            mat[0][0] = 1;
            return mat;
        }
        int startX = 0; //右边界
        int startY = 0; //下边界
        int offside = 1;
        int a = 1;
        int i = 1;
        int j = 0; // 行
        int k = 0; // 列
        while(i <= n/2){
            for(k=startY; k<n-offside; k++){
                mat[startX][k] = a;
                a++;
            }
            for(j = startX; j<n-offside; j++){
                mat[j][k] = a;
                a++;
            }
            for(;k>startY;k--){
                mat[j][k] = a;
                a++;
            }
            for(;j>startX; j--){
                mat[j][k] = a;
                a++;
            }
            startX++;
            startY++;
            offside++;
            i++;
        }
        
        if(n%2 != 0){
            mat[n/2][n/2] = n*n;
        }
        return mat;
    }
}

203、移除链表元素

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummy = new ListNode();
        dummy.next = head;
        ListNode h1 = dummy;

        while(h1.next!=null){
            if(h1.next.val == val)
                h1.next = h1.next.next;
            else{
                h1 = h1.next;
            }
        }

        return dummy.next;
    }
}

206、反转链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head==null || head.next == null)
            return head;
        ListNode pre = null;
        ListNode current = head;
        ListNode tmp =  null;
        
        while(current !=null){
            tmp = current.next;
            current.next = pre;
            pre = current;
            current = tmp;

        }
        return pre;
    }
}

6.28

707、设计链表

class ListNode{
    int val;
    ListNode next;
    ListNode(){}
    ListNode(int val){
        this.val = val;
    }
}

class MyLinkedList {
    int size; //节点个数
    ListNode head; //虚拟头结点

    public MyLinkedList() {
        size = 0;
        head = new ListNode();
    }
    
    public int get(int index) {
        if(index<0 || index >= size){
            return -1;
        }

        ListNode cur = head;
        for(int i = 0; i<=index; i++){
            cur = cur.next;
        }
        return cur.val;
    }
    
    public void addAtHead(int val) {
        ListNode tmp = new ListNode(val);
        tmp.next = head.next;
        head.next = tmp;
        size++;
    }
    
    public void addAtTail(int val) {
        if(size==0){
            ListNode tmp = new ListNode(val);
            head.next = tmp;
            tmp.next = null;
            size++;
        }
        else{
        ListNode cur = head;
        while(cur.next!=null)
            cur = cur.next;
        ListNode tmp = new ListNode(val);
        cur.next = tmp;
        tmp.next = null;
        size++;}
    }
    
    public void addAtIndex(int index, int val) {
        if(index > size)
            return;
        if(index<=0){
            addAtHead(val);
            return;
        }
        ListNode cur = head;
        ListNode tmp = new ListNode(val);
        for(int i=0; i<index; i++){
            cur = cur.next;
        }
        tmp.next = cur.next;
        cur.next = tmp;
        size++;
    }
    
    public void deleteAtIndex(int index) {
        if(index<0 || index >=size)
            return;
        if(index == 0)
        {
            head = head.next;
            size--;
            return;
        }
        ListNode cur = head;
        for(int i=0; i<index; i++){
            cur = cur.next;
        }
        if(cur.next!=null)
            cur.next = cur.next.next;
        else cur.next = null;
        size--;
    }
}

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList obj = new MyLinkedList();
 * int param_1 = obj.get(index);
 * obj.addAtHead(val);
 * obj.addAtTail(val);
 * obj.addAtIndex(index,val);
 * obj.deleteAtIndex(index);
 */

这道题要注意index到底代表什么,debug了一小时我也是醉了

24、两两交换链表中的节点

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode swapPairs(ListNode head) {

        if(head==null || head.next==null)
            return head;

        ListNode b,c;
        ListNode dummy = new ListNode();
        ListNode a = dummy;
        dummy.next = head;
        ListNode h1 = head;

        b = head;
        c = head.next;
        ListNode tmp;
        while(b!=null && c!=null){
            b.next = c.next;
            c.next = a.next;
            a.next = c;
            
            a = b;
            b = b.next;
            if(b!=null)
                c = b.next;
        }

        return dummy.next;
    }
}

6.29

160、相交链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode(int x) {
 * val = x;
 * next = null;
 * }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA==null || headB==null)
            return null;
        if(headA==headB){
                return headA;
        }
        ListNode h1 = headA;
        ListNode h2 = headB;
        int countA = 1;
        while(h1.next!=null){
            h1 = h1.next;
            countA++;    
        }
        int countB = 1;
        while(h2!=h1 && h2!=null){
            h2 = h2.next;
            countB++;
        }
        if(h2 == null)
            return null;
        
        ListNode h3 = headA;
        ListNode h4 = headB;
        if(countA > countB){
            int count = countA - countB;
            while(count>0){
                h3 = h3.next;
                count--;
            }
            while(h3!=h4){
                h3 = h3.next;
                h4 = h4.next;
            }
            return h3;
        }

        if(countA < countB){
            int count = countB - countA;
            while(count>0){
                h4 = h4.next;
                count--;
            }
            while(h3!=h4){
                h3 = h3.next;
                h4 = h4.next;
            }
            return h3;
        }

        if(countA == countB){
            while(h3!=h4){
                h3 = h3.next;
                h4 = h4.next;
            }
            return h3;
        }
        return null;
    }
}

19、删除链表的倒数第N个节点

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode();
        dummy.next = head;

        ListNode slow = dummy;
        ListNode fast = dummy;

        for(int i=1; i<=n+1; i++)
            fast = fast.next;
        while(fast!=null){
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return dummy.next;
    }
}
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode h = head;
        int count=0;
        while(h!=null){
            h = h.next;
            count++;
        }

        if(count == n){
            head = head.next;
            return head;
        }
        
        ListNode h1 = head;
        for(int i=0; i<count-n-1; i++){
            h1 = h1.next;
        }
        
        h1.next = h1.next.next;

        return head;

    }
}

142、环形链表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) {
        if(head==null || head.next==null)
            return null;
        ListNode fast= head;
        ListNode slow = head;
        ListNode h1 = head;
        while(fast!=null && fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                while(h1!=fast){
                    h1 = h1.next;
                    fast = fast.next;
                }
                return fast;
            }
        }
        return null;    
    }
}

6.30

242、有效的字母异位词

class Solution {
    Map<Character, Integer> map = new HashMap<>();
    public boolean isAnagram(String s, String t) {
        int len = s.length();
        for (int i = 0; i < len; i++) {
            char c = s.charAt(i);
            if (map.containsKey(c)) {
                map.put(c, map.get(c) + 1);
            } else {
                map.put(c, 1);
            }
        }

        int len2 = t.length();
        for (int i = 0; i < len2; i++) {
            char c = t.charAt(i);
            if (map.containsKey(c)) {
                map.put(c, map.get(c) - 1);
            } else {
                return false;
            }
        }
        for(Map.Entry<Character, Integer> entry : map.entrySet()){
            if(entry.getValue()!=0)
                return false;
        }
        return true;
    }
}

7.1

349、两个数组的交集

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();

        int len = nums1.length;
        for(int i=0; i<len; i++){
            set.add(nums1[i]);
        }
        int len2 = nums2.length;

        for(int i=0; i<len2; i++){
            if(set.contains(nums2[i])){
                set2.add(nums2[i]);
            }
        }
        Integer[] arr1 = set2.toArray(new Integer[0]);
        int[] arr = new int[arr1.length];
        for(int i=0; i<arr1.length; i++){
            arr[i] = arr1[i];
        }
        return arr;
    }
}

基础还是不扎实,set转成数组发现自己竟然不会。晕倒。

202、快乐数

class Solution {
    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        int sum = 0;
        while(sum!=1){
            n = sum(n);
            sum = n;
            if(set.contains(sum)){
                return false;
            }
            else
                set.add(sum);
        }
        return true;
    }

    public int sum(int n){
        int sum = 0;
        int a = 0;
        while(n!=0){
            a = n%10;
            sum += a*a;
            n = n/10;
        }
        return sum;
    }
}

7.2

1、两数之和

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> map = new HashMap<>();
        int[] arr = new int[2];
        for(int i=0; i<nums.length; i++){
            int tmp = target - nums[i];
            if(!map.containsKey(tmp)){
                map.put(nums[i], i);
            }
            else{
                int a = map.get(tmp);
                arr[0] = a;
                arr[1] = i;
                return arr;
            } 
        }
        return null;
    }
}

454、四数相加II

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        int count = 0;
        Map<Integer, Integer> map1 = new HashMap<>();
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                int sum = nums1[i] + nums2[j];
                if (map1.containsKey(sum)) {
                    map1.put(sum, map1.get(sum) + 1);
                } else
                    map1.put(sum, 1);
            }
        }

        for (int i = 0; i < nums3.length; i++) {
            for (int j = 0; j < nums4.length; j++) {
                int sum = nums3[i] + nums4[j];
                int sum1 = 0 - sum;
                if(map1.containsKey(sum1)){
                    count += map1.get(sum1);
                }
            }
        }
        return count;
    }
}

7.3

383、赎金信

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        Map<Character, Integer> map = new HashMap<>();
        for(int i=0; i<magazine.length(); i++){
            char c = magazine.charAt(i);
            if(map.containsKey(c)){
                map.put(c, map.get(c)+1);
            }
            else{
                map.put(c, 1);
            }
        }
        for(int i=0; i<ransomNote.length(); i++){
            char c =ransomNote.charAt(i);
            if(!map.containsKey(c))
                return false;
            else if(map.get(c)==0){
                return false;
            }
            else{
                map.put(c, map.get(c)-1);
            }
        }
        return true;
    }
}

15、三数之和

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {

        Arrays.sort(nums);
        List<List<Integer>> list = new ArrayList<>();

        int left = 0; // 左指针
        
        for(left=0; left<nums.length-2; left++){

            if(nums[left]>0){
                break;
            } //左指针指向的数大于0直接返回
            if(left>0 && nums[left] == nums[left-1]){
                continue;
            } //去除左指针重复的数

            int right = nums.length - 1; // 右指针
            int i = left+1; // 移动指针

            while(i < right){
                int sum = nums[left] + nums[i] + nums[right];
                if(sum < 0){
                    i++;
                    continue;
                }
                if(sum>0){
                    right--;
                    continue;
                }
                if(sum == 0){
                    List<Integer> tmpList = new ArrayList<>();
                    tmpList.add(nums[left]);
                    tmpList.add(nums[i]);
                    tmpList.add(nums[right]);
                    list.add(tmpList);
                    while(i<right && nums[i]==nums[i+1]){
                        i++;
                    }
                    while(i<right && nums[right] == nums[right-1]){
                        right--;
                    }
                    i++;
                    right--;
                }
            }
        }
        return list;
    }
}

7.4

541、反转字符串II

class Solution {
    public String reverseStr(String s, int k) {
        char[] ch = s.toCharArray();
        for (int i = 0; i < ch.length; i += 2 * k) {
            if(i+k<=ch.length){
                reverse(ch, i, i+k-1);
                continue;
            }
            reverse(ch, i, ch.length-1);
        
        }
        return new String(ch);
    }

    public void reverse(char[] ch, int start, int end) {
        while(start < end){
            char tmp = ch[start];
            ch[start] = ch[end];
            ch[end] = tmp;
            start++;
            end--;
        }
    }
}

7.5

18、四数之和

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);

        int left1 = 0;
        int left2 = 1;
        for (; left1 < nums.length - 1; left1++) {
            if (nums[left1] > 0 && nums[left1] > target) {
                return result;
            }
            if (left1 > 0 && nums[left1 - 1] == nums[left1]) {
                continue;
            }
            for (left2 = left1 + 1; left2 < nums.length; left2++) {
                if (left2 > left1 + 1 && nums[left2 - 1] == nums[left2]) {
                    continue;
                }
                int i = left2 + 1;
                int right = nums.length - 1;
                while (i < right) {
                    int sum = nums[left1] + nums[left2] + nums[i] + nums[right];
                    if (sum < target) {
                        i++;
                        continue;
                    }
                    if (sum > target) {
                        right--;
                        continue;
                    }
                    if (sum == target) {
                        List<Integer> list = new ArrayList<>();
                        list.add(nums[left1]);
                        list.add(nums[left2]);
                        list.add(nums[i]);
                        list.add(nums[right]);
                        result.add(list);
                        while (i < right && nums[i + 1] == nums[i])
                            i++;
                        while (i < right && nums[right - 1] == nums[right])
                            right--;
                        i++;
                        right--;
                    }
                }
            }
        }
        return result;
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值