Giter VIP home page Giter VIP logo

dailyalgorithms's People

Contributors

zhousilverbullet avatar

Watchers

 avatar  avatar

dailyalgorithms's Issues

977. 有序数组的平方

方法一:插入排序

class Solution {
    public int[] sortedSquares(int[] A) {
        int len = A.length;
        int[] ans = new int[len];
        for (int i = 0; i < len; i++) {
            int value = A[i] * A[i];
            int j = i;
            int temp = value;
            while (j - 1 >= 0 && ans[j - 1] > value) {
                ans[j] = ans[j-1]; 
                j--; 
            }
            ans[j] = temp;
        }
        return ans;
    }
}

142. 环形链表 II

public class Solution {
    public ListNode detectCycle(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow && fast != null) {
                ListNode ptr = head;
                while (ptr != slow) {
                    ptr = ptr.next;
                    slow = slow.next;
                }
                return ptr;
            }
        }
        return null;
    }
}

116. 填充每个节点的下一个右侧节点指针

方法一:通过借助一个queue来进行层次遍历

class Solution {
    public Node connect(Node root) {
        if (root == null) {
            return root;
        }
        LinkedList<Node> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            if (size == 1) {
                queue.get(0).next = null;
            } else {
                for (int i = 0; i < size; i++) {
                    if (i == size - 1) {
                        queue.get(i).next = null;
                    } else {
                        queue.get(i).next = queue.get(i + 1);     
                    }      
                }
            }
            while(size > 0) {
                Node node = queue.removeFirst();
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
                size--;
            }
        }
        return root;
    }
}

142. 环形链表 II

public class Solution {
    public ListNode detectCycle(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow && fast != null) {
                ListNode ptr = head;
                while (ptr != slow) {
                    ptr = ptr.next;
                    slow = slow.next;
                }
                return ptr;
            }
        }
        return null;
    }
}

530. 二叉搜索树的最小绝对差

方法一

时间复杂度 o(n) 空间复杂度o(n + 递归的n)

class Solution {
    int maxValue = 0;

    public int getMinimumDifference(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        tranvsal(root, list);
        int min = Integer.MAX_VALUE;
        int maxIndex = 0;
        
        for (int i = 0; i < list.size(); i++) {
            int value = list.get(i);
            for (int j = 0; j < list.size(); j++) {
                if(i != j) {
                    min = Math.min(min, Math.abs(value - list.get(j)));
                }
            }
        }
        return min;
    }

    private void tranvsal(TreeNode root, List<Integer> list) {
        if (root == null) {
            return;
        }

        maxValue = Math.max(root.val, maxValue);
        
        list.add(root.val);
        if (root.left != null) {
            tranvsal(root.left, list);
        }
        if (root.right != null) {
            tranvsal(root.right, list);
        }
    }
}

方法二 中序遍历

class Solution {
    int pre = 0;
    int min = 0;

    public int getMinimumDifference(TreeNode root) {
        pre = -1;
        min = Integer.MAX_VALUE;
        tranvsal(root);
        
        return min;
    }

    private void tranvsal(TreeNode root) {
        if (root == null) {
            return;
        }
        
        
        if (root.left != null) {
            tranvsal(root.left);
        }

        if (pre == -1) {
            pre = root.val;
        } else {
            //这里不用abs,需要自己再想想为什么
            min = Math.min(min, root.val - pre);
            pre = root.val;
        }

        if (root.right != null) {
            tranvsal(root.right);
        }
    }
}

141. 环形链表

public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(slow == fast) {
                return true;
            }
        }
        return false;
    }
}

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

方法一 递归

class Solution {
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode next = head.next;
        head.next = swapPairs(next.next);
        next.next = head;
        return next;
    }
}

方法二 迭代

class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode dummyNode = new ListNode(0);
        ListNode temp = dummyNode;
        temp.next = head;
        while (temp != null && temp.next != null && temp.next.next != null) {
            ListNode node1 = temp.next;
            ListNode node2 = temp.next.next;
            temp.next = node2;
            node1.next = node2.next;
            node2.next = node1;

            temp = node1;
        }
        
        return dummyNode.next;
    }
}

117. 填充每个节点的下一个右侧节点指针 II

class Solution {
    public Node connect(Node root) {
        if(root == null) {
            return null;
        }
        LinkedList<Node> queue = new LinkedList<Node>();
        queue.push(root);
        while(!queue.isEmpty()) {
            int size = queue.size();
            while(size > 0) {
                Node node = queue.removeLast();
                if(node.left != null) {
                    queue.addFirst(node.left);
                }
                if(node.right != null) {
                    queue.addFirst(node.right);
                }
                if(size > 1) {
                    node.next = queue.peekLast();
                }
                size--;
            }
        }
        return root;
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.