iT邦幫忙

2021 iThome 鐵人賽

DAY 3
1
AI & Data

想到甚麼打甚麼系列 第 3

找LeetCode上簡單的題目來撐過30天啦(DAY3)

各位中秋節連假愉快,我今天坐客運,還包車了呢,司機先生只載我一個人,運氣不錯,好啦今天也要繼續努力解題囉

今天的第一題,莫名其妙選到SQL題了呢,題目我真的都是亂選的,看哪個題目看起來比較親切,然後內容感覺上是我寫得出來的,好啦上題目
題號:183 標題:Customers Who Never Order 難度:Easy

題目內容:
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 |
+-----------+

我的SQL指令

select Customers.Name Customers  from Customers WHERE Customers.Id NOT IN(SELECT Orders.CustomerId from Orders)

用到重新命名欄位跟NOT IN,痾,這題沒甚麼說的,就再來一題吧

題號:283 標題:Move Zeroes 難度:Easy
題目內仍:
Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.

Example 1:
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
Example 2:
Input: nums = [0]
Output: [0]

Constraints:
• 1 <= nums.length <= 104
• -231 <= nums[i] <= 231 - 1

Follow up: Could you minimize the total number of operations done?

我的程式碼

void moveZeroes(int* nums, int numsSize){
    int i =0,count=0,count2,j;
    
     for(i=0;i<numsSize;i++){ //計算多少個0
        if(nums[i]==0){
            count++;
        }
    }
    count2=numsSize-count;//多少個非0
    j=0;
    for(i=0;i<numsSize;i++){ 
        
        if(nums[i] !=  0){
            nums[j] = nums[i]; //把非0塞到前面去
            count2=count2-1;
            j++;
            if(i >= numsSize-count){ //當i已經過了非0的idex,全塞0
                nums[i] = 0;
            }
        }
    }
    
    return nums;
}

花比較久的時間
在於怎麼把正確的0的數量補上
其實這題我也是最直觀的解法,先計算非0的各數,然後再把非0的數值填回去,當都填完以後,其他的全部塞0。

DAY3心得
不知不覺第三天來囉,漸入佳境,大家加油阿,晚安


上一篇
找LeetCode上簡單的題目來撐過30天啦(DAY2)
下一篇
找LeetCode上簡單的題目來撐過30天啦(DAY4)
系列文
想到甚麼打甚麼30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言