1. 程式人生 > >[日常刷題]leetcode D34

[日常刷題]leetcode D34

441. Arranging Coins

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.

Given n, find the total number of full staircase rows that can be formed.

n is a non-negative integer and fits within the range of a 32-bit signed integer.

Example 1:

n = 5

The coins can form the following rows:
¤
¤ ¤
¤ ¤

Because the 3rd row is incomplete, we return 2.

Example 2:

n = 8

The coins can form the following rows:
¤
¤ ¤
¤ ¤ ¤
¤ ¤

Because the 4th row is incomplete, we return 3.

Solution in C++:

關鍵點:

  • 數列求和公式

思路:

  • 題目很明顯就是一個求和公式的逆運用,這裡我主要出的問題可能是因為資料型別寫的有點問題,然後sqrt的結果居然是nan,思索好久不懂情況,然後自己拆分之後OK,但是看到別人的寫法和我的類似但是AC,所以可能還是自己對於型別轉換這樣的問題掌握的還不夠。
int arrangeCoins(int n) {
        // x * (x + 1) / 2 = n => x = -1/2 + sqrt(2*n+1/4)
        return sqrt(2.0 * double(n) + 0.25) - 0.5;
    }

443. String Compression

Given an array of characters, compress it in-place.

The length after compression must always be smaller than or equal to the original array.

Every element of the array should be a character (not int) of length 1.

After you are done modifying the input array in-place, return the new length of the array.

Follow up:
Could you solve it using only O(1) extra space?

Example 1:

Input:
["a","a","b","b","c","c","c"]

Output:
Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]

Explanation:
"aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3".

Example 2:

Input:
["a"]

Output:
Return 1, and the first 1 characters of the input array should be: ["a"]

Explanation:
Nothing is replaced.

Example 3:

Input:
["a","b","b","b","b","b","b","b","b","b","b","b","b"]

Output:
Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].

Explanation:
Since the character "a" does not repeat, it is not compressed. "bbbbbbbbbbbb" is replaced by "b12".
Notice each digit has it's own entry in the array.

Note:

  1. All characters have an ASCII value in [35, 126].
  2. 1 <= len(chars) <= 1000.

Solution in C++:

關鍵點:

  • 掃描

思路:

  • 思路很簡單就是掃描,然後計數,新增到result字串中,這裡當然寫也可以通過變數當指標的作用使用。主要需要注意的就是控制變數的合法性。
int compress(vector<char>& chars) {
        
        size_t size = chars.size();
        
        if (size == 0)
            return 0;
        string result(1, chars[0]);
        
        for(int i = 1; i < size; ++i){
            int tmp = 1;
            
            while(chars[i] == chars[i-1] && i < size){
                ++i;
                ++tmp;
            }
            if (tmp > 1)
                result += to_string(tmp);
            if (i < size)
                result += chars[i];
        }
        
        for(int i = 0; i < result.size(); ++i)
            chars[i] = result[i];
            
        return result.size();
    }

小結

今天其實兩題應該做的都可以挺快的都是因為自己的一些細節處理的不好,不僅導致心情不好而且做的效率也不高。主要第一題的問題是關於資料型別處理的問題,然後第二題是由於粗心沒有控制變數i的合法性,導致一些奇怪現象出現,讓自己摸不著頭腦。

知識點

  • 資料型別轉換
  • i合法性控制