Coding Interviews 2 Array

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;
}
时间: 2024-08-06 12:02:26

Coding Interviews 2 Array的相关文章

Coding Interviews 3 Linked List

Linked list is a one of the most important topic in interviews. Because it uses dynamic memory and short lines can finish its implement. It is often used as test question. Here is the structure of a node of linked list typedef struct Node { int val;

Coding Interviews 1

The first day. chapter 1&2 Implement assignment function for below class CMyString. class CMyString { public: CMyString(char* pData = NULL); CMyString(const CMyString& str); ~CMyString(void); private: char* m_pData; }; Before coding I should focus

Solid-state storage management

Solid-state storage management for a system, the management including establishing, externally to a solid-state storage board, a correspondence between a first logical address and a first physical address on solid-state storage devices located on the

提高Python性能的一些建议(一)

最近换住的地方,网费到期,有两个星期没更新博客了,博客还是要坚持写的,有时候工作时遇到了相关问题,查看相关博客,还是能够得到一些思路或者灵感.虽然写篇博客要话费不少时间(我一般要花一个半小时到两个小时之间),但是这中间码字呀.归纳总结的过程还是让我受益匪浅的,温故而知新!当然分享自己的学习心得,也会让自己认识一些志同道合的朋友,也挺好.不说许多,今天讲讲如何提高Python性能的问题. python的性能相对c语言等还是有一定的劣势,但是如果能掌握一些优化性能的技巧,不仅能够提高代码的运行效率,

国内有哪些好的刷题网站?

http://www.zhihu.com/question/25574458 Luau Lawrence,Data Mining 弱鸡 / [email protected] 温梦强.石一帆.知乎用户 等人赞同 - Welcome To PKU JudgeOnline 北京大学的Online Judge.POJ上面的题目有点老了,但好处是做的人多,经典算法题多,解题报告也多,适合上手.- ZOJ :: Home 浙江大学的Online Judge.ZOJ用的不多,但为数不多的几次体验好像都还可以

[it-ebooks]电子书列表

#### it-ebooks电子书质量不错,但搜索功能不是很好 #### 格式说明  [ ]中为年份      ||  前后是标题和副标题  #### [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Objective-C to develop iPhone games http://it-ebooks.info/book/3544/ Learning Web App Developmen

使用python的tornado配合html页面示例

背景:java写的非标加密算法,测试时执行java工程进行解密测试,很不方便. 目的:想写个web页面,使得任何测试人员都可以在输入加密串时得到解密后字段,方便日志查询及字段核对.(额,算法部分就不写了.) 步骤:由于内部使用的是非标Base64加密算法,所以我该次操作分为3个部分,以下具体介绍并附带python代码 1 将java的非标加密算法转为python 2 使用python的tornado调用该算法 3 编写html页面,简单明了 一 java非标加密算法转为python  dncry

数据结构与算法复习-----leetcodeOJ题解

Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the or

leetcode学习笔记--开篇

1 LeetCode是什么? LeetCode是一个在线的编程测试平台,国内也有类似的Online Judge平台.程序开发人员可以通过在线刷题,提高对于算法和数据结构的理解能力,夯实自己的编程基础.对于找工作的小伙伴十分有好处. 这是leetcode官网的简介: LeetCode OJ is a platform for preparing technical coding interviews. Pick from an expanding library of more than 190