442. Find All Duplicates in an Array

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements that appear twice in this array.
Could you do it without extra space and in O(n) runtime?
 
Example:
Input:
[4,3,2,7,8,2,3,1]

Output:
[2,3]

 Success: 本來的做法update在github裡面,採用先sort在比對的方式,不過performance不好,所以跑去看論壇裡的解法
這題最快的人是用set寫,但是我覺得多開一個seen set這種做法應該不行,因為多用了extra space啊
自己實作論壇裡我認為可行的方法,發現一個有趣的現象,我理解後重寫成的版本(399 ms),發現沒有原版本快,明明只差在< 和 >= ,不知道是不是跟access nums的行數有關係
class Solution(object):
def findDuplicates(self, nums):
"""
        :type nums: List[int]
        :rtype: List[int]
        """
result = [];
for x in nums:
if nums[abs(x)-1] < 0:
result.append(abs(x))
else:
nums[abs(x)-1] *= -1
return result

原版本(309 ms)
class Solution(object):
def findDuplicates(self, nums):
"""
        :type nums: List[int]
        :rtype: List[int]
        """
ret = []
for n in nums:
if nums[abs(n) - 1] >= 0:
nums[abs(n) - 1] *= -1
else:
ret.append(abs(n))
return ret
Fail:
1. Time Limit Exceeded ,list.count() complexity O( nlogn )

angledark0123 發表在 痞客邦 留言(0) 人氣()

同樣是看code學習,把同班同學好的code拿出來看,不懂的筆記在這裡
 
1. 如何有效率free vector memory
因為用clear() 本身只是把element 清掉,但是空間並不會減少,有人主張用resize,不過更好的是用 temporary vector swap 

angledark0123 發表在 痞客邦 留言(0) 人氣()

hw1 Sorting:
input.txt
----------------------
N: number of sequence
SEQ1
SEQ2

output.txt
----------------------
sorted SEQ1
sorted SEQ2

Example.
input.txt
----------------------
2
8 5 9
2 1

output.txt
----------------------
5 8 9
1 2

angledark0123 發表在 痞客邦 留言(0) 人氣()

Normally you will have to explicitly declare your own destructor if:
 
1.You are declaring a class which is supposed to serve as a base for inheritance involving polymorphism, if you do you'll need a virtual destructor to make sure that the destructor of a Derived class is called upon destroying it through a pointer/reference to Base.

angledark0123 發表在 痞客邦 留言(0) 人氣()

來記錄一下這次搞了我一點時間的message pack
http://msgpack.org
 
簡單來說就是一個更簡易binary pack 數據的方式,會依照data type,來決定怎麼包

angledark0123 發表在 痞客邦 留言(0) 人氣()

IMG_1728
首先這不是篇遊記,不算是開心那種
一個人出門的好處就是你不一定要開心,你可以就是體驗當地生活就好,生活本來就是有好有壞
而我想去過這麼多次日本,好的我自是熟悉了,不會特別地開心寫文撰寫(不過還是附上喜歡的飯店早餐的照片)
 

angledark0123 發表在 痞客邦 留言(0) 人氣()

其實在準備申請的過程,我都沒感覺到太多家人的支持
雖然說他們也不是說一定要支持我,但是當種種跡象就是他們不太為我著想的時候
難免是覺得難過又灰心
 

angledark0123 發表在 痞客邦 留言(0) 人氣()

這似乎也是很多人會遇到的問題,就是使用feof最後一行有時候會出現多讀一次的狀況
但其實問題是這樣,在最後一行後才是eof,在 C 的函式下, 只有當上一個讀取已經產生 end of file 錯誤的時候, feof() 才會傳回 true
所以在最後一行讀取後,feof 並不成立,然後直到一次read fai,eof才會被讀到 ,造成多while 一次

angledark0123 發表在 痞客邦 留言(0) 人氣()

這次作業memory 讀取使用紀錄時,因為只要用部分資料,所以查了一下用法
發現原來fscanf 可以選擇要讀什麼跳過什麼
 
這是寫比較詳細的網站,不過他用的是scanf 不過大同小異啦,只是看用什麼輸入

angledark0123 發表在 痞客邦 留言(0) 人氣()

這是之前作業在做quick sort的時候用到的,發現這種寫法很有趣
原來2d可以用這種array的array的方式做出來
 
以下出自:http://blog.xuite.net/ylps50138/web/31772356-C%2B%2B+%E7%9A%84%E5%8B%95%E6%85%8B%E9%85%8D%E7%BD%AE%E9%99%A3%E5%88%97+----+%E4%BB%A5%E4%BA%8C%E7%B6%AD%E9%99%A3%E5%88%97%E7%82%BA%E4%BE%8B

angledark0123 發表在 痞客邦 留言(0) 人氣()

因為這幾個月相繼發生英國脫歐和美國川普總統這些
某些違背常理或是毀壞正常秩序的事情發生
一開始我還會維持保守觀望態度,想說這一定是某種反動,也許應該尊重
直到剛剛突然有個想法,讓我開始思考也許可作為之後的選校參考

angledark0123 發表在 痞客邦 留言(0) 人氣()

有用到一些覺得有趣的指令所以來記錄一下
 
ipcs -m 可以看 ipc memory的部分
ipcrm -m shmid 指定砍掉shmid 

angledark0123 發表在 痞客邦 留言(0) 人氣()

Blog Stats
⚠️

成人內容提醒

本部落格內容僅限年滿十八歲者瀏覽。
若您未滿十八歲,請立即離開。

已滿十八歲者,亦請勿將內容提供給未成年人士。