随笔分类 -  算法题

上一页 1 ··· 4 5 6 7 8 9 10 11 12 ··· 16 下一页
摘要:题目:大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。 思路:经典的题目,递归的效率不如循环,因为递归要做许多重复计算。使用循环可以保存计算结果以重用。 实现代码: 阅读全文
posted @ 2016-03-02 19:54 Pickle 阅读(518) 评论(0) 推荐(0)
摘要:题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个非递减序列的一个旋转,输出旋转数组的最小元素。例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 思路:首先所给的旋转数组书局部有序的,这完全可以想到使用二分查找来解决会优化线性查找的 阅读全文
posted @ 2016-03-02 19:51 Pickle 阅读(810) 评论(0) 推荐(0)
摘要:题目:用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。 思路:用两个栈来模拟队列,一个栈作为存储,另一个栈作为交换空间。队列的push操作就直接push到第一个栈中,队列的pop操作,需要取得第一个栈低的元素,所以先把第一个栈的元素出栈,入栈到第二个队列,那么取的 阅读全文
posted @ 2016-03-02 19:24 Pickle 阅读(308) 评论(0) 推荐(0)
摘要:题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。 思路:知道了二叉树的前序和中序的序列就可以使用递归的方法 阅读全文
posted @ 2016-03-02 19:16 Pickle 阅读(406) 评论(0) 推荐(0)
摘要:题目:输入一个链表,从尾到头打印链表每个节点的值。 思路:链表是不可以随机访问的,所以如果不借助辅助空间的话,每打印一个节点就会遍历一遍链表,时间复杂度为O(n^2)。那么就需要以空间换取时间,因为是倒着遍历链表,所以栈是最好的选择。只需要遍历一遍链表入栈。然后遍历栈,出栈。这样时间复杂度为O(n) 阅读全文
posted @ 2016-03-02 19:08 Pickle 阅读(247) 评论(0) 推荐(0)
摘要:题目: 请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。 思路: 首先要遍历字符串得到空格的数量,则替换后的字符串长度应该是原长度+空格数量*2,从新分配长度。如果从前向后替换的话,多次替换会有 阅读全文
posted @ 2016-03-02 18:57 Pickle 阅读(275) 评论(0) 推荐(0)
摘要:题目: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 array: 待查找的二维数组target:查找的数字。查找到返回true,查找不到返回false。 思路: 1 2 8 9 阅读全文
posted @ 2016-03-02 17:38 Pickle 阅读(405) 评论(0) 推荐(0)
摘要:Description: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right 阅读全文
posted @ 2016-02-23 22:23 Pickle 阅读(458) 评论(0) 推荐(0)
摘要:Description: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5 阅读全文
posted @ 2016-02-22 18:24 Pickle 阅读(349) 评论(0) 推荐(0)
摘要:Description: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program shou 阅读全文
posted @ 2016-02-21 13:31 Pickle 阅读(469) 评论(0) 推荐(0)
摘要:Description: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 阅读全文
posted @ 2016-02-21 11:55 Pickle 阅读(246) 评论(0) 推荐(0)
摘要:Description: Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Re 阅读全文
posted @ 2016-02-20 19:48 Pickle 阅读(349) 评论(0) 推荐(0)
摘要:Description: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 阅读全文
posted @ 2016-02-19 22:29 Pickle 阅读(418) 评论(0) 推荐(0)
摘要:Description: Given an integer, write a function to determine if it is a power of three. 判断一个整数是否是3的幂。 基于公式log3^n可以得出y=lg(n)/log(3)如果y正好是个整数的话,说明n是3的幂。 阅读全文
posted @ 2016-02-18 18:51 Pickle 阅读(295) 评论(0) 推荐(0)
摘要:Description: Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. 阅读全文
posted @ 2016-02-17 23:30 Pickle 阅读(325) 评论(0) 推荐(0)
摘要:Description: Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node numbe 阅读全文
posted @ 2016-02-16 19:54 Pickle 阅读(466) 评论(0) 推荐(1)
摘要:Description:The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integernrepresenting the ... 阅读全文
posted @ 2015-11-29 22:35 Pickle 阅读(196) 评论(0) 推荐(0)
摘要:Description:Given an arraynumscontainingn+ 1 integers where each integer is between 1 andn(inclusive), prove that at least one duplicate number must e... 阅读全文
posted @ 2015-11-28 21:47 Pickle 阅读(219) 评论(0) 推荐(0)
摘要:Description:There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time c... 阅读全文
posted @ 2015-11-10 16:06 Pickle 阅读(243) 评论(0) 推荐(0)
摘要:Discription:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.Subscribe to see which companies asked t... 阅读全文
posted @ 2015-11-10 15:25 Pickle 阅读(218) 评论(0) 推荐(0)

上一页 1 ··· 4 5 6 7 8 9 10 11 12 ··· 16 下一页