Java算法 leetcode简单刷题记录11

Java算法 leetcode简单刷题记录11

  1. 删除排序链表中的重复元素: https://leetcode.cn/problems/remove-duplicates-from-sorted-list/

  2. 合并俩个有序数组: https://leetcode.cn/problems/merge-sorted-array/

    int[]数组转 List: Arrays.stream(arr).collect(Collectors.toList());
    list排序 Collections.sort(list);

import java.util.Arrays;
import java.util.stream.Collectors;

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        List<Integer> list = new ArrayList<>();
        list.addAll(Arrays.stream(nums2).boxed().collect(Collectors.toList()));
        list.addAll(Arrays.stream(nums1).boxed().collect(Collectors.toList()).subList(0, m));
        Collections.sort(list);
        for (int i = 0; i < list.size(); i++) {
            nums1[i] = list.get(i);
        }
    }
}
  1. 对称二叉树: https://leetcode.cn/problems/symmetric-tree/

给定根节点,依次遍历当前根节点左右节点值是否相同,然后左子树的左节点与右子树的右节点是否相同;左子树的右节点与右子树的子节点是否相同;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        if (root.left == null && root.right == null) {
            return true;
        } else if (root.left == null || root.right == null) {
            return false;
        }
        return isSymmetric2(root.left, root.right);
    }

    public boolean isSymmetric2(TreeNode nodeA, TreeNode nodeB) {
        if (nodeA == null && nodeB == null) {
            return true;
        } else if (nodeA == null || nodeB == null) {
            return false;
        }
        if (nodeA.val != nodeB.val) {
            return false;
        }
        if (isSymmetric2(nodeA.left, nodeB.right)) {
            return isSymmetric2(nodeA.right, nodeB.left);
        } else {
            return false;
        }
    }
}
  1. 验证回文串: https://leetcode.cn/problems/valid-palindrome/

  2. 环形链表: https://leetcode.cn/problems/linked-list-cycle/
    法一: 定义一个hashSet,把走过的每个node记录下,下次在经过就认为有环;
    法二: 定义兔子一下走俩步,乌龟一下走一步,看是否乌龟能追到兔子,能追到则认为有环,没追到则认为没有;
    只是判断值,不太对,也有可能不同node有相同的val;

/**
 * Definition for singly-linked list.
 * class ListNode {
 * int val;
 * ListNode next;
 * ListNode(int x) {
 * val = x;
 * next = null;
 * }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        //List list = new ArrayList<>();
        //return calcCycle(head, list);

        Set<ListNode> set = new HashSet<>();
        while(head != null){
            if(set.contains(head)){
                return true;
            }
            set.add(head);
            head = head.next;
        }
        return false;
    }

    public boolean calcCycle(ListNode head, List<Integer> list) {
        if (head == null) {
            return false;
        }
        if (list != null && list.contains(head.val)) {
            return true;
        }
        list.add(head.val);
        return calcCycle(head.next, list);
    }

}

你可能感兴趣的:(JAVA,算法,算法,java,leetcode)