Skip to content

Commit df67e44

Browse files
committed
6/13
1 parent a500a6c commit df67e44

10 files changed

+444
-0
lines changed

Diff for: 191. Number of 1 Bits.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight).
3+
4+
5+
6+
Example 1:
7+
8+
Input: 00000000000000000000000000001011
9+
Output: 3
10+
Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.
11+
Example 2:
12+
13+
Input: 00000000000000000000000010000000
14+
Output: 1
15+
Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.
16+
Example 3:
17+
18+
Input: 11111111111111111111111111111101
19+
Output: 31
20+
Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.
21+
22+
23+
Note:
24+
25+
Note that in some languages such as Java, there is no unsigned integer type. In this case, the input will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.
26+
In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3 above the input represents the signed integer -3.
27+
28+
29+
Follow up:
30+
31+
If this function is called many times, how would you optimize it?
32+
33+
Solution one using brute force
34+
*/
35+
36+
class Solution {
37+
public:
38+
int hammingWeight(uint32_t n) {
39+
int count = 0;
40+
while ( n )
41+
{
42+
if ( n & 1 ) count++;
43+
n = n >> 1;
44+
}
45+
return count;
46+
}
47+
};

Diff for: 193. Valid Phone Numbers.bash

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers.
2+
3+
You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)
4+
5+
You may also assume each line in the text file must not contain leading or trailing white spaces.
6+
7+
Example:
8+
9+
Assume that file.txt has the following content:
10+
11+
987-123-4567
12+
123 456 7890
13+
(123) 456-7890
14+
Your script should output the following valid phone numbers:
15+
16+
987-123-4567
17+
(123) 456-7890
18+
19+
20+
# Read from the file file.txt and output all valid phone numbers to stdout.
21+
grep -oP '^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$' file.txt

Diff for: 195. Tenth Line.bash

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Given a text file file.txt, print just the 10th line of the file.
2+
3+
Example:
4+
5+
Assume that file.txt has the following content:
6+
7+
Line 1
8+
Line 2
9+
Line 3
10+
Line 4
11+
Line 5
12+
Line 6
13+
Line 7
14+
Line 8
15+
Line 9
16+
Line 10
17+
Your script should output the tenth line, which is:
18+
19+
Line 10
20+
Note:
21+
1. If the file contains less than 10 lines, what should you output?
22+
2. There's at least three different solutions. Try to explore all possibilities.
23+
24+
# Read from the file file.txt and output the tenth line to stdout.
25+
awk 'NR == 10' file.txt
26+
27+
sed -n '10p' file.txt
28+
29+
line=$(cat file.txt | wc -l)
30+
if [ "$line" -ge 10 ] ; then
31+
cat file.txt | head -n 10 | tail -n 1
32+
fi

Diff for: 196. Delete Duplicate Emails.sql

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
3+
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.
4+
5+
+----+------------------+
6+
| Id | Email |
7+
+----+------------------+
8+
9+
10+
11+
+----+------------------+
12+
Id is the primary key column for this table.
13+
For example, after running your query, the above Person table should have the following rows:
14+
15+
+----+------------------+
16+
| Id | Email |
17+
+----+------------------+
18+
19+
20+
+----+------------------+
21+
Note:
22+
23+
Your output is the whole Person table after executing your sql. Use delete statement.
24+
25+
*/
26+
27+
# Write your MySQL query statement below
28+
29+
# This using DELETE
30+
DELETE FROM Person WHERE Id NOT IN (
31+
SELECT TempId FROM (
32+
SELECT MIN(Id) AS TempId FROM Person GROUP BY Email
33+
)AS minId
34+
);
35+
36+
SELECT
37+
MIN(Id) AS Id,
38+
Email
39+
FROM Person
40+
GROUP BY Email
41+
ORDER BY 1 ASC

Diff for: 197. Rising Temperature.sql

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates.
3+
4+
+---------+------------------+------------------+
5+
| Id(INT) | RecordDate(DATE) | Temperature(INT) |
6+
+---------+------------------+------------------+
7+
| 1 | 2015-01-01 | 10 |
8+
| 2 | 2015-01-02 | 25 |
9+
| 3 | 2015-01-03 | 20 |
10+
| 4 | 2015-01-04 | 30 |
11+
+---------+------------------+------------------+
12+
For example, return the following Ids for the above Weather table:
13+
14+
+----+
15+
| Id |
16+
+----+
17+
| 2 |
18+
| 4 |
19+
+----+
20+
*/
21+
22+
# Write your MySQL query statement below
23+
SELECT b.Id FROM
24+
Weather a, Weather b
25+
WHERE a.RecordDate = date_sub( b.RecordDate, INTERVAL 1 day )
26+
AND a.Temperature < b.Temperature;

Diff for: 198. House Robber.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
3+
4+
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
5+
6+
7+
8+
Example 1:
9+
10+
Input: nums = [1,2,3,1]
11+
Output: 4
12+
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
13+
Total amount you can rob = 1 + 3 = 4.
14+
Example 2:
15+
16+
Input: nums = [2,7,9,3,1]
17+
Output: 12
18+
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
19+
Total amount you can rob = 2 + 9 + 1 = 12.
20+
21+
22+
Constraints:
23+
24+
0 <= nums.length <= 100
25+
0 <= nums[i] <= 400
26+
27+
Solution using DP
28+
*/
29+
class Solution {
30+
public:
31+
int rob(vector<int>& nums) {
32+
int size = nums.size();
33+
if ( size == 0 ) return size;
34+
int new_sum = nums[0];
35+
int old_sum = 0;
36+
for ( int i = 1; i < size; ++i )
37+
{
38+
int temp = new_sum;
39+
new_sum = max( old_sum + nums[i], new_sum );
40+
old_sum = temp;
41+
}
42+
return max( old_sum, new_sum );
43+
}
44+
};

Diff for: 202. Happy Number.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Write an algorithm to determine if a number n is "happy".
3+
4+
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
5+
6+
Return True if n is a happy number, and False if not.
7+
8+
Example:
9+
10+
Input: 19
11+
Output: true
12+
Explanation:
13+
1^2 + 9^2 = 82
14+
8^2 + 2^2 = 68
15+
6^2 + 8^2 = 100
16+
1^2 + 0^2 + 0^2 = 1
17+
*/
18+
19+
//Solution using unordered_map, set, unordered_map can also do this
20+
21+
class Solution {
22+
private:
23+
vector<int> square = { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 };
24+
public:
25+
bool isHappy(int n) {
26+
unordered_map<int, int> temp;
27+
while ( n != 1 )
28+
{
29+
int res = 0;
30+
while ( n )
31+
{
32+
res += square[n % 10];
33+
n = n / 10;
34+
}
35+
temp[res]++;
36+
if ( temp[res] > 1 ) return false;
37+
n = res;
38+
}
39+
return true;
40+
}
41+
};

Diff for: 203. Remove Linked List Elements.cpp

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Remove all elements from a linked list of integers that have value val.
3+
4+
Example:
5+
6+
Input: 1->2->6->3->4->5->6, val = 6
7+
Output: 1->2->3->4->5
8+
*/
9+
10+
/**
11+
* Definition for singly-linked list.
12+
* struct ListNode {
13+
* int val;
14+
* ListNode *next;
15+
* ListNode() : val(0), next(nullptr) {}
16+
* ListNode(int x) : val(x), next(nullptr) {}
17+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
18+
* };
19+
*/
20+
class Solution {
21+
public:
22+
ListNode* removeElements(ListNode* head, int val) {
23+
if ( !head ) return nullptr;
24+
ListNode *curr = head;
25+
ListNode *fast = head->next;
26+
while ( curr && curr->val == val )
27+
{
28+
curr = curr->next;
29+
if ( curr ) fast = curr->next;
30+
head = curr;
31+
}
32+
while ( curr && fast )
33+
{
34+
while ( fast && fast->val == val )
35+
{
36+
fast = fast->next;
37+
curr->next = fast;
38+
}
39+
curr = curr->next;
40+
if ( curr ) fast = curr->next;
41+
}
42+
return head;
43+
}
44+
};
45+
46+
47+
48+
//Clearer version, using one less pointer
49+
/**
50+
* Definition for singly-linked list.
51+
* struct ListNode {
52+
* int val;
53+
* ListNode *next;
54+
* ListNode() : val(0), next(nullptr) {}
55+
* ListNode(int x) : val(x), next(nullptr) {}
56+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
57+
* };
58+
*/
59+
class Solution {
60+
public:
61+
ListNode* removeElements(ListNode* head, int val) {
62+
if ( !head ) return nullptr;
63+
ListNode *curr = head;
64+
ListNode *fast = head->next;
65+
while ( curr && curr->val == val )
66+
{
67+
curr = curr->next;
68+
}
69+
head = curr;
70+
while ( curr && curr->next )
71+
{
72+
if ( curr->next->val == val ) curr->next = curr->next->next;
73+
else curr = curr->next;
74+
}
75+
return head;
76+
}
77+
};

Diff for: 204. Count Primes.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Count the number of prime numbers less than a non-negative number, n.
3+
4+
Example:
5+
6+
Input: 10
7+
Output: 4
8+
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
9+
10+
Solution one using brute force
11+
12+
Notes: No overthinking, overwise will not pass.
13+
14+
15+
Solution two use sqrt can reduce the computation time, this work, but will not work on leetcode.
16+
*/
17+
18+
class Solution {
19+
public:
20+
int countPrimes(int n) {
21+
vector<bool> temp( n, 1 );
22+
int res = 0;
23+
for ( int i = 2; i < n; i++ )
24+
{
25+
if ( !temp[i] ) continue;
26+
res++;
27+
for ( int j = 2; i * j < n; j++ )
28+
{
29+
temp[i * j] = 0;
30+
}
31+
}
32+
return res;
33+
}
34+
};
35+
36+
//This is the best, wiredly not work on leetcode.s
37+
class Solution {
38+
public:
39+
int countPrimes(int n) {
40+
vector<bool> temp( n, 0 );
41+
int res = 0;
42+
temp[0] = 1;
43+
temp[1] = 1;
44+
for ( int i = 2; i <= sqrt( n ); ++i )
45+
{
46+
if ( temp[i] )
47+
{
48+
continue;
49+
}
50+
for ( int k = i * i; k < n; k += i )
51+
{
52+
temp[k] = 1;
53+
}
54+
55+
}
56+
for ( int i = 0; i < temp.size(); i++ )
57+
{
58+
if ( !temp[i] ) res++;
59+
}
60+
return res;
61+
}
62+
};

0 commit comments

Comments
 (0)