ARTS挑战-第一周

2019-03-23  本文已影响0人  陈_振

Algorithm

Leetcode-283

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

示例:

输入: [0,1,0,3,12]
输出: [1,3,12,0,0]
说明:

必须在原数组上操作,不能拷贝额外的数组。
尽量减少操作次数。


class Solution {
public:
    void moveZeroes(vector<int>& nums) {

        vector<int> notZeroArray;

        for(int i = 0 ; i < nums.size() ; i++) {
            if(nums[i]) {
                notZeroArray.push_back(nums[i]);
            }
        }
        
        for(int i = 0 ; i < notZeroArray.size() ; i++) {
            nums[i] = notZeroArray[i];
        }
        
        for(int i = notZeroArray.size() ; i < nums.size() ; i++) {
            nums[i] = 0;
        }
    }
};

Review

NSHashTable&NSMapTable

本文的单词摘自NSHiper文档,记录自己看文档时遇到的生词。

  1. workhorse
    adj. 工作重的;吃苦耐劳的
    美 ['wə:khɔ:s]
    eg: NSArray are the workhorse collection classes of Foundation.
    (这里可翻译为主要的)

  2. general-purpose
    adj. 多用途的;一般用途的
    英 ['dʒenərəl'pə:pəs]
    eg: use a more general-purpose solution.

  3. breaking assumption
    n. 打破假设,不符合预期
    eg: For NSSet and NSDictionary, the breaking assumption was in the memory behavior when storing objects in the collection.

Tip

参考文章:
nil/Nil/NSNull

1.png

NULL:C类型的指针(void *),指针变量,空指针。

nil:是一个对象类型指针,指向nothing。

Nil:是一个类对象的指针,指向nothing。

NSNull :是一个OC对象,用来表达空的单例对象。常用于存放在容器类对象中(NSArray,NSDictionary)

NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];
mutableDictionary[@"someKey"] = [NSNull null]; // Sets value of NSNull singleton for `someKey`
NSLog(@"Keys: %@", [mutableDictionary allKeys]); // @[@"someKey"]

Share

你可以不自己造轮子,但应该了解轮子的结构,而且越详尽越好,这就是程序员的自我修养吧。 ----《程序员的自我修养》

CPU体系结构,汇编,C语言(包括C++)和操作系统,永远都是编程大师们的护身法宝。 ----《《程序员的自我修养》》

珍惜在学校的最后两三个月。

上一篇 下一篇

猜你喜欢

热点阅读