AWS CV From Medium

Partition Linked List 

public class ListNode {
    int val;
    ListNode next;

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

public class Solution {
    public ListNode partition(ListNode head, int x) {
        // Two pointers for tracking before and after partitions
        ListNode beforeHead = null, beforeTail = null, afterHead = null, afterTail = null;

        for (ListNode curr = head; curr != null; curr = curr.next) {
            if (curr.val < x) {
                // Add to the "before" partition
                if (beforeHead == null) {
                    beforeHead = curr;
                    beforeTail = curr;
                } else {
                    beforeTail.next = curr;
                    beforeTail = curr;
                }
            } else {
                // Add to the "after" partition
                if (afterHead == null) {
                    afterHead = curr;
                    afterTail = curr;
                } else {
                    afterTail.next = curr;
                    afterTail = curr;
                }
            }
        }

        // Combine partitions (handle empty partitions gracefully)
        if (beforeHead == null) {
            return afterHead;
        }
        if (afterHead == null) {
            return beforeHead;
        }

        // Connect the partitions correctly
        beforeTail.next = afterHead;
        return beforeHead;
    }
}

LRU Cache

public class LRUCache {

    private final int capacity;
    private final Map map;
    private final Node head;
    private final Node tail;

    public LRUCache(int capacity) {
        this.capacity = capacity;
        map = new HashMap<>();
        head = new Node(Integer.MIN_VALUE, Integer.MIN_VALUE);
        tail = new Node(Integer.MAX_VALUE, Integer.MAX_VALUE);
        head.next = tail;
        tail.prev = head;
    }

    public int get(int key) {
        Node node = map.get(key);
        if (node == null) {
            return -1;
        }
        moveToHead(node);
        return node.value;
    }

    public void put(int key, int value) {
        Node node = map.get(key);
        if (node != null) {
            node.value = value;
            moveToHead(node);
        } else {
            if (map.size() == capacity) {
                removeTail();
            }
            Node newNode = new Node(key, value);
            addNode(newNode);
            map.put(key, newNode);
        }
    }

    private void addNode(Node node) {
        node.next = head.next;
        head.next.prev = node;
        head.next = node;
        node.prev = head;
    }

    private void removeTail() {
        Node removed = tail.prev;
        map.remove(removed.key);
        removed.prev.next = tail;
        tail.prev = removed.prev;
    }

    private void moveToHead(Node node) {
        node.prev.next = node.next;
        node.next.prev = node.prev;
        addNode(node);
    }

    private static class Node {
        final int key;
        int value;
        Node prev;
        Node next;

        public Node(int key, int value) {
            this.key = key;
            this.value = value;
            this.prev = null;
            this.next = null;
        }
    }
}

Minimum-Window-Substring

public class Solution {
    public String minWindow(String s, String t) {
        // Create frequency maps for characters in t and s
        int[] tFreq = new int[128];
        int[] sFreq = new int[128];
        for (char c : t.toCharArray()) {
            tFreq[c]++;
        }

        // Initialize count of matching characters and minimum window length
        int count = 0;
        int minWindowLength = Integer.MAX_VALUE;
        String minWindow = "";

        // Sliding window pointers
        int left = 0, right = 0;

        // Slide the window until all characters in t are found
        while (right < s.length()) {
            char c = s.charAt(right);
            sFreq[c]++;

            // Check if current character matches a character in t
            if (tFreq[c] > 0) {
                count++;
            }

            // Shrink the window as long as all characters in t are found
            while (count == t.length()) {
                char leftChar = s.charAt(left);

                // Update minimum window if window length is smaller
                if (right - left + 1 < minWindowLength) {
                    minWindow = s.substring(left, right + 1);
                    minWindowLength = right - left + 1;
                }

                // Decrement counter and frequency for left character
                sFreq[leftChar]--;
                if (tFreq[leftChar] > 0) {
                    count--;
                }

                left++;
            }

            right++;
        }

        return minWindow;
    }
}

Construct Binary Tree from Preorder and Inorder Traversal

AWS CV From Medium_第1张图片

public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

public class Solution {
    
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if (preorder.length == 0) {
            return null;
        }
        int rootVal = preorder[0];
        TreeNode root = new TreeNode(rootVal);

        int rootIndex = findRootIndexInInorder(inorder, rootVal);

        int[] leftPreorder = Arrays.copyOfRange(preorder, 1, 1 + rootIndex);
        int[] leftInorder = Arrays.copyOfRange(inorder, 0, rootIndex);

        int[] rightPreorder = Arrays.copyOfRange(preorder, 1 + rootIndex, preorder.length);
        int[] rightInorder = Arrays.copyOfRange(inorder, rootIndex + 1, inorder.length);

        root.left = buildTree(leftPreorder, leftInorder);
        root.right = buildTree(rightPreorder, rightInorder);

        return root;
    }

    private int findRootIndexInInorder(int[] inorder, int rootVal) {
        for (int i = 0; i < inorder.length; i++) {
            if (inorder[i] == rootVal) {
                return i;
            }
        }
        return -1;
    }
}
int[] preorder = {3, 9, 20, 15, 7};
int[] inorder = {9, 3, 15, 20, 7};

Solution solution = new Solution();
TreeNode root = solution.buildTree(preorder, inorder);

System.out.println(root.val); // 3
System.out.println(root.left.val); // 9
System.out.println(root.right.val); // 20
System.out.println(root.left.left); // null
System.out.println(root.left.right); // null
System.out.println(root.right.left.val); // 15
System.out.println(root.right.right.val); // 7

See

https://gemini.google.comLeetCode - The World's Leading Online Programming Learning Platform

你可能感兴趣的:(How,to,Solve,New,Developer,Data,Structure,DSA)