Coding Interviews 2 Array

來源:互聯網
上載者:User

標籤:

Data Structure is the most important aspect in interviews. Most questions are on array, string, linked list, tree, stack, queue.

Array and string are two basic data structure. They are both continuous memory. Linked list and tree have high frequency occurrence in interviews. Since pointer is used very often, robust should be taken care of. Stack and recursion always stay together.

Array

Space Efficiency is bad, some spare room is not efficiently used.

O(1) read/write any element. Time Efficiency is good. We can make a simple hash table by setting the index of array to key. The value of every element become value of hash table.

Vector is a dynamic array in C++ STL. Vector extend its volume by getting double space, putting the old data to new vector and releasing the previous memory, Which is bad to time efficiency. We try our best to reduce times of change of volume.

When array is used as a parameter, then it becomes a pointer whose size is 4 bytes on 32 bits OS.

Initialization:

one dimension

int foo [5] = { 16, 2, 77, 40, 12071 }; int bar [5] = { 10, 20, 30 };int baz [5] = { };int foo [] = { 16, 2, 77, 40, 12071 };int foo[] = { 10, 20, 30 };int foo[] { 10, 20, 30 };

Multidimensional Arrays

int jimmy [3][5];int array[3][5] ={{ 1, 2, 3, 4, 5, }, // row 0{ 6, 7, 8, 9, 10, }, // row 1{ 11, 12, 13, 14, 15 } // row 2};int array[][5] ={{ 1, 2, 3, 4, 5, },{ 6, 7, 8, 9, 10, },{ 11, 12, 13, 14, 15 }};int array[][] = {{ 1, 2, 3, 4 },{ 5, 6, 7, 8 }};int array[3][5] = { 0 };

space replacement

int replaceBlank(char string[], int length) {    if (string == NULL || length <= 0)         return 0;    int spaceCount = 0;    int i = 0;    while (char c=string[i]){        if (c == ‘ ‘) spaceCount += 2;        i++;    }    int lastIdx = length + spaceCount - 1;    for (int i = length - 1; i >= 0; i--){        if (string[i] == ‘ ‘){            string[lastIdx--] = ‘0‘;            string[lastIdx--] = ‘2‘;            string[lastIdx--] = ‘%‘;        }        else{            string[lastIdx--] = string[i];        }    }    return length + spaceCount;}

Coding Interviews 2 Array

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.