随笔分类 -  Interview Algorithm

摘要:Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or ... 阅读全文
posted @ 2015-01-11 20:11 Acjx 阅读(471) 评论(0) 推荐(0)
摘要:Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. ... 阅读全文
posted @ 2015-01-09 21:03 Acjx 阅读(303) 评论(0) 推荐(0)
摘要:Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1 / \ 2 3 Return 6. 题目意思很简单,就是给定一棵二叉... 阅读全文
posted @ 2015-01-06 15:41 Acjx 阅读(539) 评论(0) 推荐(0)
摘要:Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination... 阅读全文
posted @ 2014-12-28 20:08 Acjx 阅读(357) 评论(0) 推荐(0)
摘要:Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "2... 阅读全文
posted @ 2014-12-28 15:07 Acjx 阅读(399) 评论(0) 推荐(0)
摘要:Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number o... 阅读全文
posted @ 2014-12-28 12:30 Acjx 阅读(427) 评论(0) 推荐(0)
摘要:Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 以下是我... 阅读全文
posted @ 2014-12-24 21:27 Acjx 阅读(511) 评论(0) 推荐(0)
摘要:1 定义 二叉树是树的一种特殊结构,在二叉树中每个结点最多只能有两个子结点。 二叉树结点的定义如下: struct BinaryTreeNode { int m_nValue; BinaryTreeNode *m_pLeft; BinaryTreeNode *m_pRight; }; 注意:在二叉树的前序遍历序列中,第一个数字总是... 阅读全文
posted @ 2014-11-18 15:39 Acjx 阅读(1468) 评论(0) 推荐(0)
摘要:需求 编程实现计算器,当输入一个表达式时,可以得出计算结果。(实现加、减、乘、除、取余以及负号运算) 思路 1. 维护两个栈,一个栈my_dig用于push数字,另一个栈my_op用于push运算符。栈中元素结构如下: typedef struct tag_stack1 { int dig_arr[1024]; int dig_top; }DIG, *pDIG; typedef ... 阅读全文
posted @ 2014-09-11 14:27 Acjx 阅读(1565) 评论(0) 推荐(0)
摘要:1. 快排 详见之前博文快速排序算法。 2. 堆排序 详见之前博文非递归方法的堆排序实现。 3. 简单排序(冒泡排序、选择排序和插入排序) 代码如下: #include #include #include #define N 20static void show(int *arr, int len){ int index; for(index = 0; index 前N-1个数放好位置... 阅读全文
posted @ 2014-09-05 12:42 Acjx 阅读(304) 评论(0) 推荐(0)
摘要:需求 从一亿个数据中,找出其中最小的10个数。 分析 最笨的方法就是将这一亿个数据,按从小到大进行排序,然后取前10个。这样的话,即使使用时间复杂度为nlogn的快排或堆排,由于元素会频繁的移动,效率也不会是最高的。 实际上我们可以维护一个大小为10的大顶堆,开始可以就将数列中的前10个数用来建堆,根元素最大。之后遍历剩余的数,分别将其与根元素进行比较,只要小于根元素,就将该数替代原来的根元素,成... 阅读全文
posted @ 2014-09-03 21:07 Acjx 阅读(734) 评论(1) 推荐(0)
摘要:引言 首先需要明确,如何根据父亲结点的位置得知孩子结点的位置,以及如何根据孩子结点的位置得知父亲结点的位置。 假设数列索引从0开始,如果父亲结点的索引为i,那么左孩子索引为2i+1,右孩子索引为2i+2;如果孩子结点的索引为j,那么父亲结点的索引为(j-1)/2。 堆排序的核心在于函数 void adjustdown(int *arr, int i, int end) ,其中第i+1个元素到最后一... 阅读全文
posted @ 2014-09-03 16:50 Acjx 阅读(2100) 评论(0) 推荐(1)
摘要:引言 快排采用分治法(Divide and Conquer)把一个list分为两个sub-lists。 算法步骤 1. 从数列中跳出一个元素,作为基准(pivot)。 2. 重新排序数列,所有比基准值小的元素(elements pivot)放在基准值后面,与基准值相等的数可以放在任意一边。此操作即为分区(partition)操作。 3. 递归地把小于基准值元素的子数列和大于基准值元素的子数列进... 阅读全文
posted @ 2014-09-02 21:07 Acjx 阅读(391) 评论(0) 推荐(0)
摘要:思路 先将字符串中的单词分割保存至二维数组中,再经排序后输出。水题,直接上代码了。 代码 /************************************************************************* > File Name: words_sort.c > Author: KrisChou > Mail:zhoujx0219@163.com... 阅读全文
posted @ 2014-08-24 21:45 Acjx 阅读(450) 评论(0) 推荐(0)
摘要:思路 每次内部循环需要找到一个单词,将其存入数组。外循环遍历至字符串末尾结束。 代码 /************************************************************************* > File Name: word_split.c > Author: KrisChou > Mail:zhoujx0219@163.com ... 阅读全文
posted @ 2014-08-24 12:42 Acjx 阅读(1332) 评论(0) 推荐(0)
摘要:题目很水,我想代码应该就是最好的注释,实现如下: /************************************************************************* > File Name: trim_space.c > Author: KrisChou > Mail:zhoujx0219@163.com > Created Time: ... 阅读全文
posted @ 2014-08-24 10:28 Acjx 阅读(922) 评论(0) 推荐(0)
摘要:原题 设任意n个整数存放于数组A中,编写程序,将所有整数排在所有负数前面。 思路 题目超级水,直接上代码了。 实现代码 /************************************************************************* > File Name: testmain.c > Author: KrisChou > Mail:zhoujx... 阅读全文
posted @ 2014-08-18 01:06 Acjx 阅读(850) 评论(0) 推荐(0)
摘要:原题 N个人围成一个圆圈,首先第一个人从1开始一个人一个人顺时针报数,报道第m个人,令其出列。然后再从下一个人开始,从1顺时针报数,报到第m个人,再令其出列,…如此下去,直到圈中只剩下一个人为止。此人即为优胜者。写一个函数求N个人中的胜者。 我的思路 可以使用一数组,来存放标记为1,2,3,…,N的N个人,每当有人出列,对应数组元素置为0,下次报数遇到置0的数组元素直接跳过。 实现代码 /****... 阅读全文
posted @ 2014-08-17 23:45 Acjx 阅读(246) 评论(0) 推荐(0)
摘要:原题 打印魔方阵,魔方阵是指这样的方针,每一行、每一列以及对角线的和相等。例如三阶魔方阵: 8 1 6 3 5 7 4 9 2 编程打印奇数阶魔方阵。 提示 问题解决的关键是元素的填充,第一个元素1的位置在第一行正中,新的位置应该处于最近插入元素的右上方;但如果右上方的位置超出方针上边界,则新的位置应该取列的最下一个位置;超出右边界则取行的最左的一个位置;若最近插入的元素为n的整数倍,则选下面一行... 阅读全文
posted @ 2014-08-17 22:30 Acjx 阅读(1266) 评论(0) 推荐(0)
摘要:原题 自然数采用蛇形排列方式填充到数组中。将自然数1、2、3…、N*N逐个顺序插入方阵中适当的位置,这个过程沿斜列进行。将斜列编号为0、1、2…、2n(以i标记,n=N-1),如下面的数据排列,这个排列为蛇形排列。 1 3 4 10 2 5 9 11 6 8 12 15 7 13 14 16 我的思路 观察以下坐标: (0,0) 和为0 (1,0) (0,1) 和为1 (0,2) (1,1... 阅读全文
posted @ 2014-08-17 21:27 Acjx 阅读(3526) 评论(0) 推荐(0)