HTML5技术

算法面试题 - 请叫我头头哥(2)

字号+ 作者:H5之家 来源:博客园 2016-07-08 14:00 我要评论( )

namespace TestApp{ using System; using System.Text.RegularExpressions; class Program{ static void Main( string [] args){ , , , , }; for ( int i = 0 ; i source.Length; i++ ){ for ( int j = i; j source

namespace TestApp { using System; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { , , , , }; for (int i = 0; i < source.Length; i++) { for (int j = i; j < source.Length; j++) { ).Value) > Convert.ToInt32(Regex.Match(source[j], ).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) { , , , , , , , , , }; 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博客总结

 各位道友,以上所有的算法面试题都是我平常在面试中积累下来的,出场率比较高的我都列出来了(出场率只是相对我面试的经历而言)。

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 业务订单号生成算法,每秒50W左右,不同机器保证不重复,包含日期可读性好 - 洛城秋色

    业务订单号生成算法,每秒50W左右,不同机器保证不重复,包含日期可

    2017-04-23 12:02

  • 一道面试题引发的对javascript类型转换的思考 - ChokCoco

    一道面试题引发的对javascript类型转换的思考 - ChokCoco

    2017-03-06 17:00

  • 这个发现是否会是RSA算法的BUG、或者可能存在的破解方式? - Bro__超

    这个发现是否会是RSA算法的BUG、或者可能存在的破解方式? - Bro__超

    2017-01-21 14:01

  • 让IE9以下版本的浏览支持html5,CSS3的插件 - 叫我小龙哥

    让IE9以下版本的浏览支持html5,CSS3的插件 - 叫我小龙哥

    2016-11-04 12:00

网友点评