随笔分类 -  算法

摘要:1Two Sum25arraysortsetTwo Pointers2Add Two Numbers34linked listTwo PointersMath3Longest Substring Without Repeating Characters32stringTwo Pointershash... 阅读全文
posted @ 2015-10-29 22:29 Binyao 阅读(446) 评论(0) 推荐(0)
摘要:class Program { static void Main(string[] args) { string test = "this is test case"; //except result="esac tset... 阅读全文
posted @ 2015-10-29 20:23 Binyao 阅读(196) 评论(0) 推荐(0)
摘要://统计重复字符串 如 eeefffkkkhjk 得到如下结果 3e3f3khjk; string zipStr = Console.ReadLine(); var charList = zipStr.ToCharArray(); var listArr... 阅读全文
posted @ 2015-10-29 19:40 Binyao 阅读(246) 评论(0) 推荐(0)
摘要:class Program { static void Main(string[] args) { int[] num1 = new int[] { 3, 5, 6, 7 }; int[] num2 = new int[]... 阅读全文
posted @ 2015-10-28 16:14 Binyao 阅读(1514) 评论(0) 推荐(0)
摘要:15道算法题如下:源码下载。1:合并排序,将两个已经排序的数组合并成一个数组,其中一个数组能容下两个数组的所有元素;2:合并两个单链表;3:倒序打印一个单链表;4:给定一个单链表的头指针和一个指定节点的指针,在O(1)时间删除该节点;5:找到链表倒数第K个节点;6:反转单链表;7:通过两个栈实现一个... 阅读全文
posted @ 2015-09-25 15:29 Binyao 阅读(861) 评论(0) 推荐(0)
摘要:算法--两道百度笔试题 今天看到一位园友写了一篇关于百度的面试题的博客,成了评论头条,再下看了一下,非常感兴趣,那位博主的算法能力跟我一样需要提高,估计他的功力还在我之下,所以再下不才,在这里把自己的源码贴出来。百度面试题(一):假设一整型数组存在若干正数和负数,现在通过某种算法使得该数组的所有... 阅读全文
posted @ 2015-09-25 15:22 Binyao 阅读(781) 评论(0) 推荐(0)
摘要:这个其实没什么难度,但感觉应该还有更优的算法算法1public static void VerfiyChar1(string str) { char[] chars = str.ToCharArray(); foreach (char c in chars) { // 表示大写字母 A(65) - Z(90) if (c >= 65 && c <= 90) Console.Write(c); ... 阅读全文
posted @ 2013-06-19 17:23 Binyao 阅读(474) 评论(0) 推荐(0)
摘要:朋友问了个斐波那契算法。我给出了个递归算法public static int Foo(int n) { if (n <= 2) { return n; } ... 阅读全文
posted @ 2013-06-19 16:11 Binyao 阅读(1218) 评论(0) 推荐(0)
摘要:一个方法是递归的,另一个值返回常量值1,就是把递归中的判断改成了一个返回值始终是1的方法。View Code using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 递归算法{ class Program { static void Main(string[] args) { int num = 3; int result = Sum(num); } pro... 阅读全文
posted @ 2013-05-03 11:11 Binyao 阅读(218) 评论(0) 推荐(0)
摘要:最好的办法是用hashtable, 时间复杂度最坏a.lengh+b.lengh最差的用两个for. 时间复杂度 a*b//求两个数组的交集给你两个排序的数组,求两个数组的交集。//比如: A = 1 3 4 5 7, B = 2 3 5 8 9, 那么交集就是 3 5./*本文方法:因为数组A B... 阅读全文
posted @ 2013-05-02 16:14 Binyao 阅读(524) 评论(0) 推荐(0)
摘要:栈,先进后出,像桶一样,先放进去,最后才出来。队列,先进先出,就像管道一样,自来水管道,先进先出View Code using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;namespace StackTest{ class Program { static void Main(string[] args) { int[] iArrary = new int[] { 1, 13... 阅读全文
posted @ 2013-05-02 15:58 Binyao 阅读(238) 评论(0) 推荐(0)
摘要:需要用到ArrayList 和 StringBuilder static void Main(string[] args) { string str = "ABCDEFABCD"; string test = DeleteCom... 阅读全文
posted @ 2013-05-02 15:09 Binyao 阅读(186) 评论(0) 推荐(0)
摘要:其实主要用到Array.Sort(), ArrayList, List就可以对字符串数组或者int数组进行操作View Code using System;using System.Collections;using System.Collections.Generic;namespace InsertionSorter{ public class InsertionSorter { //List 可以直接操作,直接ToArray数组 public static string[] GetString(string[] values) { ... 阅读全文
posted @ 2013-05-02 15:03 Binyao 阅读(325) 评论(0) 推荐(0)
摘要:字符串里面的单词反转,可以用string/stringBuilderView Code using System;using System.Collections;using System.Text;namespace InsertionSorter{ public static class InsertionSorter { public static string ReverseString(string str) { string result = null; string[] s = str.Split... 阅读全文
posted @ 2013-05-02 11:16 Binyao 阅读(156) 评论(0) 推荐(0)
摘要:明,本文所有13道算法题目,覆盖了基本上所有常见的单链表问题,全都用C#实现,并测试通过,代码下载:TestLink.zip1.单链表反转2.找出单链表的倒数第4个元素3.找出单链表的中间元素4.删除无头单链表的一个节点5.两个不交叉的有序链表的合并6.有个二级单链表,其中每个元素都含有一个指向一个单链表的指针。写程序把这个二级链表称一级单链表。7.单链表交换任意两个元素(不包括表头)8.判断单链表是否有环?如何找到环的“起始”点?如何知道环的长度?9.判断两个单链表是否相交10.两个单链表相交,计算相交点11.用链表模拟大整数加法运算12.单链表排序13.删除单链表中重复的元素首先写一个单链 阅读全文
posted @ 2013-04-30 13:46 Binyao 阅读(321) 评论(0) 推荐(0)
摘要:1. 数组在内存中栈上按顺序存储的,而链表是在堆上随机存储的。2. 要访问数组中的元素可以按下标索引来访问,速度比较快,如果对他进行插入操作的话,就得移动很多元素,所以对数组进行插入操作效率很低. 由于连表是随机存储的,链表在插入,删除操作上有很高的效率(相对数组)3. 如果要访问链表中的某个元素的话,那就得从链表的头逐个遍历,直到找到所需要的元素为止,所以链表的随机访问的效率就比数组要低 。 阅读全文
posted @ 2013-04-30 13:39 Binyao 阅读(623) 评论(1) 推荐(0)
摘要:算法题(说明:这些题就不是什么花样了,考的是你的基础知识怎么样。再聪明而没有实学的人都将会被这些题所淘汰。)1.链表和数组的区别在哪里?2.编写实现链表排序的一种算法。说明为什么你会选择用这样的方法?3.编写实现数组排序的一种算法。说明为什么你会选择用这样的方法?4.请编写能直接实现strstr()函数功能的代码。5.编写反转字符串的程序,要求优化速度、优化空间。6.在链表里如何发现循环链接?7.给出洗牌的一个算法,并将洗好的牌存储在一个整形数组里。8.写一个函数,检查字符是否是整数,如果是,返回其整数值。(或者:怎样只用4行代码编写出一个从字符串到长整形的函数?)9.给出一个函数来输出一个字 阅读全文
posted @ 2013-04-30 13:31 Binyao 阅读(233) 评论(0) 推荐(0)
摘要:假设这样的一个场景,有两个List<string>,分别是listA和listB,其中均有超过1w+的数据量,现在要求找出在B中但是不在A中的数据集合,于是我们写了以下代码View Code listB.ForEach(re => { if (!listA.Contains(re)) { //....... } });这样写的话,是能够达到我们的需求的,但是很不幸,时间也会消耗很多。我们看下List.Contai... 阅读全文
posted @ 2013-04-29 23:39 Binyao 阅读(2430) 评论(0) 推荐(0)
摘要:static void Main(string[] args) { string s = "this is a test"; Dictionary dict = GetCharCount(s); //foreach (... 阅读全文
posted @ 2013-04-29 22:50 Binyao 阅读(144) 评论(0) 推荐(0)
摘要:设要排序的数组是A[0]……A[N-1],首先任意选取一个数据(通常选用第一个数据)作为关键数据,然后将所有比它小的数都放到它前面,所有比它大的数都放到它后面,这个过程称为一趟快速排序。值得注意的是,快速排序不是一种稳定的排序算法,也就是说,多个相同的值的相对位置也许会在算法结束时产生变动。优点:极快,数据移动少;O(nlog n) 期望时间,O(n^2) 最坏情况 缺点:不稳定。 View Code using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 快速排序{ 阅读全文
posted @ 2013-04-29 21:50 Binyao 阅读(239) 评论(0) 推荐(0)