diff --git a/[1]. Math Implementation/1.1 Fibonacci Implementation/README.md b/[1]. Math Implementation/1.1 Fibonacci Implementation/README.md index ab99cfd..1f2ec81 100644 --- a/[1]. Math Implementation/1.1 Fibonacci Implementation/README.md +++ b/[1]. Math Implementation/1.1 Fibonacci Implementation/README.md @@ -46,3 +46,37 @@ long long Fibonacci(unsigned n) } ``` +### Java + +```java +// Iteration +long Fibonacci(long n) { + + if(n < 2){ + return n; + } + + long fibNMinusOne = 1; + long fibNMinusTwo = 0; + + long fibN = 0; + + for(int i = 2; i <= n; ++i){ + + fibN = fibNMinusOne + fibNMinusTwo; + + fibNMinusTwo = fibNMinusOne; + fibNMinusOne = fibN; + } + + return fibN; +} +``` + +```java +// Recursive +long Fibonacci(long n) { + if (n < 2) return n; + return Fibonacci(n - 1) + Fibonacci(n - 2); +} +``` \ No newline at end of file diff --git a/[1]. Math Implementation/1.5 Is Power Of Two/README.md b/[1]. Math Implementation/1.5 Is Power Of Two/README.md index 864cb16..90954da 100644 --- a/[1]. Math Implementation/1.5 Is Power Of Two/README.md +++ b/[1]. Math Implementation/1.5 Is Power Of Two/README.md @@ -39,3 +39,14 @@ public: }; ``` +### Java + +```java +// time O(1) space O(1) +// 解释:2的N次方用二进制可表示为:10,100,1000...,首先他们是大于等于1的, +// 其次,n-1为01,011,0111,因此,n & (n - 1) == 0. +boolean isPowerOfTwo(int n) { + return (n >= 1) && (n & (n - 1)) == 0; +} +``` + diff --git a/[2]. Algorithm Implementation/2.4 Quick Sort Implementation/README.md b/[2]. Algorithm Implementation/2.4 Quick Sort Implementation/README.md index 6eb00f4..0a2de57 100644 --- a/[2]. Algorithm Implementation/2.4 Quick Sort Implementation/README.md +++ b/[2]. Algorithm Implementation/2.4 Quick Sort Implementation/README.md @@ -126,5 +126,35 @@ int __partition2Way(int arr[], int l , int r){ } ``` +### Java + +```java +int parttion(int[] nums) { + if (nums == null || nums.length == 0) return -1; + int len = nums.length; + // 避免有序情况下,时间复杂度最坏 O(N^2) + int idx = (int)(100 * Math.random() % nums.length); + swap(nums, len - 1, idx); + int pivot = nums[len - 1]; + + idx = 0; + for (int i = 0; i < len - 1; ++i) { + if (nums[i] <= pivot) { + swap(nums, i, idx ++); + } + } + swap(nums, idx, len - 1); + return idx; +} + +void swap(int[] nums, int i, int j) { + if (nums[i] == nums[j]) return; + // 位操作比加减速度更快,减少临时变量的开销 + nums[i] ^= nums[j]; + nums[j] ^= nums[i]; + nums[i] ^= nums[j]; +} +``` + diff --git a/[3]. Linked List/3.10 Remove Duplicates II/README.md b/[3]. Linked List/3.10 Remove Duplicates II/README.md index 6d1f044..e9eae8c 100644 --- a/[3]. Linked List/3.10 Remove Duplicates II/README.md +++ b/[3]. Linked List/3.10 Remove Duplicates II/README.md @@ -38,7 +38,7 @@ struct ListNode { ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; - +// #warning 这个解法可能不太对,题干的第二个例子都 ac 不过 ListNode* deleteDuplicates(ListNode* head){ if (head == NULL || head->next == NULL){ @@ -63,3 +63,45 @@ ListNode* deleteDuplicates(ListNode* head){ } ``` +### Java + +```java +ListNode deleteDuplicates(ListNode head) { + head = deleteHead(head); + + if (head == null || head.next == null) return head; + + ListNode temp = head.next, pre = head; + + while (temp != null && temp.next != null) { + if (temp.next.val != temp.val) { + temp = temp.next; + pre = pre.next; + } else { + pre.next = deleteHead(temp); + temp = pre.next; + } + } + + return head; +} + +// 删除所有出现超过两次的结点 +ListNode deleteHead(ListNode head) { + if (head == null || head.next == null) return head; + + int cnt = 0; + while (head.next != null && head.next.val == head.val) { + head = head.next; + cnt ++; + } + + if (cnt != 0) { + head = head.next; + return deleteHead(head); + } else { + return head; + } +} +``` + diff --git a/[4]. Array/4.1 Contains Duplicate I/README.md b/[4]. Array/4.1 Contains Duplicate I/README.md index ae83d30..fee125d 100644 --- a/[4]. Array/4.1 Contains Duplicate I/README.md +++ b/[4]. Array/4.1 Contains Duplicate I/README.md @@ -87,3 +87,19 @@ public: }; ``` + + +### Java + +```java +boolean containsDuplicate(int[] nums) { + if (nums == null || nums.length < 2) return false; + + Set set = new HashSet<>(); + for (int i: nums) { + if (!set.add(i)) return true; + } + return false; +} +``` + diff --git a/[4]. Array/4.10 Intersection of Two Arrays/README.md b/[4]. Array/4.10 Intersection of Two Arrays/README.md index db2e2f2..36ee813 100644 --- a/[4]. Array/4.10 Intersection of Two Arrays/README.md +++ b/[4]. Array/4.10 Intersection of Two Arrays/README.md @@ -50,3 +50,33 @@ vector intersection(vector& nums1, vector& nums2) { } ``` + + +### Java + +```java +int[] intersection(int[] A, int[] B) { + + Set set = new HashSet<>(); + Set res = new HashSet<>(); + + for (int i: A) { + set.add(i); + } + + for (int i: B) { + if (!set.add(i)) { + res.add(i); + } + } + + int[] ret = new int[res.size()]; + int i = 0; + for (int num: res) { + ret[i++] = num; + } + + return ret; +} +``` + diff --git a/[4]. Array/4.14 Two Sum I/README.md b/[4]. Array/4.14 Two Sum I/README.md index 3a60c7f..c77a214 100644 --- a/[4]. Array/4.14 Two Sum I/README.md +++ b/[4]. Array/4.14 Two Sum I/README.md @@ -54,3 +54,20 @@ vector twoSum(vector& nums, int target) { } ``` +### Java + +```java +int[] twoSum(int[] nums, int target) { + if (nums == null || nums.length < 2) return null; + + Map map = new HashMap<>(); + for (int i = 0; i < nums.length; ++i) { + map.put(nums[i], i); + if (map.keySet().contains(target - nums[i])) { + return new int[]{i, map.get(nums[i])}; + } + } + return null; +} +``` + diff --git a/[4]. Array/4.8 Two Sum II/README.md b/[4]. Array/4.8 Two Sum II/README.md index 26621c1..cbd82e8 100644 --- a/[4]. Array/4.8 Two Sum II/README.md +++ b/[4]. Array/4.8 Two Sum II/README.md @@ -66,3 +66,46 @@ vector twoSumII(vector& numbers, int target) { } ``` + + +### Java + +```java +int[] twoSumII(int[] nums, int target) { + if (nums == null || nums.length < 2) return null; + + // If nums is not sorted, you can sort it, time complexity is O(N * lgN) + Arrays.sort(nums); + // If nums is sorted, time complexity is O(lg N) + for (int i = 0; i < nums.length; ++i) { + int idx = Arrays.binarySearch(nums, target - nums[i]); + if (idx >= 0 && idx != i) return new int[]{i, idx}; + } + return null; +} +``` + + + +```java +/* If nums is not sorted, you can use map, time complexity will be +* O(N), but space complexity will be O(N), use O(N) space exchange +* O(lgN) time +* If nums is sorted, use binary search!!! +*/ +int[] twoSumII(int[] nums, int target) { + if (nums == null || nums.length < 2) return null; + + Map map = new HashMap<>(); + for (int i = 0; i < nums.length; ++i) { + map.put(nums[i], i); + if (map.keySet().contains(target - nums[i])) { + return new int[]{i, map.get(nums[i])}; + } + } + return null; +} +``` + + + diff --git a/[5]. Tree/5.1 Maximum Depth Of Binary Tree/README.md b/[5]. Tree/5.1 Maximum Depth Of Binary Tree/README.md index d410a66..556996c 100644 --- a/[5]. Tree/5.1 Maximum Depth Of Binary Tree/README.md +++ b/[5]. Tree/5.1 Maximum Depth Of Binary Tree/README.md @@ -60,3 +60,12 @@ public: }; ``` +### Java + +```java +int maxDepth(TreeNode root) { + if (root == null) return 0; + return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); +} +``` +