Skip to content

Commit 206a5ba

Browse files
committed
[Function add]
1. Add leetcode solution.
1 parent af4fdfe commit 206a5ba

File tree

6 files changed

+371
-0
lines changed

6 files changed

+371
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## 181. Employees Earning More Than Their Managers
2+
3+
### Question
4+
The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.
5+
6+
+----+-------+--------+-----------+
7+
| Id | Name | Salary | ManagerId |
8+
+----+-------+--------+-----------+
9+
| 1 | Joe | 70000 | 3 |
10+
| 2 | Henry | 80000 | 4 |
11+
| 3 | Sam | 60000 | NULL |
12+
| 4 | Max | 90000 | NULL |
13+
+----+-------+--------+-----------+
14+
15+
Given the Employee table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.
16+
17+
+----------+
18+
| Employee |
19+
+----------+
20+
| Joe |
21+
+----------+
22+
23+
### Thinking:
24+
* Method: 左外连接,速度比where要快。
25+
26+
```SQL
27+
# Write your MySQL query statement below
28+
select
29+
a.Name as Employee
30+
from Employee a
31+
left join
32+
Employee b
33+
on
34+
a.ManagerID = b.Id
35+
where
36+
a.Salary > b.Salary
37+
```
38+
39+
* Method 2:通过查询确定判断条件,这种方法比较慢,经过两次查询,使用了两个session。
40+
41+
```Java
42+
# Write your MySQL query statement below
43+
select
44+
name as Employee
45+
from
46+
Employee a
47+
where
48+
a.Salary > (
49+
select
50+
b.Salary
51+
from
52+
Employee b
53+
where
54+
a.ManagerId = b.Id
55+
);
56+
```

leetcode/182. Duplicate Emails.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
## 181. Employees Earning More Than Their Managers
2+
3+
### Question
4+
The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.
5+
6+
+----+-------+--------+-----------+
7+
| Id | Name | Salary | ManagerId |
8+
+----+-------+--------+-----------+
9+
| 1 | Joe | 70000 | 3 |
10+
| 2 | Henry | 80000 | 4 |
11+
| 3 | Sam | 60000 | NULL |
12+
| 4 | Max | 90000 | NULL |
13+
+----+-------+--------+-----------+
14+
15+
Given the Employee table, a## 182. Duplicate Emails
16+
17+
### Question
18+
Write a SQL query to find all duplicate emails in a table named Person.
19+
20+
+----+---------+
21+
| Id | Email |
22+
+----+---------+
23+
| 1 | a@b.com |
24+
| 2 | c@d.com |
25+
| 3 | a@b.com |
26+
+----+---------+
27+
28+
For example, your query should return the following for the above table:
29+
30+
+---------+
31+
| Email |
32+
+---------+
33+
| a@b.com |
34+
+---------+
35+
36+
Note: All emails are in lowercase.
37+
38+
### Thinking:
39+
* Method
40+
41+
```SQL
42+
# Write your MySQL query statement below
43+
select
44+
Email
45+
from
46+
Person
47+
group by
48+
Email
49+
having
50+
count(*) > 1
51+
```write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.
52+
53+
+----------+
54+
| Employee |
55+
+----------+
56+
| Joe |
57+
+----------+
58+
59+
### Thinking:
60+
* Method: 左外连接,速度比where要快。
61+
62+
```SQL
63+
# Write your MySQL query statement below
64+
select
65+
a.Name as Employee
66+
from Employee a
67+
left join
68+
Employee b
69+
on
70+
a.ManagerID = b.Id
71+
where
72+
a.Salary > b.Salary
73+
```
74+
75+
* Method 2:通过查询确定判断条件,这种方法比较慢,经过两次查询,使用了两个session。
76+
77+
```Java
78+
# Write your MySQL query statement below
79+
select
80+
name as Employee
81+
from
82+
Employee a
83+
where
84+
a.Salary > (
85+
select
86+
b.Salary
87+
from
88+
Employee b
89+
where
90+
a.ManagerId = b.Id
91+
);
92+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## 183. Customers Who Never Order
2+
3+
### Question
4+
Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything.
5+
6+
Table: Customers.
7+
8+
+----+-------+
9+
| Id | Name |
10+
+----+-------+
11+
| 1 | Joe |
12+
| 2 | Henry |
13+
| 3 | Sam |
14+
| 4 | Max |
15+
+----+-------+
16+
17+
Table: Orders.
18+
19+
+----+------------+
20+
| Id | CustomerId |
21+
+----+------------+
22+
| 1 | 3 |
23+
| 2 | 1 |
24+
+----+------------+
25+
26+
Using the above tables as example, return the following:
27+
28+
+-----------+
29+
| Customers |
30+
+-----------+
31+
| Henry |
32+
| Max |
33+
+-----------+
34+
35+
### Thinking:
36+
* Method
37+
38+
```SQL
39+
# Write your MySQL query statement below
40+
select
41+
Name as Customers
42+
from
43+
Customers
44+
where
45+
Id
46+
not in(
47+
select CustomerId from Orders
48+
);
49+
```

leetcode/190. Reverse Bits.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## 190. Reverse Bits
2+
3+
### Question
4+
Reverse bits of a given 32 bits unsigned integer.
5+
6+
```
7+
Example:
8+
9+
Input: 43261596
10+
Output: 964176192
11+
Explanation: 43261596 represented in binary as 00000010100101000001111010011100,
12+
return 964176192 represented in binary as 00111001011110000010100101000000.
13+
```
14+
Follow up:
15+
If this function is called many times, how would you optimize it?
16+
17+
### Thinking:
18+
* Method 1: 首尾取出元素,并交换位置存入一个新的int型数据中。
19+
20+
```Java
21+
public class Solution {
22+
// you need treat n as an unsigned value
23+
public int reverseBits(int n) {
24+
int low = 1;
25+
int high = 1 << 31;
26+
int result = 0;
27+
for(int i = 16; i >= 1; i--){
28+
int right = n & low;
29+
int left = n & high;
30+
left >>= i * 2 - 1;
31+
result += left;
32+
right <<= i * 2 - 1;
33+
result += right;
34+
low <<= 1;
35+
right >>= 1;
36+
}
37+
return result;
38+
}
39+
}
40+
```

leetcode/191. Number of 1 Bits.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## 191. Number of 1 Bits
2+
3+
### Question
4+
Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).
5+
6+
```
7+
Example 1:
8+
9+
Input: 11
10+
Output: 3
11+
Explanation: Integer 11 has binary representation 00000000000000000000000000001011
12+
13+
Example 2:
14+
15+
Input: 128
16+
Output: 1
17+
Explanation: Integer 128 has binary representation 00000000000000000000000010000000
18+
```
19+
20+
### Thinking:
21+
* Method:
22+
23+
```Java
24+
public class Solution {
25+
// you need to treat n as an unsigned value
26+
public int hammingWeight(int n) {
27+
int count = 0;
28+
for(int i = 0; i <= 31; i++){
29+
count = ((n & 1) == 1)? count + 1: count;
30+
n >>>= 1;
31+
}
32+
return count;
33+
}
34+
}
35+
```

leetcode/200. Number of Islands.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
## 200. Number of Islands
2+
3+
### Question
4+
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
5+
6+
```
7+
Example 1:
8+
9+
Input:
10+
11110
11+
11010
12+
11000
13+
00000
14+
15+
Output: 1
16+
17+
Example 2:
18+
19+
Input:
20+
11000
21+
11000
22+
00100
23+
00011
24+
25+
Output: 3
26+
```
27+
28+
### Thinking:
29+
* Method:DFS AC
30+
31+
```Java
32+
class Solution {
33+
private int[][] dir = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
34+
public int numIslands(char[][] grid) {
35+
if(grid == null || grid.length == 0) return 0;
36+
int height = grid.length;
37+
int width = grid[0].length;
38+
boolean[][] used = new boolean[height][width];
39+
int count = 0;
40+
LinkedList<Integer> q = new LinkedList<>();
41+
for(int i = 0; i < height; i++){
42+
for(int j = 0; j < width; j++){
43+
if(grid[i][j] == '1' && !used[i][j]){
44+
count ++;
45+
bfs(grid, used, i, j);
46+
}
47+
}
48+
}
49+
return count;
50+
}
51+
private void bfs(char[][] grid, boolean[][] used, int row, int col){
52+
if(!(row >= 0 && row < grid.length && col >= 0 && col < grid[0].length && grid[row][col] == '1' && !used[row][col]))
53+
return;
54+
used[row][col] = true;
55+
for(int i = 0; i < 4; i++){
56+
bfs(grid, used, row + dir[i][0], col + dir[i][1]);
57+
}
58+
}
59+
}
60+
```
61+
62+
* Method 2: BFS TLE
63+
64+
```Java
65+
class Solution {
66+
public int numIslands(char[][] grid) {
67+
int count = 0;
68+
int[][] dir = new int[][]{{1,0}, {0,1}, {-1, 0}, {0, -1}};
69+
if(grid == null || grid.length == 0) return 0;
70+
int height = grid.length;
71+
int width = grid[0].length;
72+
boolean[][] used = new boolean[height][width];
73+
LinkedList<Integer> queue = new LinkedList<>();
74+
for(int i = 0; i < height; i++){
75+
for(int j = 0; j < width; j++){
76+
if(grid[i][j] == '0' || used[i][j]) continue;
77+
else{
78+
count++;
79+
queue.add(i * width + j);
80+
while(!queue.isEmpty()){
81+
Integer cnt = queue.poll();
82+
int row = cnt / width;
83+
int col = cnt % width;
84+
used[row][col] = true;
85+
for(int p = 0; p < 4; p++){
86+
int tempRow = row, tempCol = col;
87+
tempRow += dir[p][0];
88+
tempCol += dir[p][1];
89+
if(tempRow >= 0 && tempRow < height && tempCol >= 0 && tempCol < width && !used[tempRow][tempCol] && grid[tempRow][tempCol] == '1')
90+
queue.add(tempRow * width + tempCol);
91+
}
92+
}
93+
}
94+
}
95+
}
96+
return count;
97+
}
98+
}
99+
```

0 commit comments

Comments
 (0)