Algorithm
給定一個(gè)數(shù)組 nums,編寫一個(gè)函數(shù)將所有 0 移動(dòng)到數(shù)組的末尾,同時(shí)保持非零元素的相對順序。
示例:
輸入: [0,1,0,3,12]
輸出: [1,3,12,0,0]
說明:
必須在原數(shù)組上操作,不能拷貝額外的數(shù)組。
盡量減少操作次數(shù)。
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
本文的單詞摘自NSHiper文檔,記錄自己看文檔時(shí)遇到的生詞。
workhorse
adj. 工作重的;吃苦耐勞的
美 ['w?:kh?:s]
eg: NSArray are the workhorse collection classes of Foundation.
(這里可翻譯為主要的)general-purpose
adj. 多用途的;一般用途的
英 ['d?en?r?l'p?:p?s]
eg: use a more general-purpose solution.breaking assumption
n. 打破假設(shè),不符合預(yù)期
eg: For NSSet and NSDictionary, the breaking assumption was in the memory behavior when storing objects in the collection.
Tip
參考文章:
nil/Nil/NSNull

NULL:C類型的指針(void *),指針變量,空指針。
nil:是一個(gè)對象類型指針,指向nothing。
Nil:是一個(gè)類對象的指針,指向nothing。
NSNull :是一個(gè)OC對象,用來表達(dá)空的單例對象。常用于存放在容器類對象中(NSArray,NSDictionary)
NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];
mutableDictionary[@"someKey"] = [NSNull null]; // Sets value of NSNull singleton for `someKey`
NSLog(@"Keys: %@", [mutableDictionary allKeys]); // @[@"someKey"]
Share
你可以不自己造輪子,但應(yīng)該了解輪子的結(jié)構(gòu),而且越詳盡越好,這就是程序員的自我修養(yǎng)吧。 ----《程序員的自我修養(yǎng)》
CPU體系結(jié)構(gòu),匯編,C語言(包括C++)和操作系統(tǒng),永遠(yuǎn)都是編程大師們的護(hù)身法寶。 ----《《程序員的自我修養(yǎng)》》
珍惜在學(xué)校的最后兩三個(gè)月。