Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions [1]. Math Implementation/1.1 Fibonacci Implementation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
```
11 changes: 11 additions & 0 deletions [1]. Math Implementation/1.5 Is Power Of Two/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```

Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
```



44 changes: 43 additions & 1 deletion [3]. Linked List/3.10 Remove Duplicates II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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;
}
}
```

16 changes: 16 additions & 0 deletions [4]. Array/4.1 Contains Duplicate I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,19 @@ public:
};
```



### Java

```java
boolean containsDuplicate(int[] nums) {
if (nums == null || nums.length < 2) return false;

Set<Integer> set = new HashSet<>();
for (int i: nums) {
if (!set.add(i)) return true;
}
return false;
}
```

30 changes: 30 additions & 0 deletions [4]. Array/4.10 Intersection of Two Arrays/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,33 @@ vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
}
```



### Java

```java
int[] intersection(int[] A, int[] B) {

Set<Integer> set = new HashSet<>();
Set<Integer> 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;
}
```

17 changes: 17 additions & 0 deletions [4]. Array/4.14 Two Sum I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,20 @@ vector<int> twoSum(vector<int>& nums, int target) {
}
```

### Java

```java
int[] twoSum(int[] nums, int target) {
if (nums == null || nums.length < 2) return null;

Map<Integer, Integer> 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;
}
```

43 changes: 43 additions & 0 deletions [4]. Array/4.8 Two Sum II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,46 @@ vector<int> twoSumII(vector<int>& 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<Integer, Integer> 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;
}
```



9 changes: 9 additions & 0 deletions [5]. Tree/5.1 Maximum Depth Of Binary Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
```