191. Number of 1 Bits
Write a function that takes an unsigned integer and return the number of ‘1’ bits it has (also known as the Hamming weight).
Example 1:
Input: 00000000000000000000000000001011 Output: 3 Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.
Example 2:
Input: 00000000000000000000000010000000 Output: 1 Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.
Example 3:
Input: 11111111111111111111111111111101 Output: 31 Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.
Note:
- 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.
- 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
.Follow up:
If this function is called many times, how would you optimize it?
// Solution
int hammingWeight(uint32_t n) {
int cnt = 0;
while(n>0){
uint32_t temp = n-1;
n = temp &n;
cnt++;
}
return cnt;
}
196. Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table named
Person
, keeping only unique emails based on its smallest Id.+----+------------------+ | Id | Email | +----+------------------+ | 1 | john@example.com | | 2 | bob@example.com | | 3 | john@example.com | +----+------------------+ Id is the primary key column for this table.
For example, after running your query, the above
Person
table should have the following rows:+----+------------------+ | Id | Email | +----+------------------+ | 1 | john@example.com | | 2 | bob@example.com | +----+------------------+
Note:
Your output is the whole
Person
table after executing your sql. Usedelete
statement.
// Solution
-- 不能直接选择
DELETE FROM Person
WHERE Id IN
(SELECT Id
from (select P1.Id
FROM Person AS P1, Person AS P2
WHERE P1.Id > P2.Id AND P1.Email = P2.Email) temp);
197. Rising Temperature
SQL Schema
Create table If Not Exists Weather (Id int, RecordDate date, Temperature int) Truncate table Weather insert into Weather (Id, RecordDate, Temperature) values ('1', '2015-01-01', '10') insert into Weather (Id, RecordDate, Temperature) values ('2', '2015-01-02', '25') insert into Weather (Id, RecordDate, Temperature) values ('3', '2015-01-03', '20') insert into Weather (Id, RecordDate, Temperature) values ('4', '2015-01-04', '30')
Given a
Weather
table, write a SQL query to find all dates’ Ids with higher temperature compared to its previous (yesterday’s) dates.+---------+------------------+------------------+ | Id(INT) | RecordDate(DATE) | Temperature(INT) | +---------+------------------+------------------+ | 1 | 2015-01-01 | 10 | | 2 | 2015-01-02 | 25 | | 3 | 2015-01-03 | 20 | | 4 | 2015-01-04 | 30 | +---------+------------------+------------------+
For example, return the following Ids for the above
Weather
table:+----+ | Id | +----+ | 2 | | 4 | +----+
// Solution
-- 不能直接从id1-1 = id2这样子判断
SELECT wt1.Id
FROM Weather wt1, Weather wt2
WHERE wt1.Temperature > wt2.Temperature AND
TO_DAYS(wt1.RecordDate)-TO_DAYS(wt2.RecordDate)=1;