Skip to content

Commit 6c35321

Browse files
committed
Merge branch 'master' of https://github.com/doocs/leetcode
2 parents 801ec12 + 4916f9d commit 6c35321

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public int uniquePathsWithObstacles(int[][] dp) {
3+
if (dp[0][0] == 1 || dp[dp.length - 1][dp[0].length - 1] == 1) return 0;
4+
for (int i = 1; i < dp.length; i ++) {
5+
if (dp[i][0] == 1) {
6+
dp[i][0] = -1;
7+
continue;
8+
}
9+
if (dp[i - 1][0] == -1) {
10+
dp[i][0] = -1;
11+
} else dp[i][0] = 1;
12+
}
13+
for (int i = 1; i < dp[0].length; i ++) {
14+
if (dp[0][i] == 1) {
15+
dp[0][i] = -1;
16+
continue;
17+
}
18+
if (dp[0][i - 1] == -1) {
19+
dp[0][i] = -1;
20+
} else dp[0][i] = 1;
21+
}
22+
for (int i = 1; i < dp.length; i ++) {
23+
for (int j = 1; j < dp[0].length; j ++) {
24+
if (dp[i][j] == 1) {
25+
dp[i][j] = -1;
26+
} else if (dp[i][j - 1] == -1 && dp[i - 1][j] == -1) {
27+
dp[i][j] = -1;
28+
} else if (dp[i][j - 1] == -1) {
29+
dp[i][j] = dp[i - 1][j];
30+
} else if (dp[i - 1][j] == -1) {
31+
dp[i][j] = dp[i][j - 1];
32+
} else {
33+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
34+
}
35+
}
36+
}
37+
int res = dp[dp.length - 1][dp[0].length - 1];
38+
if (res == 0) return 1;
39+
if (res == -1) return 0;
40+
return res;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Check If N and Its Double Exist
2+
3+
Given an array arr of integers, check if there exists two integers N and M such that N is the double of M ( i.e. N = 2 * M).
4+
5+
More formally check if there exists two indices i and j such that :
6+
* i != j
7+
* 0 <= i, j < arr.length
8+
* arr[i] == 2 * arr[j]
9+
10+
## Example 1:
11+
```
12+
Input: arr = [10,2,5,3]
13+
Output: true
14+
Explanation: N = 10 is the double of M = 5,that is, 10 = 2 * 5.
15+
16+
```
17+
18+
## Example 2:
19+
```
20+
Input: arr = [7,1,14,11]
21+
Output: true
22+
Explanation: N = 14 is the double of M = 7,that is, 14 = 2 * 7.
23+
24+
```
25+
26+
## Example 3:
27+
```
28+
Input: arr = [3,1,7,11]
29+
Output: false
30+
Explanation: In this case does not exist N and M, such that N = 2 * M.
31+
32+
```
33+
34+
35+
## Constraints:
36+
* 2 <= arr.length <= 500
37+
* -10^3 <= arr[i] <= 10^3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public boolean checkIfExist(int[] arr) {
5+
Map<Integer, Integer> map = new HashMap<>();
6+
for (int i = 0; i < arr.length; i++) {
7+
map.put(arr[i], i);
8+
}
9+
for (int i = 0; i < arr.length; i++) {
10+
if (map.containsKey(arr[i] * 2) && i != map.get(arr[i] * 2))
11+
return true;
12+
}
13+
return false;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Minimum Number of Steps to Make Two Strings Anagram
2+
3+
Given two equal-size strings s and t. In one step you can choose any character of t and replace it with another character.
4+
5+
Return the minimum number of steps to make t an anagram of s.
6+
7+
An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.
8+
9+
## Example 1:
10+
```
11+
Input: s = "bab", t = "aba"
12+
Output: 1
13+
Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s.
14+
15+
```
16+
17+
## Example 2:
18+
```
19+
Input: s = "leetcode", t = "practice"
20+
Output: 5
21+
Explanation: Replace 'p', 'r', 'a', 'i' and 'c' from t with proper characters to make t anagram of s.
22+
23+
```
24+
25+
## Example 3:
26+
```
27+
Input: s = "anagram", t = "mangaar"
28+
Output: 0
29+
Explanation: "anagram" and "mangaar" are anagrams.
30+
31+
```
32+
33+
34+
## Constraints:
35+
* 1 <= s.length <= 50000
36+
* s.length == t.length
37+
* s and t contain lower-case English letters only.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int minSteps(String s, String t) {
5+
Map<Character, Integer> map = new HashMap<>();
6+
for (char c : t.toCharArray()) {
7+
if (map.containsKey(c)) {
8+
map.put(c, map.get(c) + 1);
9+
} else map.put(c, 1);
10+
}
11+
int res = 0;
12+
for (char c : s.toCharArray()) {
13+
if (map.containsKey(c) && map.get(c) > 0) {
14+
map.put(c, map.get(c) - 1);
15+
} else res ++;
16+
}
17+
return res;
18+
}
19+
}

0 commit comments

Comments
 (0)