教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 实用文档 >

程序员面试题精选100题完整版(2)

来源:网络收集 时间:2026-03-11
导读: { } // get the last element of non-mutable stack template typename T const T CStackWithMinT::top() const { } // insert an elment at the end of stack template typename T void CStackWithMinT::push(cons

{

}

// get the last element of non-mutable stack

template <typename T> const T& CStackWithMin<T>::top() const

{

}

// insert an elment at the end of stack

template <typename T> void CStackWithMin<T>::push(const T& value) {

}

// erease the element at the end of stack

template <typename T> void CStackWithMin<T>::pop()

{

}

// get the minimum element of stack

template <typename T> const T& CStackWithMin<T>::min() const

{

assert(m_data.size() > 0); assert(m_minIndex.size() > 0); // pop m_minIndex m_minIndex.pop_back(); // pop m_data m_data.pop_back(); // set the index of minimum elment in m_data at the end of m_minIndex if(m_minIndex.size() == 0) { } if(value < m_data[m_minIndex.back()]) m_minIndex.push_back(m_data.size() - 1); m_minIndex.push_back(m_minIndex.back()); else m_minIndex.push_back(0); else // append the data into the end of m_data m_data.push_back(value); return m_data.back(); return m_data.back();

很全面,很经典的程序员面试题精选100题,包括网络编程,socket编程,c++/c编程程序员面试题,linux/windows网络编程面试必备。

} return m_data[m_minIndex.back()];

举个例子演示上述代码的运行过程:

步骤 数据栈 辅助栈 最小值

1.push 3 3 0 3

2.push 4 3,4 0,0 3

3.push 2 3,4,2 0,0,2 2

4.push 1 3,4,2,1 0,0,2,3 1

5.pop 3,4,2 0,0,2 2

6.pop 3,4 0,0 3

7.push 0 3,4,0 0,0,2 0

讨论:如果思路正确,编写上述代码不是一件很难的事情。但如果能注意一些细节无疑能在面试中加分。比如我在上面的代码中做了如下的工作:

用模板类实现。如果别人的元素类型只是int类型,模板将能给面试官带来好印象; 两个版本的top函数。在很多类中,都需要提供const和非const版本的成员访问函数;

min函数中assert。把代码写的尽量安全是每个软件公司对程序员的要求; 添加一些注释。注释既能提高代码的可读性,又能增加代码量,何乐而不为?

总之,在面试时如果时间允许,尽量把代码写的漂亮一些。说不定代码中的几个小亮点就能让自己轻松拿到心仪的Offer。

(03)-求子数组的最大和

题目:输入一个整形数组,数组里有正数也有负数。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。求所有子数组的和的最大值。要求时间复杂度为O(n)。 例如输入的数组为1, -2, 3, 10, -4, 7, 2, -5,和最大的子数组为3, 10, -4, 7, 2,因此输出为该子数组的和18。

分析:本题最初为2005年浙江大学计算机系的考研题的最后一道程序设计题,在2006年里包括google在内的很多知名公司都把本题当作面试题。由于本题在网络中广为流传,本题也顺利成为2006年程序员面试题中经典中的经典。

如果不考虑时间复杂度,我们可以枚举出所有子数组并求出他们的和。不过非常遗憾的是,由于长度为n的数组有O(n2)个子数组;而且求一个长度为n的数组的和的时间复杂度为O(n)。因此这种思路的时间是O(n3)。

很容易理解,当我们加上一个正数时,和会增加;当我们加上一个负数时,和会减少。如果当前得到的和是个负数,那么这个和在接下来的累加中应该抛弃并重新清零,不然的话这个负数将会减少接下来的和。基于这样的思路,我们可以写出如下代码。

参考代码:

/////////////////////////////////////////////////////////////////////////////

// Find the greatest sum of all sub-arrays

// Return value: if the input is valid, return true, otherwise return false

很全面,很经典的程序员面试题精选100题,包括网络编程,socket编程,c++/c编程程序员面试题,linux/windows网络编程面试必备。

/////////////////////////////////////////////////////////////////////////////

bool FindGreatestSumOfSubArray

(

)

{

} return true; // if all data are negative, find the greatest element in the array if(nGreatestSum == 0) { } nGreatestSum = pData[0]; for(unsigned int i = 1; i < nLength; ++i) { } if(pData[i] > nGreatestSum) nGreatestSum = pData[i]; } // if a greater sum is found, update the greatest sum if(nCurSum > nGreatestSum) nGreatestSum = nCurSum; // if the current sum is negative, discard it if(nCurSum < 0) nCurSum = 0; int nCurSum = nGreatestSum = 0; for(unsigned int i = 0; i < nLength; ++i) { nCurSum += pData[i]; // if the input is invalid, return false if((pData == NULL) || (nLength == 0)) return false; int *pData, // an array unsigned int nLength, // the length of array int &nGreatestSum // the greatest sum of all sub-arrays

讨论:上述代码中有两点值得和大家讨论一下:

函数的返回值不是子数组和的最大值,而是一个判断输入是否有效的标志。如果函数返回值的是子数组和的最大值,那么当输入一个空指针是应该返回什么呢?返回0?那这个函数的用户怎么区分输入无效和子数组和的最大值刚好是0这两中情况呢?基于这个考

很全面,很经典的程序员面试题精选100题,包括网络编程,socket编程,c++/c编程程序员面试题,linux/windows网络编程面试必备。

虑,本人认为把子数组和的最大值以引用的方式放到参数列表中,同时让函数返回一个函数是否正常执行的标志。

输入有一类特殊情况需要特殊处理。当输入数组中所有整数都是负数时,子数组和的最大值就是数组中的最大元素。

(04)-在二元树中找出和为某一值的所有路径

题目:输入一个整数和一棵二元树。从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。打印出和与输入整数相等的所有路径。

例如输入整数22和如下二元树

10

/ \

5 12

/ \

4 7

则打印出两条路径:10, 12和10, 5, 7。

二元树结点的数据结构定义为:

struct BinaryTreeNode // a node in the binary tree

{

}; int m_nValue; // value of node BinaryTreeNode *m_pLeft; // left child of node BinaryTreeNode *m_pRight; // right child of node

分析:这是百度的一道笔试题,考查对树这种基本数据结构以及递归函数的理解。

当访问到某一结点时,把该结点添加到路径上,并累加当前结点的值。如果当前结点为 …… 此处隐藏:4272字,全部文档内容请下载后查看。喜欢就下载吧 ……

程序员面试题精选100题完整版(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/133659.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)