10 2013 档案

摘要:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [6,5,7], [4,1,8,3]]The minimum path sum from top to bottom is11(i.e.,2+3+5+1= 11).今天的最后一题Triangle是经典的动态规划问题,第i行... 阅读全文
posted @ 2013-10-29 16:01 Apprentice.Z 阅读(156) 评论(0) 推荐(0) 编辑
摘要:今天的练习题是triangle三件套:Triangle, PASCAL Triangle, PASCAL Triangle II.PASCAL Triangle就是小学奥数中的“杨辉三角”, 存在如下关系triangle[i+1][j+1] = triangle[i][j]+triangle[i][j+1]即第i+1行第j+1个元素为第i行第j个元素与第j+1个元素之和PASCAL Triangle的题目要求:输出PASCAL Triangle的前numRows行GivennumRows, generate the firstnumRowsof Pascal's triangle.Fo 阅读全文
posted @ 2013-10-29 15:47 Apprentice.Z 阅读(315) 评论(0) 推荐(0) 编辑
摘要:又一道模拟题,按照一定规律生成字符串,求第n个字符串The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1is read off as"one 1"or11.11is read off as"two 1s"or21.21is read off as"one 2, thenone 1"or1211.Given an integern, generate thenthsequence. 阅读全文
posted @ 2013-10-28 13:23 Apprentice.Z 阅读(529) 评论(0) 推荐(0) 编辑
摘要:给定字符串a,b,分别表示两个二进制数。将a,b的二进制和以字符串形式返回。Given two binary strings, return their sum (also a binary string).For example,a ="11"b ="1"Return"100".思路:模拟二进制加法过程,从最低位开始难点在于进位的计算。记进位符为carry, 该位的和为numint num = a[i]-'0'+b[j]-'0'+carry;carry = num/2;num %= 2;代码 1 cl 阅读全文
posted @ 2013-10-28 13:12 Apprentice.Z 阅读(175) 评论(0) 推荐(0) 编辑
摘要:Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B aremandnrespectively.给定两个有序数组A和B,将B中的元素插入A(假设A有足够大的空间),保持有序性Tip: A和B的大小已知,即合并完成后的数组大小确定。将指针指向A和B的尾 阅读全文
posted @ 2013-10-27 22:57 Apprentice.Z 阅读(133) 评论(0) 推荐(0) 编辑
摘要:Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its level order traversal as:[ [3], [9,20], [15,7]]这道题目是二叉树的层次遍历。思路:借助于队列结构(queue),保存尚未访问儿子节点... 阅读全文
posted @ 2013-10-27 22:52 Apprentice.Z 阅读(178) 评论(0) 推荐(0) 编辑
摘要:Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order ofO(logn).If the target is not found in the array, return[-1, -1].For example,Given[5, 7, 7, 8, 8, 10]and target value 8,return[3, 4].题目要求:查找有序 阅读全文
posted @ 2013-10-27 11:23 Apprentice.Z 阅读(147) 评论(0) 推荐(0) 编辑
摘要:求输入数组的全排列输出。我是用经典的递归思路求解的:1. 当输入数组num只有一个元素,将num压入ret, 返回;(递归终止条件)2. 假设num中的元素个数为N,取出num的最后一个元素,记为b求出num[0..N-2]的全排列,将b插入所有可能的位置,得到完整的全排列结果这道题还有其它思路,已经有人做了讨论,戳这里Given a collection of numbers, return all possible permutations.For example,[1,2,3]have the following permutations:[1,2,3],[1,3,2],[2,1,3], 阅读全文
posted @ 2013-10-25 13:59 Apprentice.Z 阅读(313) 评论(0) 推荐(0) 编辑
摘要:链表题again. 实现链表节点的循环移位. 注意k的边界范围。思路:1. 得到链表长度N,指针end指向尾节点2. 找到新链表头节点在老链表中的前驱节点,记为p. p = N-k%N-13. end指向原链表head节点,此时链表为环状。更新链表头节点位置,head指向p->next。将p->next置为NULL,成为尾节点。Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLan 阅读全文
posted @ 2013-10-25 13:54 Apprentice.Z 阅读(136) 评论(0) 推荐(0) 编辑
摘要:又一道链表题。CareerCup上也有类似题目,trick和“部分链表逆序”一样,由于不知道链表的长度,second指针比first指针先走n步,然后在同时向前移动,直到second指针到达链表尾。注意删除头节点的情况。假设链表长度为N,倒数第k个节点是正数第N-k个节点(k2->3->4->5, and n = 2.After removing the second node from the end, the linked list becomes 1->2->3->5.代码 1 /** 2 * Definition for singly-linked 阅读全文
posted @ 2013-10-22 14:57 Apprentice.Z 阅读(147) 评论(0) 推荐(0) 编辑
摘要:很久没有打理cnblog的站点了,用octopress在github上搭了一个新主页,学术一些的笔记po用markdown在github主页更新。之前偶尔翻翻careercup,也会用白纸写些代码,但是很难坚持上机验证,链表/树的测试用例有些麻烦。暑假的时候刷了一些USACO的题目,OJ题难度略高,不是很适合准备面试。以上内容,一言以蔽之,曰:懒。正找工作的师兄推荐了LeetCode这个面试题OJ平台,题目难度和careercup相当,是个不错的练习平台。在暑假实习面试被狂虐以后,是时候知耻而后勇了。计划每天做两道题,3个月左右完成。立贴为证。今天的题目是部分链表逆序,将给定链表第m个节点到第 阅读全文
posted @ 2013-10-22 14:49 Apprentice.Z 阅读(298) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示