What does “in-place” mean?

In programming problems, we often encounter the requirements of implementing something "in-place".

For example:

Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?

**In-place ** means that you should update the original string rather than creating a new one. Depending on the language/framework that you're using this could be impossible. For example, strings are immutable in .NET and Java, so it would be impossible to perform an in-place update of a string. In Java, character array can be used instead of string to perform string operations in place.

In-place algorithms can only use O(1) extra space, essentially. Array reversal (essentially what the interview question boils down to) is a classic example. The following is taken from Wikipedia:
Suppose we want to reverse an array of n items. One simple way to do this is:

function reverse(a[0..n])
    allocate b[0..n]
    for i from 0 to n
       b[n - i] = a[i]
    return b

Unfortunately, this requires O(n) extra space to create the array b, and allocation is often a slow operation. If we no longer need a, we can instead overwrite it with its own reversal using this in-place algorithm:

function reverse-in-place(a[0..n])
    for i from 0 to floor(n/2)
       swap(a[i], a[n-i])

Sometimes doing something in-place is VERY HARD. A classic example is general non-square matrix transposition, as well as the example problem given at the beginning of this article.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末袖瞻,一起剝皮案震驚了整個(gè)濱河市翔始,隨后出現(xiàn)的幾起案子镀梭,更是在濱河造成了極大的恐慌钦幔,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,451評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件桨啃,死亡現(xiàn)場(chǎng)離奇詭異忍燥,居然都是意外死亡墓卦,警方通過查閱死者的電腦和手機(jī)掷贾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門睛榄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人想帅,你說我怎么就攤上這事场靴。” “怎么了港准?”我有些...
    開封第一講書人閱讀 164,782評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵旨剥,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我浅缸,道長(zhǎng)轨帜,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,709評(píng)論 1 294
  • 正文 為了忘掉前任衩椒,我火速辦了婚禮蚌父,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘烟具。我一直安慰自己梢什,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評(píng)論 6 392
  • 文/花漫 我一把揭開白布朝聋。 她就那樣靜靜地躺著,像睡著了一般囤躁。 火紅的嫁衣襯著肌膚如雪冀痕。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,578評(píng)論 1 305
  • 那天狸演,我揣著相機(jī)與錄音言蛇,去河邊找鬼。 笑死宵距,一個(gè)胖子當(dāng)著我的面吹牛腊尚,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播满哪,決...
    沈念sama閱讀 40,320評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼婿斥,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼劝篷!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起民宿,我...
    開封第一講書人閱讀 39,241評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤娇妓,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后活鹰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體哈恰,經(jīng)...
    沈念sama閱讀 45,686評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評(píng)論 3 336
  • 正文 我和宋清朗相戀三年志群,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了着绷。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,992評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡锌云,死狀恐怖蓬戚,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情宾抓,我是刑警寧澤子漩,帶...
    沈念sama閱讀 35,715評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站石洗,受9級(jí)特大地震影響幢泼,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜讲衫,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評(píng)論 3 330
  • 文/蒙蒙 一缕棵、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧涉兽,春花似錦招驴、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至拥诡,卻和暖如春触趴,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背渴肉。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工冗懦, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人仇祭。 一個(gè)月前我還...
    沈念sama閱讀 48,173評(píng)論 3 370
  • 正文 我出身青樓披蕉,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子没讲,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容