随笔分类 -  Algrorithms

1
Sort
摘要:--heap sort . O(n*logn). auxiliary space O(1). Unstable sorting. class ArraySort { static void Main(string[] args) { //give up 0 index. get/set from 1 so that getting right index when do j=2*i. int[] a = new int[11] {0,5,77,1,61,11,59,15,48,47,25 }; ... 阅读全文
posted @ 2013-06-16 17:43 Alan Yang 阅读(186) 评论(0) 推荐(0)
k largest(or smallest) elements in an array | added Min Heap method
摘要:http://www.geeksforgeeks.org/k-largestor-smallest-elements-in-an-array/k largest(or smallest) elements in an arrayMarch 1, 2010Question:Write an efficient program for printing k largest elements in an array. Elements in array can be in any order.For example, if given array is [1, 23, 12, 9, 30, 2, 5 阅读全文
posted @ 2013-06-14 21:56 Alan Yang 阅读(361) 评论(0) 推荐(0)
将递归转化成迭代的通用技术
摘要:http://blog.csdn.net/whinah/article/details/6419680从理论上讲,只要允许使用栈,所有的递归程序都可以转化成迭代。但是并非所有递归都必须用栈,不用堆栈也可以转化成迭代的,大致有两类尾递归:可以通过简单的变换,让递归作为最后一条语句,并且仅此一个递归调用。// recursiveint fac1(int n) { if (n <= 0) return 1; return n * fac1(n-1);}// iterativeint fac2(int n) { int i = 0, y = 1; for (; i <= n; ... 阅读全文
posted @ 2013-06-13 17:08 Alan Yang 阅读(1060) 评论(0) 推荐(1)
A child is running up a staircase with N steps, and can hop either 1 step,2steps,3 steps at a time. Count how many possible ways the child can run up
摘要:A child is running up a staircase with N steps, and can hop either 1 step,2steps,3 steps at a time. Count how many possible ways the child can run up the stairs. class Upstaircase { static void Main(string[] args) { /* Enter your code here. Read input from STDIN. Print ou... 阅读全文
posted @ 2013-06-13 00:54 Alan Yang 阅读(370) 评论(0) 推荐(0)
Find whether an array is subset of another array
摘要:http://www.geeksforgeeks.org/find-whether-an-array-is-subset-of-another-array-set-1/Given two arrays: arr1[0..m-1] and arr2[0..n-1]. Find whether arr2[] is a subset of arr1[] or not. Both the arrays are not in sorted order.Examples:Input: arr1[] = {11, 1, 13, 21, 3, 7}, arr2[] = {11, 3, 7, 1}Output: 阅读全文
posted @ 2013-06-09 16:34 Alan Yang 阅读(321) 评论(0) 推荐(0)
print all permutations of a given string
摘要:http://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/A permutation, also called an “arrangement number” or “order,” is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation.So 阅读全文
posted @ 2013-06-07 16:40 Alan Yang 阅读(483) 评论(0) 推荐(0)
Write your own atoi()
摘要:heatoi()function takes a string (which represents an integer) as an argument and returns its value.Following is a simple implementation. We initialize result as 0. We start from the first character and update result for every character.// A simple C++ program for implementation of atoi#include <s 阅读全文
posted @ 2013-06-07 14:38 Alan Yang 阅读(192) 评论(0) 推荐(0)
Convert Binary Search Tree (BST) to Sorted Doubly-Linked List -- leap of faith with recursion.
摘要:1st Solution:When I first see this problem, my first thought was in-order traversal. Couldn’t we modify the nodes’ left and right pointers as we do an in-order traversal of the tree? However, we have to beware not to modify the pointers and accessing it at a later time.As we traverse the tree in-ord 阅读全文
posted @ 2013-05-29 16:18 Alan Yang 阅读(530) 评论(1) 推荐(0)
Become A Better Developer By Indexing Your Brain
摘要:From: http://www.skorks.com/2009/09/become-a-better-developer-by-indexing-your-brain/ By Alan SkorkinThis is the fourth post in the teaching and learning series. The teaching and learning series includes the following posts:All Developers Should Know How They Learn BestThe Secret Of Being A Great M. 阅读全文
posted @ 2013-05-27 18:37 Alan Yang 阅读(216) 评论(0) 推荐(0)
convert sorted array/list to a height balanced BST.
摘要:Given an array where elements are sorted in ascending order, convert it to a height balanced BST./*** Definition for binary tree* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/public class Solution {public TreeNode sortedArrayToBST(int[] num) {/ 阅读全文
posted @ 2013-05-21 17:52 Alan Yang 阅读(311) 评论(0) 推荐(0)
Two sum.
摘要:Given an array A[] and a number x, check for pair in A[] with sum as xhttp://www.geeksforgeeks.org/write-a-c-program-that-given-a-set-a-of-n-numbers-and-another-number-x-determines-whether-or-not-there-exist-two-elements-in-s-whose-sum-is-exactly-x/ public int[] twoSum(int[] numbers, int target) ... 阅读全文
posted @ 2013-05-20 17:22 Alan Yang 阅读(225) 评论(1) 推荐(0)
Palindrome
摘要:Determine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to string, note the restriction of using extra space.You could also try reversing an integer. However, if you have solved 阅读全文
posted @ 2013-05-20 14:45 Alan Yang 阅读(172) 评论(0) 推荐(0)
Best Time to Buy and Sell Stock
摘要:Best Time to Buy and Sell StockSay you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.//Time complexity O(n), auxi 阅读全文
posted @ 2013-05-17 16:22 Alan Yang 阅读(168) 评论(2) 推荐(0)
Maximum difference between two elements
摘要:Maximum difference between two elementsFrom http://www.geeksforgeeks.org/maximum-difference-between-two-elements/April 10, 2010Given an array arr[] of integers, find out the difference between any two elementssuch that larger element appears after the smaller number in arr[].Examples: If array is [2 阅读全文
posted @ 2013-05-17 14:58 Alan Yang 阅读(293) 评论(0) 推荐(0)
Matrix issue
摘要:Search a 2D MatrixWrite an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each row is greater than the last integer of the previous row.For example,Consider the following m 阅读全文
posted @ 2013-05-17 00:18 Alan Yang 阅读(326) 评论(0) 推荐(0)
二分查找法的实现和应用汇总
摘要:From:http://www.cnblogs.com/ider/archive/2012/04/01/binary_search.html在学习算法的过程中,我们除了要了解某个算法的基本原理、实现方式,更重要的一个环节是利用big-O理论来分析算法的复杂度。在时间复杂度和空间复杂度之间,我们又会更注重时间复杂度。时间复杂度按优劣排差不多集中在:O(1), O(log n), O(n), O(n log n), O(n2), O(nk), O(2n)到目前位置,似乎我学到的算法中,时间复杂度是O(log n),好像就数二分查找法,其他的诸如排序算法都是 O(n log n)或者O(n2)。但是 阅读全文
posted @ 2013-03-26 18:17 Alan Yang 阅读(234) 评论(0) 推荐(0)
组合与组合数
摘要:组合与组合数高中内容重新学习:http://stu.bdchina.com/ziyuan/14/7/2/8ed04be2-8ccb-45ff-bc87-0427189a5bf7.doc 阅读全文
posted @ 2010-04-06 16:49 Alan Yang 阅读(482) 评论(1) 推荐(0)
[转]Levenshtein Distance(LD)-计算两字符串相似度算法
摘要:两字符串相似度计算方法有好多,现对基于编距的算法的相似度计算自己总结下。 简单介绍下Levenshtein Distance(LD):LD 可能衡量两字符串的相似性。它们的距离就是一个字符串转换成那一个字符串过程中的添加、删除、修改数值。 举例:如果str1="test",str2="test",那么LD(str1,str2) = 0。没有经过转换。 如果str1="test",str2="te... 阅读全文
posted @ 2010-03-24 14:12 Alan Yang 阅读(600) 评论(0) 推荐(0)
[转]彻底搞定C指针-函数名与函数指针
摘要:彻底搞定C指针-函数名与函数指针函数名与函数指针一 通常的函数调用一个通常的函数调用的例子://自行包含头文件void MyFun(int x);//此处的申明也可写成:void MyFun( int );int main(int argc, char* argv[]){MyFun(10);//这里是调用MyFun(10);函数return 0;}void MyFun(int x)//这里定义一个... 阅读全文
posted @ 2010-01-21 23:56 Alan Yang 阅读(346) 评论(0) 推荐(0)
排序算法分析与设计实验
摘要:实验内容分别针对随机生成的三组整数序列(规模为1000个数、10000个数、100000个数)进行排序,排序算法使用以下五种经典的方法,分别是:冒泡排序算法,选择排序算法,插入排序算法,归并排序算法和快速排序算法。实验目的• 回顾并熟悉常用的排序算法。• 通过实验体会算法设计对问题求解效率所产生的深刻影响。算法设计的基本思路 选择排序 在要排序的一组数中,选出最小的一个数与... 阅读全文
posted @ 2009-12-14 20:47 Alan Yang 阅读(824) 评论(1) 推荐(0)

1