Skip to content

Commit 2c197c6

Browse files
committed
feat: solve No.389,905
1 parent 51603ec commit 2c197c6

File tree

2 files changed

+175
-0
lines changed

2 files changed

+175
-0
lines changed

301-400/389. Find the Difference.md

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# 389. Find the Difference
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Hash Table, String, Bit Manipulation, Sorting.
5+
- Similar Questions: Single Number.
6+
7+
## Problem
8+
9+
You are given two strings `s` and `t`.
10+
11+
String `t` is generated by random shuffling string `s` and then add one more letter at a random position.
12+
13+
Return the letter that was added to `t`.
14+
15+
 
16+
Example 1:
17+
18+
```
19+
Input: s = "abcd", t = "abcde"
20+
Output: "e"
21+
Explanation: 'e' is the letter that was added.
22+
```
23+
24+
Example 2:
25+
26+
```
27+
Input: s = "", t = "y"
28+
Output: "y"
29+
```
30+
31+
 
32+
**Constraints:**
33+
34+
35+
36+
- `0 <= s.length <= 1000`
37+
38+
- `t.length == s.length + 1`
39+
40+
- `s` and `t` consist of lowercase English letters.
41+
42+
43+
44+
## Solution 1
45+
46+
```javascript
47+
/**
48+
* @param {string} s
49+
* @param {string} t
50+
* @return {character}
51+
*/
52+
var findTheDifference = function(s, t) {
53+
var num = 0;
54+
for (var i = 0; i < t.length; i++) {
55+
num += t[i].charCodeAt(0);
56+
}
57+
for (var j = 0; j < s.length; j++) {
58+
num -= s[j].charCodeAt(0);
59+
}
60+
return String.fromCharCode(num);
61+
};
62+
```
63+
64+
**Explain:**
65+
66+
nope.
67+
68+
**Complexity:**
69+
70+
* Time complexity : O(n).
71+
* Space complexity : O(1).
72+
73+
## Solution 2
74+
75+
```javascript
76+
/**
77+
* @param {string} s
78+
* @param {string} t
79+
* @return {character}
80+
*/
81+
var findTheDifference = function(s, t) {
82+
var num = 0;
83+
for (var i = 0; i < t.length; i++) {
84+
num ^= t[i].charCodeAt(0);
85+
}
86+
for (var j = 0; j < s.length; j++) {
87+
num ^= s[j].charCodeAt(0);
88+
}
89+
return String.fromCharCode(num);
90+
};
91+
```
92+
93+
**Explain:**
94+
95+
nope.
96+
97+
**Complexity:**
98+
99+
* Time complexity : O(n).
100+
* Space complexity : O(1).

901-1000/905. Sort Array By Parity.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# 905. Sort Array By Parity
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Array, Two Pointers, Sorting.
5+
- Similar Questions: Sort Even and Odd Indices Independently, Largest Number After Digit Swaps by Parity.
6+
7+
## Problem
8+
9+
Given an integer array `nums`, move all the even integers at the beginning of the array followed by all the odd integers.
10+
11+
Return ****any array** that satisfies this condition**.
12+
13+
 
14+
Example 1:
15+
16+
```
17+
Input: nums = [3,1,2,4]
18+
Output: [2,4,3,1]
19+
Explanation: The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
20+
```
21+
22+
Example 2:
23+
24+
```
25+
Input: nums = [0]
26+
Output: [0]
27+
```
28+
29+
 
30+
**Constraints:**
31+
32+
33+
34+
- `1 <= nums.length <= 5000`
35+
36+
- `0 <= nums[i] <= 5000`
37+
38+
39+
40+
## Solution
41+
42+
```javascript
43+
/**
44+
* @param {number[]} nums
45+
* @return {number[]}
46+
*/
47+
var sortArrayByParity = function(nums) {
48+
var right = nums.length - 1;
49+
var i = 0;
50+
while (i < right) {
51+
if (nums[i] % 2) {
52+
swap(nums, i, right);
53+
right--;
54+
} else {
55+
i++;
56+
}
57+
}
58+
return nums;
59+
};
60+
61+
var swap = function(nums, i, j) {
62+
var tmp = nums[i];
63+
nums[i] = nums[j];
64+
nums[j] = tmp;
65+
};
66+
```
67+
68+
**Explain:**
69+
70+
nope.
71+
72+
**Complexity:**
73+
74+
* Time complexity : O(n).
75+
* Space complexity : O(1).

0 commit comments

Comments
 (0)