Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/azl397985856/leetcode
Browse files Browse the repository at this point in the history
  • Loading branch information
robot committed Oct 17, 2022
2 parents c542c9a + 7754eb3 commit e0e438d
Show file tree
Hide file tree
Showing 20 changed files with 571 additions and 30 deletions.
121 changes: 121 additions & 0 deletions 91/binary-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,66 @@ def bisect_left(A, x):
return l
```

##### Java

```java
import java.util.*;
public class BinarySearch {
public int getPos(int[] A, int val) {
int low=0,high=A.lenght-1;
while (low <= high){
int mid = (low + high)/2;
if (A[mid] >= val) {
high = mid-1;
} else {
low = mid+1;
}
}
return low;
}
}
```

##### C++

```cpp
public:
int binarySearch(int* arr, int arrLen,int a) {
int left = 0;
int right = arrLen - 1;
while(left<=right)
{
int mid = (left+right)/2;
if(arr[mid]>=a)
right = mid - 1;
else
left = mid + 1;
}
return left;
}
```
##### JavaScript
```js
function binarySearch(nums, target) {
let left = 0;
let right = nums.length - 1;
while (left <= right) {
const mid = Math.floor(left + (right - left) / 2);
if (nums[mid] >= target) {
// 搜索区间变为 [left, mid-1]
right = mid - 1;
}
else {
// 搜索区间变为 [mid+1, right]
left = mid + 1;
}
}
return left;
}
```

其他语言暂时空缺,欢迎
[PR](https://github.com/azl397985856/leetcode-cheat/issues/4)

Expand Down Expand Up @@ -588,6 +648,67 @@ def bisect_right(A, x):
else: r = mid - 1
return l # 或者返回 r + 1
```
##### Java

```java
import java.util.*;
public class BinarySearch {
public int getPos(int[] A, int val) {
int low=0,high=A.lenght-1;
while (low <= high){
int mid = (low + high)/2;
if (A[mid] <= val) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return low;
}
}
```

##### C++

```cpp
public:
int binarySearch(int* arr, int arrLen,int a) {
int left = 0;
int right = arrLen - 1;
while(left<=right)
{
int mid = (left+right)/2;
if(arr[mid]<=a)
// 搜索区间变为 [mid+1, right]
left = mid + 1;
else
// 搜索区间变为 [left, mid-1]
right = mid - 1;
}
return left;
}
```
##### JavaScript
```js
function binarySearch(nums, target) {
let left = 0;
let right = nums.length - 1;
while (left <= right) {
const mid = Math.floor(left + (right - left) / 2);
if (nums[mid] <= target) {
// 搜索区间变为 [mid+1, right]
left = mid + 1;
}
else {
// 搜索区间变为 [left, mid-1]
right = mid - 1;
}
}
return left;
}
```

其他语言暂时空缺,欢迎
[PR](https://github.com/azl397985856/leetcode-cheat/issues/4)
Expand Down
53 changes: 52 additions & 1 deletion problems/1.two-sum.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ The easiest solution to come up with is Brute Force. We could write two for-loop

## Code

- Support Language: JS
- Support Language: JS,C++,Java,Python

Javascript Code:

```js
/**
Expand All @@ -49,6 +51,55 @@ const twoSum = function (nums, target) {
};
```

C++ Code:

```cpp
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> hashtable;
for (int i = 0; i < nums.size(); ++i) {
auto it = hashtable.find(target - nums[i]);
if (it != hashtable.end()) {
return {it->second, i};
}
hashtable[nums[i]] = i;
}
return {};
}
};
```
Java Code:
```java
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; ++i) {
if (hashtable.containsKey(target - nums[i])) {
return new int[]{hashtable.get(target - nums[i]), i};
}
hashtable.put(nums[i], i);
}
return new int[0];
}
}
```

Python Code:

```py
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashtable = dict()
for i, num in enumerate(nums):
if target - num in hashtable:
return [hashtable[target - num], i]
hashtable[nums[i]] = i
return []
```

**_Complexity Anlysis_**

- _Time Complexity_: O(N)
Expand Down
32 changes: 31 additions & 1 deletion problems/1.two-sum.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ for(int i = 0; i < n; i++) {

## 代码

- 语言支持:JS, GoCPP
- 语言支持:JS, Go,CPP,Java,Python

```js
/**
Expand Down Expand Up @@ -119,6 +119,36 @@ public:
};
```
Java Code:
```java
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; ++i) {
if (hashtable.containsKey(target - nums[i])) {
return new int[]{hashtable.get(target - nums[i]), i};
}
hashtable.put(nums[i], i);
}
return new int[0];
}
}
```

Python Code:

```py
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashtable = dict()
for i, num in enumerate(nums):
if target - num in hashtable:
return [hashtable[target - num], i]
hashtable[nums[i]] = i
return []
```

**复杂度分析**

- 时间复杂度:$O(N)$
Expand Down
14 changes: 13 additions & 1 deletion problems/122.best-time-to-buy-and-sell-stock-ii.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/description/

## 代码

语言支持:JS,C++,Java
语言支持:JS,C++,Java,Python

JS Code:

Expand Down Expand Up @@ -129,6 +129,18 @@ class Solution {
}
```

Python Code:

```py
class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit = 0
for i in range(1, len(prices)):
tmp = prices[i] - prices[i - 1]
if tmp > 0: profit += tmp
return profit
```

**复杂度分析**

- 时间复杂度:$O(N)$
Expand Down
29 changes: 28 additions & 1 deletion problems/125.valid-palindrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ https://leetcode-cn.com/problems/valid-palindrome/description/

## 代码

- 语言支持:JS,C++,Python
- 语言支持:JS,C++,Python,Java

JavaScript Code:

Expand Down Expand Up @@ -160,6 +160,33 @@ class Solution:
return s == s[::-1]
```

Java Code:

```java
class Solution {
public boolean isPalindrome(String s) {
int n = s.length();
int left = 0, right = n - 1;
while (left < right) {
while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
++left;
}
while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
--right;
}
if (left < right) {
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
return false;
}
++left;
--right;
}
}
return true;
}
}
```

**复杂度分析**

- 时间复杂度:$O(N)$
Expand Down
22 changes: 21 additions & 1 deletion problems/169.majority-element.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ https://leetcode-cn.com/problems/majority-element/

## 代码

- 语言支持:JS,Python, CPP
- 语言支持:JS,Python, CPP,Java

Javascript Code:

Expand Down Expand Up @@ -112,6 +112,26 @@ public:
};
```
Java Code:
```java
class Solution {
public int majorityElement(int[] nums) {
int count = 0;
Integer candidate = null;
for (int num : nums) {
if (count == 0) {
candidate = num;
}
count += (num == candidate) ? 1 : -1;
}
return candidate;
}
}
```

**复杂度分析**

- 时间复杂度:$O(N)$,其中 N 为数组长度
Expand Down
17 changes: 16 additions & 1 deletion problems/190.reverse-bits.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ eg :

## 代码

- 语言支持:JS,C++,Python
- 语言支持:JS,C++,Python,Java

JavaScript Code:

Expand Down Expand Up @@ -132,6 +132,21 @@ class Solution:
return ans
```

Java Code:

```java
public class Solution {
public int reverseBits(int n) {
int rev = 0;
for (int i = 0; i < 32 && n != 0; ++i) {
rev |= (n & 1) << (31 - i);
n >>>= 1;
}
return rev;
}
}
```

**复杂度分析**

- 时间复杂度:$O(logN)$
Expand Down
Loading

0 comments on commit e0e438d

Please sign in to comment.