A
LeetCode:
26. Remove Duplicates from Sorted Array
Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.
Example 1:
Given nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,0,1,1,1,2,2,3,3,4],
Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
It doesn't matter what values are set beyond the returned length.
我的代碼如下
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
vector<int>::iterator i, it = nums.begin() + 1;
if(nums.size() == 0) {return 0;}
if(nums.size() == 1) {return nums.size();}
for(i = nums.begin() + 1; i != nums.end();) {
if(*(i - 1) == *i) {
i = nums.erase(i);
}
else {i++;}
}
return nums.size();
}
};
運(yùn)行結(jié)果:
Runtime: 160 ms, faster than 15.01% of C++ online submissions for Remove Duplicates from Sorted Array.
Memory Usage: 10 MB, less than 99.00% of C++ online submissions for Remove Duplicates from Sorted Array.
本題我主要使用了vector中的eraser()
方法陪捷,但運(yùn)行的時間很不理想萤衰。對比其他代碼:
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.size() == 0) return 0;
int length = 1;
int pos = 0;
for (int i = 1; i < nums.size(); i++) {
if (nums[i] != nums[pos]) {
length++;
pos++;
nums[pos] = nums[i];
}
}
return length;
}
};
該作者巧妙地將前列元素逐個替代,并記錄數(shù)組長度日川,達(dá)到截取數(shù)組的效果,明顯提高了效率囱淋。
R
Why wearables, health records and clinical trials need a blockchain injection
- 本文主要講述的是個人的健康數(shù)據(jù)與區(qū)塊鏈的關(guān)系秘车。
個人的健康記錄是一種敏感信息,對于醫(yī)療保障事業(yè)來說轻腺,它具有巨大的作用;但是除了一些研究人員的使用划乖,大量地共享個人的電子健康記錄可能對用戶的隱私造成侵犯贬养。因此,現(xiàn)在正在開發(fā)一種能匿名共享個人的健康數(shù)據(jù)琴庵,能夠在患者同意的前提下误算,出售或共享自己的健康數(shù)據(jù)。而區(qū)塊鏈能為每一個加密的數(shù)據(jù)塊提供唯一的數(shù)字簽名迷殿,很好地保障了用戶信息地安全性儿礼。不僅如此,它還能用于藥物的跟蹤監(jiān)督以及對勒索病毒地防范庆寺,安全性較強(qiáng)蚊夫。
T
- 頭文件:
<vector>
- 構(gòu)造:
vector<type> vec;
- 迭代器:
begin
:返回指向容器第一個元素的迭代器
end
:返回指向容器最后一個元素下一個位置的迭代器
rbegin
:返回指向容器最后一個元素的反向迭代器
rend
:返回指向容器第一個元素前一個位置的反向迭代器
cbegin
:返回指向容器第一個元素的const_iterator迭代器
cend
:返回指向容器最后一個元素的const_iterator迭代器
crbegin
:返回指向容器最后一個元素的反向const_iterator迭代器
crend
:返回指向容器第一個元素前一個位置的反向const_iterator迭代器 - 容量:
size
:返回容器大小
max_size
:返回容器最大大小
resize
:調(diào)整容器大小
capacity
:返回已分配存儲容量的大小
empty
:測試vector是否為空
reserve
:請求更改容量
shrink_to_fit
:收縮至適合 - 元素的訪問:
使用[]
操作:類似數(shù)組訪問
at
:與[]
相似
front
:訪問第一個元素
back
:訪問最后一個元素
data
:訪問數(shù)據(jù) - 修飾符:
asign
:重新分配元素
push_back
:末尾添加元素
pop_back
:刪除末尾元素
insert
:插入元素
erase
:清除某一元素
swap
:交換兩容器內(nèi)容
clear
:清空內(nèi)容
emplace
:類似insert
emplace_back
:類似push_back
S
分享的網(wǎng)站:tutorialspoint
本周嘗試了一些難度較大的算法題,到目前為止仍未做出——算法路漫漫啊~~還是慢慢來吧懦尝。