1. 程式人生 > >LeetCode刷題記錄(第六天)

LeetCode刷題記錄(第六天)

Swap Salary

這道題竟然是一個sql題,雖然經常寫sql和看關於mysql的效能優化,但是我發現一個大問題,我都白學來。。。。

題目:

Given a table salary, such as the one below, that has m=male and f=female values. Swap all f and m values (i.e., change all f values to m and vice versa) with a single update query and no intermediate temp table.

翻譯:

給定一個表格salary,如下面

的表格,其中m =男性,f =女性值。用一個更新查詢和沒有中間臨時表交換所有f和m值(即,將所有f值更改為m,反之亦然)。

事例:

For example:
| id | name | sex | salary |
|----|------|-----|--------|
| 1  | A    | m   | 2500   |
| 2  | B    | f   | 1500   |
| 3  | C    | m   | 5500   |
| 4  | D    | f   | 500    |
After running your query, the above salary table should have the following rows:
| id | name | sex | salary |
|----|------|-----|--------|
| 1  | A    | f   | 2500   |
| 2  | B    | m   | 1500   |
| 3  | C    | f   | 5500   |
| 4  | D    | m   | 500    |

這道題沒有什麼好講的,就是一個更新操作賦值時case...when的一個用法,case+要更新的欄位,when+條件,then+要更新的值,可以多個連結使用,當然,case...then也可以用在查詢中,其他用法可以再去多學習一些。

sql:

UPDATE salary
SET
    sex = CASE sex
        WHEN 'm' THEN 'f'
        ELSE 'm'
    END;

Array Partition I

原題目:

Given an array of 2n integers, your task is to group these integers into 

n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.

事例:

Example 1:

Input: [1,4,3,2]

Output: 4
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).

Note:

  1. n is a positive integer, which is in the range of [1, 10000].
  2. All the integers in the array will be in the range of [-10000, 10000].

翻譯後:

給定一個2n整數的陣列,你的任務是將這些整數分成n對整數,例如(a 1,b 1),(a 2,b 2),...,(a n,b n對於從1到n的所有i ,min(a i,b i)的總和儘可能大。

思路:

讀完題目後,思考了一下,想要和最大,首先需要從小到大排序,因為只有從小到大排序後,兩個一組中取最小數相加的和最大。而當從小到大排序後,所要相加的數則為第一個、第三個、第五個.....以此類推,所以一步一步想,問題還是能想明白的,就是不知道我這麼想有沒有什麼問題,但是結果還是對的嘛,下面是我的程式碼:

package com.mianshi.suanfa.ArrayPartitionI;

/**
 * Created by macbook_xu on 2018/3/25.
 */
public class Solution {
    public static int arrayPairSum(int[] nums) {
        for (int i = 0 ; i<nums.length ; i++){
            for (int j = i+1 ; j<nums.length ; j++){
                if(nums[j]<nums[i]){
                    int temp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = temp;
                }
            }
        }

        int count = 0;
        for (int i = 0 ; i<nums.length ; i+=2){
            count += nums[i];
        }

        return count;
    }

    public static void main(String[] args) {
        int[] nums = {1,4,3,2};
        System.out.println(arrayPairSum(nums));
    }
}

我現在用的最熟的也只有最簡單的冒泡了。。。。雖然學過快速排序啊、插入排序啊什麼的,但是沒有使用過,還不太熟,最近好好學學排序,下次就不用冒泡丟人了。。。。當然,陣列本身也是可以進行排序的,就像下面這個方法:

class Solution {
    public int arrayPairSum(int[] nums) {
        int sum = 0;
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i += 2) {
            sum += nums[i];
        }
        
        return sum;
    }
}

直接利用陣列的排序,然後進行求和,是不是一下覺得更加簡單了呢?

今天的這兩個題都比較簡單,還順便複習來一下sql,感覺還是不錯的,下面是本週的程式碼整理,以後每週都會往GitHub上這個專案裡更新的。