This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public String reverseWords(String s) | |
| { | |
| String[] words = s.split(" "); | |
| StringBuilder result = new StringBuilder(words.length); | |
| for (int i = words.length - 1; i >= 0; i--) | |
| { | |
| if (words[i].length() != 0) | |
| { | |
| if (result.length() != 0) | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public ListNode mergeKLists(ListNode[] lists) | |
| { | |
| if (lists == null || lists.length == 0) | |
| { | |
| return null; | |
| } | |
| ListNode dummy = new ListNode(0); | |
| ListNode prev = dummy; | |
| ListNode current; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public int[][] merge(int[][] intervals) | |
| { | |
| Arrays.sort(intervals, (a, b) -> | |
| { | |
| if (a[1] == b[1]) | |
| { | |
| return a[0] - b[0]; | |
| } | |
| return a[1] - b[1]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Node | |
| { | |
| int val; | |
| Node next; | |
| Node random; | |
| public Node(int val) | |
| { | |
| this.val = val; | |
| this.next = null; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Map<Node, Node> map = new HashMap<>(); | |
| Node temp = head; | |
| while (temp != null) | |
| { | |
| map.put(temp, new Node(temp.val)); | |
| temp = temp.next; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public Node copyRandomList(Node head) | |
| { | |
| Map<Node, Node> map = new HashMap<>(); | |
| Node temp = head; | |
| while (temp != null) | |
| { | |
| map.put(temp, new Node(temp.val)); | |
| temp = temp.next; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| List<Integer> result = new ArrayList<>(); | |
| if (root == null) | |
| { | |
| return result; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Map<Integer, Boolean> visited = new HashMap<>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private void rightSideView(TreeNode root, List<Integer> result, Map<Integer, Boolean> visited, int currentLevel) | |
| { | |
| if (root == null) | |
| { | |
| return; | |
| } | |
| if (!visited.getOrDefault(currentLevel, false)) | |
| { | |
| visited.put(currentLevel, true); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public List<Integer> rightSideView(TreeNode root) | |
| { | |
| List<Integer> result = new ArrayList<>(); | |
| if (root == null) | |
| { | |
| return result; | |
| } | |
| Map<Integer, Boolean> visited = new HashMap<>(); | |
| rightSideView(root, result, visited, 1); |
OlderNewer