Skip to content

Commit ca302f5

Browse files
committed
6/12 practice
1 parent af53caa commit ca302f5

9 files changed

+445
-0
lines changed

171. Excel Sheet Column Number.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
3+
Given a column title as appear in an Excel sheet, return its corresponding column number.
4+
5+
For example:
6+
7+
A -> 1
8+
B -> 2
9+
C -> 3
10+
...
11+
Z -> 26
12+
AA -> 27
13+
AB -> 28
14+
...
15+
Example 1:
16+
17+
Input: "A"
18+
Output: 1
19+
Example 2:
20+
21+
Input: "AB"
22+
Output: 28
23+
Example 3:
24+
25+
Input: "ZY"
26+
Output: 701
27+
28+
29+
First solution is recursive, second is loop
30+
*/
31+
class Solution {
32+
public:
33+
int titleToNumber(string s) {
34+
return s == "" ? 0 : ( pow( 26, s.size() - 1 ) * ( s[0] - 64 ) +
35+
titleToNumber( s.substr( 1 ) ) );
36+
37+
}
38+
};
39+
40+
41+
class Solution {
42+
public:
43+
int titleToNumber(string s) {
44+
int res = 0;
45+
for ( int i = 0; i < s.size(); ++i )
46+
{
47+
res *= 26;
48+
res += s[i] - 'A' + 1;
49+
}
50+
return res;
51+
}
52+
};

172. Factorial Trailing Zeroes.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
3+
Given an integer n, return the number of trailing zeroes in n!.
4+
5+
Example 1:
6+
7+
Input: 3
8+
Output: 0
9+
Explanation: 3! = 6, no trailing zero.
10+
Example 2:
11+
12+
Input: 5
13+
Output: 1
14+
Explanation: 5! = 120, one trailing zero.
15+
Note: Your solution should be in logarithmic time complexity.
16+
17+
*/
18+
19+
class Solution {
20+
public:
21+
int trailingZeroes(int n) {
22+
int count = 0;
23+
while ( n )
24+
{
25+
count += n / 5;
26+
n = n / 5;
27+
}
28+
return count;
29+
}
30+
};

175. Combine Two Tables.sql

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
SQL Schema
3+
Table: Person
4+
5+
+-------------+---------+
6+
| Column Name | Type |
7+
+-------------+---------+
8+
| PersonId | int |
9+
| FirstName | varchar |
10+
| LastName | varchar |
11+
+-------------+---------+
12+
PersonId is the primary key column for this table.
13+
Table: Address
14+
15+
+-------------+---------+
16+
| Column Name | Type |
17+
+-------------+---------+
18+
| AddressId | int |
19+
| PersonId | int |
20+
| City | varchar |
21+
| State | varchar |
22+
+-------------+---------+
23+
AddressId is the primary key column for this table.
24+
25+
26+
Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:
27+
28+
FirstName, LastName, City, State
29+
30+
*/
31+
32+
# Write your MySQL query statement below
33+
SELECT FirstName, LastName, City, State FROM Person
34+
LEFT JOIN Address
35+
ON Person.PersonId = Address.PersonId;

176. Second Highest Salary.sql

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Write a SQL query to get the second highest salary from the Employee table.
3+
4+
+----+--------+
5+
| Id | Salary |
6+
+----+--------+
7+
| 1 | 100 |
8+
| 2 | 200 |
9+
| 3 | 300 |
10+
+----+--------+
11+
For example, given the above Employee table, the query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null.
12+
13+
+---------------------+
14+
| SecondHighestSalary |
15+
+---------------------+
16+
| 200 |
17+
+---------------------+
18+
First one create temporary table, second one using IFNULL
19+
*/
20+
# Write your MySQL query statement below
21+
SELECT
22+
(SELECT DISTINCT Salary FROM Employee
23+
ORDER BY Salary DESC LIMIT 1 OFFSET 1)
24+
AS SecondHighestSalary
25+
26+
27+
# Write your MySQL query statement below
28+
SELECT
29+
IFNULL( (SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 1 OFFSET 1), NULL) AS SecondHighestSalary
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.
3+
4+
+----+-------+--------+-----------+
5+
| Id | Name | Salary | ManagerId |
6+
+----+-------+--------+-----------+
7+
| 1 | Joe | 70000 | 3 |
8+
| 2 | Henry | 80000 | 4 |
9+
| 3 | Sam | 60000 | NULL |
10+
| 4 | Max | 90000 | NULL |
11+
+----+-------+--------+-----------+
12+
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.
13+
14+
+----------+
15+
| Employee |
16+
+----------+
17+
| Joe |
18+
+----------+
19+
20+
Solution one is not using join, two is using join
21+
*/# Write your MySQL query statement below
22+
SELECT
23+
a.Name As Employee
24+
FROM
25+
Employee AS a,
26+
Employee AS b
27+
WHERE
28+
a.ManagerId = b.ID
29+
AND
30+
a.Salary > b.Salary
31+
;
32+
33+
34+
35+
SELECT
36+
a.Name As Employee
37+
FROM
38+
Employee AS a
39+
JOIN
40+
Employee AS b
41+
ON
42+
a.ManagerId = b.ID
43+
AND
44+
a.Salary > b.Salary

182. Duplicate Emails.sql

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Write a SQL query to find all duplicate emails in a table named Person.
3+
4+
+----+---------+
5+
| Id | Email |
6+
+----+---------+
7+
8+
9+
10+
+----+---------+
11+
For example, your query should return the following for the above table:
12+
13+
+---------+
14+
| Email |
15+
+---------+
16+
17+
+---------+
18+
Note: All emails are in lowercase.
19+
20+
First Solution using having, second one use WHERE
21+
*/
22+
23+
# Write your MySQL query statement below
24+
SELECT Email FROM Person GROUP BY Email
25+
HAVING COUNT( Email ) > 1;
26+
27+
# Write your MySQL query statement below
28+
SELECT Email FROM(
29+
SELECT Email, COUNT(Email) AS num FROM Person GROUP BY Email
30+
) AS temp
31+
WHERE temp.num > 1;

183. Customers Who Never Order.sql

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
SQL Schema
3+
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.
4+
5+
Table: Customers.
6+
7+
+----+-------+
8+
| Id | Name |
9+
+----+-------+
10+
| 1 | Joe |
11+
| 2 | Henry |
12+
| 3 | Sam |
13+
| 4 | Max |
14+
+----+-------+
15+
Table: Orders.
16+
17+
+----+------------+
18+
| Id | CustomerId |
19+
+----+------------+
20+
| 1 | 3 |
21+
| 2 | 1 |
22+
+----+------------+
23+
Using the above tables as example, return the following:
24+
25+
+-----------+
26+
| Customers |
27+
+-----------+
28+
| Henry |
29+
| Max |
30+
+-----------+
31+
32+
Solution one us NOT IN, two use LEFT JOIN
33+
*/
34+
# Write your MySQL query statement below
35+
SELECT Name AS Customers FROM Customers WHERE Id
36+
NOT IN ( SELECT CustomerID FROM Orders);
37+
38+
SELECT Name AS Customers
39+
FROM Customers
40+
LEFT JOIN Orders
41+
ON Customers.Id = Orders.CustomerId
42+
WHERE Orders.CustomerId IS NULL;

189. Rotate Array.cpp

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
Given an array, rotate the array to the right by k steps, where k is non-negative.
3+
4+
Follow up:
5+
6+
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
7+
Could you do it in-place with O(1) extra space?
8+
9+
10+
Example 1:
11+
12+
Input: nums = [1,2,3,4,5,6,7], k = 3
13+
Output: [5,6,7,1,2,3,4]
14+
Explanation:
15+
rotate 1 steps to the right: [7,1,2,3,4,5,6]
16+
rotate 2 steps to the right: [6,7,1,2,3,4,5]
17+
rotate 3 steps to the right: [5,6,7,1,2,3,4]
18+
Example 2:
19+
20+
Input: nums = [-1,-100,3,99], k = 2
21+
Output: [3,99,-1,-100]
22+
Explanation:
23+
rotate 1 steps to the right: [99,-1,-100,3]
24+
rotate 2 steps to the right: [3,99,-1,-100]
25+
26+
27+
Constraints:
28+
29+
1 <= nums.length <= 2 * 10^4
30+
It's guaranteed that nums[i] fits in a 32 bit-signed integer.
31+
k >= 0
32+
33+
34+
35+
Solution one using brute force, it does not work in C++
36+
Two using extra space
37+
Three using rotation.
38+
*/
39+
class Solution {
40+
public:
41+
void rotate(vector<int>& nums, int k) {
42+
int size = nums.size();
43+
k = k % size;
44+
if( k == 0 ) return;
45+
int temp, previous;
46+
for ( int i = 0; i < k; i++ )
47+
{
48+
previous = nums[size - 1];
49+
for ( int j = 0; j < size; j++ )
50+
{
51+
temp = nums[j];
52+
nums[j] = previous;
53+
previous = temp;
54+
}
55+
}
56+
}
57+
};
58+
59+
class Solution {
60+
public:
61+
void rotate(vector<int>& nums, int k) {
62+
int size = nums.size();
63+
k = k % size;
64+
if ( k == 0 ) return;
65+
vector<int> temp( size );
66+
for ( int i = 0; i < size; i++ )
67+
{
68+
temp[ ( i + k ) % size ] = nums[i];
69+
}
70+
nums = temp;
71+
}
72+
};
73+
74+
class Solution {
75+
public:
76+
void rotate(vector<int>& nums, int k) {
77+
int size = nums.size();
78+
k = k % size;
79+
if( k == 0 ) return;
80+
int count = 0;
81+
//We need this loop as when ( curr + k ) % size = 0, the array may not be fully rotated.
82+
for ( int i = 0; count < size; i++ )
83+
{
84+
int curr = i;
85+
int prev = nums[i];
86+
do
87+
{
88+
int next = ( curr + k ) % size;
89+
int temp = nums[next];
90+
nums[next] = prev;
91+
curr = next;
92+
prev = temp;
93+
count++;
94+
} while( i != curr );
95+
}
96+
}
97+
};

0 commit comments

Comments
 (0)