摘要: 方法是把数组中的数按2个2个的分成一组,先对比他们俩,然后再对比MAX和MIN。最后再加个判断,如果是奇数,那最后一个数字也要和MAX和MIN判断。using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication4{ class Program { static void Main(string[] args) { int[] input = { 1,2,-1,3,55,1}; ... 阅读全文
posted @ 2014-02-04 08:38 Ligeance 阅读(504) 评论(0) 推荐(0) 编辑
摘要: 有一个无限大的棋盘,棋盘上有一匹马,马移动的方式为日字型。即假设马当前的位置为(x,y),那么下一步可以移动到(x+1,y+2),(x+1,y-2),(x-1,y+2),(x-1,y-2),(x+2,y+1),(x+2,y-1),(x-2,y+1)或者(x-2,y-1)这8个位置。问马是否能从坐标(x,y)按照上述移动规则移动到坐标(x2,y2)。using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication9{ class Pro... 阅读全文
posted @ 2014-01-28 16:12 Ligeance 阅读(328) 评论(0) 推荐(0) 编辑
摘要: 8. 有一个包含n个元素的数组arr,计算最大的子段和(允许空段),即。9.有一个包含n个元素的首尾相连的环形数组arr,计算最大的子段和(允许空段)。样例:数组[1, 3, -2, 6, -1],最大子段和应该为9,对应的子段为[6, -1, 1, 3]。using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication8{ class Program { static void Main(string[] ar... 阅读全文
posted @ 2014-01-28 15:14 Ligeance 阅读(579) 评论(0) 推荐(0) 编辑
摘要: 题目:有一颗树,给定树中任意两个结点,计算出这两个结点的最近公共祖先(查看定义)。树结点的定义为(请不要在代码中再次定义该结构):C/C++struct TreeNode { TreeNode *parent;}Javapublic class TreeNode { public TreeNode parent;}树结点只包含父结点指针(父结点指针为null表示该结点为根结点),该题存在空间复杂度O(1),时间复杂度低于O(n)的简单算法。View Code 代码:using System;using System.Collections.Generic;using System.... 阅读全文
posted @ 2014-01-09 08:29 Ligeance 阅读(167) 评论(0) 推荐(0) 编辑
摘要: 题目:写程序判断一个9*9的数字盘面是否为合法的数独(查看定义)。9*9的盘面按照Row-major order表示为一个81维的一维数组。提示:请直接在一维数组上操作,不要先将一维数组拷贝到9*9的二维数组。View Code Code:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;namespace Sudoku{ class Program { static void Main(string[] ... 阅读全文
posted @ 2013-12-27 06:14 Ligeance 阅读(495) 评论(0) 推荐(0) 编辑
摘要: 此题在2008年Google暑期实习生面试中出现过。有一个环形公路上有n个加油站,第i个加油站的油量为ai。假设有一辆邮箱体积无穷大的汽车,初始邮箱是空的,汽车从加油站i行驶到加油站i+1需耗油g[i]。问是否能够选出某个加油站作为起点,使汽车能够绕环形公路行驶一圈返回到该加油站。实现函数int selectGasStation(int a[], int g[], int n),如果存在满足条件的加油站,返回该加油站的序号(0-based)。否则返回-1。提示:n可能达到106,O(n2)的枚举算法会超出时间限制。因为是环形公路,把这个展开成线性数组,也就是两个重复的数组排一起。看了别人的提示 阅读全文
posted @ 2013-10-30 13:18 Ligeance 阅读(284) 评论(0) 推荐(0) 编辑
摘要: 有一个包含n个整数的数组arr,请计算出数组中所有元素之和。(假设中间结果以及最终结果都不会超出32位有符号整型的范围) static int SumOfArray(int[] intArr) { if (intArr.Length == 0 || intArr == null) { throw new Exception("Input Array can not be empty"); } int result = 0; fo... 阅读全文
posted @ 2013-10-29 07:50 Ligeance 阅读(225) 评论(0) 推荐(0) 编辑
摘要: 希望每天最少一题,一个月内完成50题。COME ON! 阅读全文
posted @ 2013-10-29 07:44 Ligeance 阅读(125) 评论(0) 推荐(0) 编辑
摘要: 例如有一个字符串“Google Nexus 4 mobile phone”,你要搜索“Google”和“Nexus”,那么应该返回TRUE。 static void Main(string[] args) { string[] strArr = { "Nexus","Google"}; string str = "Google Nexus 4 mobile phone"; Console.Write(MatchKeyWords(str, strArr)); } static... 阅读全文
posted @ 2013-06-21 07:07 Ligeance 阅读(638) 评论(0) 推荐(0) 编辑
摘要: 一副扑克有52张牌,那么使用数组存储这52张牌,然后打乱这些牌。using System;using System.Collections.Generic;using System.Collections;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Poker{ class Program { static void Main(string[] args) { ArrayList al = new ArrayList(); ... 阅读全文
posted @ 2013-06-13 06:57 Ligeance 阅读(365) 评论(0) 推荐(0) 编辑