算法
再次重申一下,这里只是列出一些最近遇到的算法面试题,拿出来给大家分享一下面试经历(仅是常见算法方面的经历,还有一些不常见的就懒得列举了,技术方向的不介绍)。还有就是我的答案。ps:无论是算法题还是答案都不具任何代表性也并非是正确答案,仅仅是我面试的时候给出的答案,只是分享而已。另外,对于我所列出的问题以及给出的答案,如果有园友有疑议或者是更好的解决办法,那就分享出来! 写算法是一个非常过瘾的享受!
顺便说一嘴,因为有一次面试面试官是在伦敦,所以只能远程面试,我们是用collabedit,这个东东还是很受用的。
v算法百科
----算法百科摘自百度百科(ps:不摘点介绍性的文字在这,直接开门见山的来题目。感觉有点duangduang的。性急的可以直接跳过此处!)
算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的清晰指令,算法代表着用系统的方法描述解决问题的策略机制。也就是说,能够对一定规范的输入,在有限时间内获得所要求的输出。如果一个算法有缺陷,或不适合于某个问题,执行这个算法将不会解决这个问题。不同的算法可能用不同的时间、空间或效率来完成同样的任务。一个算法的优劣可以用空间复杂度与时间复杂度来衡量。 算法中的指令描述的是一个计算,当其运行时能从一个初始状态和(可能为空的)初始输入开始,经过一系列有限而清晰定义的状态,最终产生输出并停止于一个终态。一个状态到另一个状态的转移不一定是确定的。随机化算法在内的一些算法,包含了一些随机输入。 形式化算法的概念部分源自尝试解决希尔伯特提出的判定问题,并在其后尝试定义有效计算性或者有效方法中成形。这些尝试包括库尔特·哥德尔、Jacques Herbrand和斯蒂芬·科尔·克莱尼分别于1930年、1934年和1935年提出的递归函数,阿隆佐·邱奇于1936年提出的λ演算,1936年Emil Leon Post的Formulation 1和艾伦·图灵1937年提出的图灵机。即使在当前,依然常有直觉想法难以定义为形式化算法的情况。
v算法题目
- 第一题:在一个已经排序的Int数组中,查找某个number,如果存在这个number,返回在数组的位置,反之返回-1 查看博主面试答案
- 第二题:{"12,bob","3,sky","6,cool","1,good","22,go"},按元素第一列排序 查看博主面试答案
- 第三题:字符串数组去除重复项 查看博主面试答案
- 第四题:将一个未排序的整形数组进行归置,数组中的负数移动到数组的左边,正数移动到数组的右边。0不动(ps:我一开做这题就打算用无脑排序搞定,结果我才开始写,面试官就说了"这里需要提示一下,移动后的数组只是负数在左,正数在右。不一定非要用传统的排序,如果不是传统的排序可以加分。") 查看博主面试答案
所有答案仅供参考,并非标准或者正确答案,只是在面试的时候给出的答案而已,所以欢迎大家给出更好的答案
所有答案都是面试的时候notepad写的,回来以后我没有double check,直接贴出了代码,其中有一部分我后面附加了图。如果大家有兴趣,我也建议大家可以先用notepad写写看,因为算法这种东西本身就是一个思路而已。没必要用vs。
v参考答案
第一题
//------------------------------------------------------------------------------
// <copyright file="Runner.cs" company="CNBlogs Corporation" owner="请叫我头头哥">
// Copyright (C) 2015-2016 All Rights Reserved
// 原博文地址: http://www.cnblogs.com/toutou/
// </copyright>
//------------------------------------------------------------------------------
namespace TestApp
{
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
class Runner
{
static void Main(string[] args)
{
int[] arr = { 3, 5, 6, 7, 8, 9, 14, 23, 45, 56, 63, 72, 87, 91, 92, 93, 95, 97, 98, 534, 555, 676, 878, 988, 1365 };
int number = 555;
int result = Search(arr, number);
Console.WriteLine(result);
Console.ReadKey();
}
public static int Search(int[] arr, int number)
{
int result = 0;
if (arr == null || arr.Length == 0 || number > arr[arr.Length - 1] || number < arr[0])
{
result = -1;
}
else
{
result = Bisearch(arr.Length - 1, arr, number);
}
return result;
}
public static int Bisearch(int endIndex, int[] arr, int number, int startIndex = 0)
{
int result = 0;
if ((endIndex - startIndex) < 2)
{
for (int i = startIndex; i <= endIndex; i++)
{
if (arr[i] == number)
{
result = i;
break;
}
else
{
result = -1;
}
}
}
else
{
if (arr[startIndex] <= number && number <= arr[(endIndex + startIndex) / 2])
{
Bisearch((endIndex + startIndex) / 2, arr, number, startIndex);
}
else
{
Bisearch(endIndex, arr, number, (endIndex + startIndex) / 2 + 1);
}
}
return result;
}
}
}

第二题
namespace TestApp
{
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main(string[] args)
{
string[] source = new string[] { "12,bob", "3,sky", "6,cool", "1,good", "22,go" };
for (int i = 0; i < source.Length; i++)
{
for (int j = i; j < source.Length; j++)
{
if (Convert.ToInt32(Regex.Match(source[i], @"\d+").Value) > Convert.ToInt32(Regex.Match(source[j], @"\d+").Value))
{
string temp = source[i];
source[i] = source[j];
source[j] = temp;
}
}
}
for (int i = 0; i < source.Length; i++)
{
Console.WriteLine(source[i]);
}
}
}
}

第三题
namespace TestApp
{
using System;
class Program
{
static void Main(string[] args)
{
string[] source = { "aaa", "bbb", "aaa", "ccc", "bbb", "ddadd", "ccc", "aaa", "bbb", "ddd" };
foreach (var item in ArrDistinct(source))
{
Console.WriteLine(item);
}
}
public static String[] ArrDistinct(string[] source)
{
if (source != null && source.Length > 0)
{
Array.Sort(source);
int size = 1;
for (int i = 1; i < source.Length; i++)
if (source[i] != source[i - 1])
size++;
string[] tempArr = new string[size];
int j = 0;
tempArr[j++] = source[0];
for (int i = 1; i < source.Length; i++)
if (source[i] != source[i - 1])
tempArr[j++] = source[i];
return tempArr;
}
return source;
}
}
}
//因为这题的主要考点在字符串数组去重,所以排序我就没太在意,直接用的系统排序。当然,这样是很不专业的。不过如果有必要的话,这里也可以先用算法排序字符串,然后再用我这个办法。 至于算法排序字符串,我能想到的比较"卑鄙"的办法就只有先将字符串数组转成char再转成int数组, Array.ConvertAll<string, int>(strArray, s => int.Parse(s)); //再排序,大神你有更好的办法?有就不要藏着掖着了,来吧!
第四题
public int[] IntArrSort(int[] source)
{
if (source == null || source.Length == 0)
return source;
int rightIndex = source.Length - 1, tempNumber = 0;
for (int i = 0; i < source.Length; i++)
{
if (i > rightIndex)
break;
if (source[i] <= 0)
{
continue;
}
else if (source[i] > 0)
{
for (int j = rightIndex; j >= 0; j--)
{
if (source[j] < 0)
{
tempNumber = source[j];
source[j] = source[i];
source[i] = tempNumber;
rightIndex = j;
break;
}
}
}
}
return source;
}

v博客总结
各位道友,以上所有的算法面试题都是我平常在面试中积累下来的,出场率比较高的我都列出来了(出场率只是相对我面试的经历而言)。
另外:特别说明一下,我给出的算法面试题答案可能也有错误的,只是给出我的参考意见,算法这种东西本身就没有什么标准答案可言,而且可能有些题目我的思路或者解题方式也不一样对。欢迎各位道友给出更好的答案或者解题思路,共同进步!

浙公网安备 33010602011771号