LeetCode #17(#183、#189、#190)

183. Customers Who Never Order

SQL Schema

Create table If Not Exists Customers (Id int, Name varchar(255))

Create table If Not Exists Orders (Id int, CustomerId int)

Truncate table Customers

insert into Customers (Id, Name) values ('1', 'Joe')

insert into Customers (Id, Name) values ('2', 'Henry')

insert into Customers (Id, Name) values ('3', 'Sam')

insert into Customers (Id, Name) values ('4', 'Max')

Truncate table Orders

insert into Orders (Id, CustomerId) values ('1', '3')

insert into Orders (Id, CustomerId) values ('2', '1')

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.

Table: Customers.

+----+-------+
| Id | Name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+

Table: Orders.

+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+

Using the above tables as example, return the following:

+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+
// Solution
# Write your MySQL query statement below
select name as Customers
from Customers
where id not in (
    select 
    customerId 
    from orders
)
189. Rotate Array

Given an array, rotate the array to the right by k steps, where k is non-negative.

Example 1:

Input: [1,2,3,4,5,6,7] and k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input: [-1,-100,3,99] and k = 2
Output: [3,99,-1,-100]
Explanation: 
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]

Note:

  • Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
  • Could you do it in-place with O(1) extra space?
// Solution

void rotate(int* nums, int numsSize, int k){
    k = k%numsSize; if (k==0) return;
    int pos = numsSize-k, i = 0, last = nums[pos], temp, m = 1, j = numsSize-1;
    while (1) {
        temp = nums[i];
        nums[i] = last;
        last = temp;
        if (j--==0) break;
        else {
            if (i < pos) i += k;
            else if (i > pos) i -= numsSize-k;
            else {i = m++; last=nums[++pos];}
        }
    }
}
190. Reverse Bits

Reverse bits of a given 32 bits unsigned integer.

Example 1:

Input: 00000010100101000001111010011100
Output: 00111001011110000010100101000000
Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.

Example 2:

Input: 11111111111111111111111111111101
Output: 10111111111111111111111111111111
Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111.

Note:

  • Note that in some languages such as Java, there is no unsigned integer type. In this case, both input and output 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 2 above the input represents the signed integer -3 and the output represents the signed integer -1073741825.

Follow up:

If this function is called many times, how would you optimize it?

// Solution
uint32_t reverseBits(uint32_t n) {
    uint32_t m = 0;
    for (int i = 0; i < 32; i++, n >>= 1) {
        m <<= 1;
        m |= n & 1;
    }
    return m;
}
发布了81 篇原创文章 · 获赞 24 · 访问量 8421

猜你喜欢

转载自blog.csdn.net/weixin_44198992/article/details/105609715