摘要:
345. Reverse Vowels of a String 头尾指针开始扫描。 String.contains(char) class Solution { public String reverseVowels(String s) { if(s == null || s.length() == 阅读全文
摘要:
41. First Missing Positive 思路是把1放在数组第一个位置 nums[0],2放在第二个位置 nums[1],即需要把 nums[i] 放在 nums[nums[i] - 1]上,遍历整个数组,如果 nums[i] != i + 1, 而 nums[i] 为整数且不大于n,另 阅读全文
摘要:
165. Compare Version Numbers class Solution { public int compareVersion(String version1, String version2) { String[] levels1 = version1.split("\\."); 阅读全文
摘要:
208. Implement Trie (Prefix Tree) class TrieNode{ private char val; public boolean isWord; public TrieNode[] children = new TrieNode[26]; public TrieN 阅读全文
摘要:
130. Surrounded Regions BFS: 把所有边界上的O先换成A(标记),再深度遍历周围的点。 最后把O换成X(表示不符合要求),所有的A换回O class Solution { public void solve(char[][] board) { if(board.length 阅读全文
摘要:
382. Linked List Random Node class Solution { ListNode node; Random random; /** @param head The linked list's head. Note that the head is guaranteed t 阅读全文
摘要:
384. Shuffle an Array random.nextInt(n) 返回[0, n) 的随机数,故要+1; class Solution { private int[] nums; private Random random; public Solution(int[] nums) { 阅读全文
摘要:
207. Course Schedule 我们定义二维数组 graph 来表示这个有向图,一维数组 in 来表示每个顶点的入度。我们开始先根据输入来建立这个有向图,并将入度数组也初始化好。然后我们定义一个 queue 变量,将所有入度为0的点放入队列中,然后开始遍历队列,从 graph 里遍历其连接 阅读全文
摘要:
215. Kth Largest Element in an Array 快速排序法,选择一个数,比这个数大的交换到左边,比这个数小的交换到右边。 class Solution { public int findKthLargest(int[] nums, int k) { shuffle(nums 阅读全文
摘要:
155. Min Stack class MinStack { int min = Integer.MAX_VALUE; Stack<Integer> stack = new Stack<Integer>(); /** initialize your data structure here. */ 阅读全文