
算法与面试
文章平均质量分 71
fanoICT
找工作、写软件、写文章,2014加油!
展开
-
[算法]Fibonacci数列O(n)和O(lgn)的解法
九度oj题目1387:斐波那契数列大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。斐波那契数列的定义如下:输入:输入可能包含多个测试样例,对于每个测试案例,输入包括一个整数n(1输出:对应每个测试案例,输出第n项斐波那契数列的值。样例输入:3样例输出:2原创 2014-09-09 22:24:48 · 1886 阅读 · 0 评论 -
[leetcode] Implement Trie (Prefix Tree)
Problem Description:Implement a trie with insert, search, and startsWith methods.Note:You may assume that all inputs are consist of lowercase letters a-z.继续我的每天水题之路。这是一道小的设计题,设计一个字典树原创 2015-11-04 22:37:48 · 603 阅读 · 0 评论 -
[leetcode] Best Time to Buy and Sell Stock IV
Problem Description:Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most k transact原创 2015-10-20 23:50:23 · 539 阅读 · 0 评论 -
[leetcode] Rotate and Reverse
今天刷了两道比较简单的题目,恰好都与reverse有关,所以就一起简单说说了。(1)字符串的反转。“abcd”反转后变成“dcba”,这是最最基础的reverse了,也有文章分享过多种方法。但基本思路都差不多,就是依次把第一个元素与倒数第一个交换,第二个与倒数第二个交换,...,直至第n/2个为止。(2)Reverse Integer。将整数反转,如123变成321。可以把整数转换成字符串原创 2015-10-22 22:38:28 · 730 阅读 · 0 评论 -
[leetcode] House Robber
Problem Description: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them原创 2015-10-24 01:05:00 · 384 阅读 · 0 评论 -
[leetcode] Binary Tree Right Side View
Problem Description:Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the followin原创 2015-10-25 01:32:54 · 421 阅读 · 0 评论 -
[leetcode] Number of Islands
Problem Description:Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vert原创 2015-10-25 23:04:55 · 649 阅读 · 0 评论 -
[leetcode] Summary of Linked List (1)
今天总结leetcode上那些与Linked List有关的题目。1. Reverse Linked List题目大意:将单链表反转(可以分别考虑递归和迭代的方式)基本思路:(1)递归方式,对于某个待反转的list,先递归调用函数得到list->next的反转,然后将list节点加到list->next节点之后即可。注意递归出口会返回反转后的链表的头指针,原始链表的头指针变原创 2015-10-29 22:01:04 · 586 阅读 · 0 评论 -
[leetcode] Summary of Bit Manipulation
今天刷了几道题,发现leetcode现在都有分类了,所以顺便把以前刷过的bit manipulation相关的题目都看了一遍,下面简单总结一下。1. Single Number题目大意:在给定的整数数组中,除了一个数字之外,其他数字都出现了两次,现让求出只出现一次的那个数字。基本思想:遍历数组一次,将所有数字依次进行异或运算,结果即为所求的数字。因为其他数字都恰好出现了两次,异或得0。原创 2015-10-28 00:25:39 · 699 阅读 · 0 评论 -
[leetcode] Isomorphic Strings
Problem Description:Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character原创 2015-11-03 00:24:36 · 447 阅读 · 0 评论 -
[leetcode] Divide and Conquer
下面简单聊聊leetcode中的两道类似的DC的题目。之所以来写博客,是因为它们的思路确实与以往遇到的简单的DC有所不同。1. Count of range sumGiven an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra原创 2016-02-24 23:55:46 · 1571 阅读 · 0 评论 -
[leetcode] Largest Number
Problem Description:Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.原创 2015-10-19 00:26:33 · 658 阅读 · 0 评论 -
[leetcode] Repeated DNA Sequences
Problem Description: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated seque原创 2015-10-19 22:53:11 · 518 阅读 · 0 评论 -
[算法]子数组之和问题
问题描述:给定一个含有n个元素的整形数组a,再给定一个和target,求出数组中满足给定和的所有元素组合的数目。举个例子,设有数组a[5] = { 1, 2, 3, 4, 5},sum = 8,则满足和为8的组合数为3,即{1,2,5}, {1,3,4}, {3, 5}。http://www.cnblogs.com/graphics/archive/2011/07/14/2105195.htm原创 2014-09-10 22:39:22 · 1261 阅读 · 0 评论 -
笔试题next_permutation & Largest Rectangle in Histogram
看了看去年有道的2013年10月北邮站的笔试题,第一题很简单但unicode字符的输出没实现成功(题目见http://www.cnblogs.com/dancingrain/p/3405186.html),后两道编程题都很经典,在leetcode上遇到过,但是还是记不清了,所以决定写一写,争取把思路说清楚,把方法变成自己的。 第二道,对于给定的正整数n,1至n个数的全排列有n!个,对于任意原创 2014-09-12 23:36:34 · 956 阅读 · 0 评论 -
[算法]约瑟夫环问题
约瑟夫环问题描述:约瑟夫环问题(Josephus) 用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至全部输出。写出C程序。(约瑟夫环问题 Josephus)原创 2014-09-02 16:31:40 · 1155 阅读 · 0 评论 -
[待字闺中]最大乘积
今天待字闺中(微信公共账号)发布了一道这样的ti原创 2014-09-02 22:20:11 · 913 阅读 · 0 评论 -
[算法]作死的链表排序
链表排序应该最好写的是插入排序,但时间复杂度为O(原创 2014-09-23 22:57:06 · 445 阅读 · 0 评论 -
关于数组的面试题总结(一)
关于数组的面试题总结(一):1. 二维数组中的查找在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 2. 采用递归的方法求数组所有元素的和3. 求数组中出现次数超过一半的数字4. 旋转数组(包括重复元素)的最小数字原创 2014-09-16 21:41:13 · 2403 阅读 · 2 评论 -
关于数组的面试题总结(三)
再发一组关于数组的题目,其中部分来自leetcode,部分来自一些公司的面试题()。原创 2014-09-30 15:38:19 · 745 阅读 · 0 评论 -
关于数组的面试题总结(二)
1.给定一个无序的整型数组,求出最小的k个数两种思路:(1)如果所有的数组可以全部装入内存的话,采用快速排序的思想进行划分,不断向第k个小的数靠近,当得到第k小的数(key)时就相当于得到了最小的k个数,因为划分的时候保证了比key小的数全部放在了左边。平均时间复杂度为O(N)。(2)当数组太大无法一次性装入内存时,上面的方法失效,可以维持一个大小为k的大顶堆,取前k个数建堆后依次原创 2014-09-30 11:33:28 · 651 阅读 · 0 评论 -
将一根木棍分成三段,求这三段构成三角形的概率
题目:将一根木棍分成三段,求这三段构成三角形的概率。方法一:设线段长度为a,任意分成三段长分别为x,y和a-x-y,显然有x>0,y>0,a-x-y>0,将这三个约束条件画到(x,y)二维平面坐标系上,这三条直线围成了一个直角三角形即为可行域,其面积为(1/2)a^2。 而这三段长能构成三角形的条件是:任意两边之和大于第三边,也就是下面三个不等式得同时成立:x原创 2014-10-22 12:55:12 · 34549 阅读 · 10 评论 -
尼姆(Nim)游戏
不用博弈论的概念,通俗地讲一下尼姆(Nim)游戏现有若干堆石子,每堆石子的数量都是有限的,双人进行游戏,每个人在每次行动的时候可以“选择一堆石子并拿走若干颗,不能不拿”,没有石子可拿的人为输。是否有先手必胜的策略?直接考虑这个问题有点难度,我们还是从最简单的只有两堆石子来讲起吧!(a)当只有两堆石子,若两堆石子的数目一样多,那么先手必输。因为不管先手选择那一堆拿多少个,后手原创 2014-10-31 22:55:10 · 1898 阅读 · 2 评论 -
[leetcode] Burst Balloons
题目描述:Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you wil原创 2016-02-25 00:00:17 · 892 阅读 · 0 评论