失效链接处理 |
LeetCode刷题题解答案 PDF 下载
本站整理下载:
相关截图:
主要内容:
ݓ͓͙⊝◨ a b ᭞ॕⰧへᬥ喌̼㺰⩗ a==b喌Ꮓ䄔ݓλ㔴ͺጝ⮳㐌ᄨի
fabs(a-b) ᭞ॕᄾν͙䬷ի喌Һັ 1e-9ȡ
ݓ̯͙᪣᭞ॕ᭞ͩ喌⩗
x % 2 != 0喌̼㺰⩗ x % 2 == 1喌ఏͩ x ञ㘬᭞䉎
ȡ ⩗
char ⮳իҋͩ㏳̺ᴶ喈Һັ喌㐎䃐ႆさ͙͜͡ႆさܩ⣟⮳⁐喉喌㺰㔲㮀ݟ
char ञ㘬᭞䉎ȡ⮳ϩ㔲㮀ݟε喌ۇٴᑩݥ䒛ͩ unsigned int ڼ⩗ҋ̺ᴶ喌䔈ϼ♥᭞
䩈⮳ȡₒ⮳։∄᭞喌ۇٴᑩݥ䒛ͩ
unsigned char喌ڼ⩗ҋ̺ᴶȡ䔈⊸ࣹ C++ ᪣ࡶ
⮳㻳݈喌ᅠ̼䄕䔟εȡ
Д̺᭞ڢν
STL Ү⩗ឯ⮳喌ᒷ้ᲐᲔ㜙Ȩ Effective STLȩ䔈ᱛΕȡ
vector string чۇٴνߗᔰܵ䙼⮳㏳
仅ۇٴ喌ᕖ㘬̹喌⩠ν
vector 㘬๎Ԍ䃰䔍㐜ڴႇ喌ఏₓ̯ᬕܵ䙼εऽ喌Ⴒ⮳ᕖ㘬䌎
㏳Ⱗᒂ喛࣎
⩗ັ喌⁐ڥ
new喌ᘾঢⱯҏ㺰Ԍऽ䲑䔊㵻ε delete喌̯ᬕᔇ䃟ε喌ᅠщܩ⣟ BUG喌
̓䔈ᵦ䰯㺰䘬ۈ̯㵻
delete喌Вⴰ̼๎ⴜ喛
ڼ⁐喌ฟᬽ้㐣㏳⮳䄌喌ङ㘬̯͙̯͙
new喌Һັ喚
int** ary = new int*[row_num];
for(int i = 0; i < row_num; ++i)
ary[i] = new int[col_num];
⩗
vector ⮳䄌̯㵻Вⴰ᥍喌
vector<vector<int> > ary(row_num, vector<int>(col_num, 0));
Ү⩗
reserve Ე䖮ټ̼ᓴ㺰⮳䛼ܵ䙼
1
せ 2 』
㏮ᕖ㶗
䔈ㆪ题Ⱍ㔲ᄎ㏮ᕖ㶗⮳᧼ҋ喌Һັ喌㏳喌ࢄ䨭㶗喌ࣻी䨭㶗へȡ
2.1 ㏳
2.1.1 Remove Duplicates from Sorted Array
䔟
Given a sorted array, remove the duplicates in place such that each element appear only once
and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example, Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
ܵᲿ ᬏ
Вⴰ 1
// LeetCode, Remove Duplicates from Sorted Array
// ᬥ䬣ᱱᏕ O(n)喌⾩䬣ᱱᏕ O(1)
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.empty()) return 0;
int index = 0;
for (int i = 1; i < nums.size(); i++) {
if (nums[index] != nums[i])
nums[++index] = nums[i];
}
return index + 1;
}
};
2
2.1 ㏳ 3
Вⴰ 2
// LeetCode, Remove Duplicates from Sorted Array
// Ү⩗ STL喌ᬥ䬣ᱱᏕ O(n)喌⾩䬣ᱱᏕ O(1)
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
return distance(nums.begin(), unique(nums.begin(), nums.end()));
}
};
Вⴰ
3
// LeetCode, Remove Duplicates from Sorted Array
// Ү⩗ STL喌ᬥ䬣ᱱᏕ O(n)喌⾩䬣ᱱᏕ O(1)
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
return distance(nums.begin(), removeDuplicates(nums.begin(), nums.end(), nums.begin()));
}
template<typename InIt, typename OutIt>
OutIt removeDuplicates(InIt first, InIt last, OutIt output) {
while (first != last) {
*output++ = *first;
first = upper_bound(first, last, *first);
}
return output;
}
};
|