自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(49)
  • 收藏
  • 关注

原创 【leetcode】Pascal's Triangle II (python)

其实每一行的结果是二项式展开的系数,但是考虑到当给定的参数过大的时候,在求组合的过程中会出现溢出(中间过程要用到乘法),但是这样的算法的时间复杂度是O(N),所以在参数不太大的时候,还是不错的。这里用迭代的方法来求,当然复杂度就高了,是O(N^2),这里主要说下迭代时候的技巧,即在一个列表(数组)里进行迭代,实现如此的操作,要求在求下一行的时候,要从后往前进行,若是从前向后,就把后面要用的变量

2014-07-28 20:27:22 1768 2

原创 【leetcode】Sum Root to leaf Numbers

简单的二叉树的先根遍历模板的应用class Solution: # @param root, a tree node # @return an integer def hehe(self, num, root): #再原来的基础上*10,再加上当前的root.val num = num * 10 + root.val

2014-07-20 16:18:16 844

原创 【leetcode】Clone Graph(python)

类似于二叉树的三种遍历,我们可以基于遍历的模板做很多额外的事情,图的两种遍历,深度和广度模板同样也可以做很多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先访问第一个结点,接着访问第一个邻接点,再访问邻节点的邻节点。。。。class Solution: # @param node, a undirected graph node # @return a

2014-07-17 00:13:36 2101

原创 【leetcode】Candy(python)

题目要求比其高的邻居要比本身的奖励多,那么最少也要多一个,所有我们可以找到所有的凹点,凹点如下三种情形。找到所有的凹点后,我们就可以从凹点处开始向左右两个方向依次查找递增序列,其中每个高的都要比相邻的矮的多一个,比如1,2,5,4.我们找到凹点为1 和4,那么从1开始向左没有其他点,我们向右,依次得到2 比1高,2的糖果应该是1的基础上加1,为2, 5比2高,5的糖果是在2的基础上加1

2014-07-16 21:00:46 1303

原创 【剑指offer】面试题26:复杂链表的复制

def copyRandomList(self, head): if None == head: return None phead = head while phead: pnext = phead.next phead.next = RandomListNode(phead.label)

2014-07-16 13:32:43 910

原创 【leetcode】Word Break(python)

思路是这样的,我们从第一个字符开始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到,那么就是False了。在找到第一个后,接下来找下一个断句处,当然是从第一个断句处的下一个字符开始找连续的子串,但是这时与第一个就稍有不同,比如说word=‘ab’, dict={ 'a', ab', ...},在找到a后,接下来处理的是b,我们发现b不在dict中,但是我们

2014-07-15 21:53:59 2395

原创 【leetcode】Linked List Cycle (python)

class Solution: # @param head, a ListNode # @return a boolean def hasCycle(self, head): if None == head or None == head.next: return False pfast = head

2014-07-15 16:30:27 761

原创 【leetcode】Linked List Cycle (python)

题目分析见这里class Solution: # @param head, a ListNode # @return a list node def detectCycle(self, head): if None == head or None == head.next: return None pfast =

2014-07-15 16:24:51 756

原创 【leetcode】Reorder List (python)

问题的思路是这样:循环取头部合并,其实也可以换个角度来看,就是将后面的链表结点,一次隔空插入到第一部分的链表中。class Solution: # @param head, a ListNode # @return nothing def reorderList(self, head): if None == head or None ==

2014-07-15 13:27:48 1524

原创 二叉树的非递归遍历

先写下这个问题的模式def preorderTraversal(self, root): if root == None: return [] re = [] insert root to stack s while s not empty: cur_root = top of stack s s.pop() how to handle cur_root how to

2014-07-14 22:22:47 823

原创 【剑指offer】q50:树中结点的最近祖先

#@ root: the root of searched tree#@ nodeToFind: the tree-node to be found#@ path: the path from root to node#@@#@@ search tree referenced by root, and return the path #@@ from root to node, if n

2014-07-14 11:17:33 1876

原创 【剑指offer】 堆排序查找最小的K个数

上一篇 说了些堆的建立及其相关操作,这里看下用堆来解决数据量较大的时候,查找最小的k个数的情况。这里会用到上一篇中的函数。我们先生存1千万个随机数,写到文件中:import randomdef randData(): with open('randint.txt', 'w') as fd: for i in range(1, 10000000): fd.write('%

2014-07-13 17:00:04 1436

原创 堆排序及其相关操作

这里记录下堆的相关操作。op 1:''' @ data: the heap array @ p : index of parent item @ n : number of data @@ Swap p and it's son items, make p the largest of them'''def swapForMaxHeap(data, n, p): l

2014-07-13 03:12:43 1174

原创 【剑指offer】面试题43:n个骰子的点数

第一种思路是,每个骰子的点数从最小到最大,假设为1-6,那么所有的骰子从最小1开始,我们假设一种从左向右的排列,右边的最低,索引从最低开始,判断和的情况。def setTo1(dices, start, end): for i in range(start, end): dices[i] = 1def probability(n, s, dmax = 6, dmin = 1): i

2014-07-10 00:50:42 2289

原创 【剑指offer】面试题42:单词翻转顺序&左右旋转字符串

这里尽可能的不去用语言本身提供的函数。将string逆置def reverse(string): #return string[::-1] reversedStr = '' for i in xrange(len(string) - 1, -1, -1): reversedStr += string[i] return reversedStr单词的翻转def reverse

2014-07-08 15:43:28 1033

原创 【剑指offer】面试题28的习题:正方体,八皇后

test1 的判定函数:''' @ sum(1,2,3,4) = sum(5,6,7,8) and @ sum(1,2,5,6) = sum(3,4,7,8) and @ sum(1,3,5,7) = sum(2,4,6,8) @ used for test 1'''def sumEqual(data): eq1 = sum(data[0:4]) == sum(data[4:8

2014-07-08 13:57:27 868

原创 【剑指offer】面试题39:二叉树的深度

def TreeDepth1(root): if None == root: return 0 if None == root.left and None == root.right: return 1 leftDepth = 0; rightDepth = 0 if root.left: leftDepth = TreeDepth(root.left) if root.ri

2014-07-08 00:32:30 786

原创 【剑指offer】面试题37:两个链表的第一个公共结点

#@ util function, get the number of nodes in list referenced by headdef getLenOfList(head): nodeNum = 0 while head: nodeNum += 1 head = head.next return nodeNum# find the first common node o

2014-07-08 00:31:27 781

原创 【剑指offer】面试题36:数组中的逆序对

# @left part: [start, mid]# @right part: (mid, end]def merge(data, start, mid, end): if mid < start or end < mid: return 0 reverse = 0 ''' @ for start, it play as the start index of left par

2014-07-08 00:23:20 1066

原创 【剑指offer】面试题35:第一个只出现一次的数

def FirstNotRepeatingChar(string): hashStr = [0] * 256 for c in string: hashStr[ord(c)] += 1 for c in string: if hashStr[ord(c)] == 1: return c这里说下ord, 可以作为atoi来用,功能是若给定的参数是一个长度为1的字符串,那么若

2014-07-08 00:20:26 1189

原创 【剑指offer】面试题28:字符串的排列

def Permutation(data, i): if len( data ) == 0: return # i stand for the start of first part for i in range(i, len( data ) - 1): # j stand for the start of second part for j in range(i + 1, le

2014-07-08 00:14:06 792

原创 【剑指offer】面试题30:最小的K个数

import randomdef partition(data, start, end): if end <= start: return start index = random.randint(start, end) guard = data[index] while True: while data[start] < guard: start += 1 w

2014-07-07 00:37:03 1031 2

原创 【剑指offer】面试题21:包含min函数的栈

class StackWithMin: def __init__(self): self.data = [] self.min_data = [] self.size = 0 def push(self, val): self.data.append(val) if len(self.min_data) == 0 or \ self.min_data[-1] >

2014-07-06 22:03:55 647

原创 【剑指offer】面试题20:顺时针打印矩阵

“如果你愿意一层一层一层的剥开。。。”,这里就是按层剥开。def PrintMatrixClockWisely(matrix): rows = len(matrix) if rows == 0: return cols = len( matrix[0] ) cells = (min(rows, cols) + 1) >> 1 for cell in range(cells):

2014-07-06 21:27:20 683

原创 【剑指offer】Q19:二叉树的镜像

def MirroRecursively(root): # root is None or just one node, return root if None == root or None == root.left and None == root.right: return root root.left, root.right = root.right, root.left Mi

2014-07-06 19:41:54 642

原创 【剑指offer】Q18:树的子结构

类似于字符串的匹配,我们总是找到第一个匹配的字符,在继续比较以后的字符是否全部相同,如果匹配串的第一个字符与模式串的第一个不相同,我们就去查看匹配串的下一个字符是否与模式串的第一个相同,对应到这里,就是我们要遍历root1,找到与root2相同的第一个结点,若root1的根不相同,那么我们查找其左子树是否有第一个相同的,相同的操作再去看右子树是否有相同的第一个,若找到了第一个相同的,与字符串匹配思

2014-07-06 18:10:14 857

原创 【剑指offer】Q17:合并两个排序的链表

def Merge(head1, head2): if head1 == None: return head2 if head2 == None: return head1 psuhead = ListNode(-1) tail = psuhead while head1 and head2: if head1.val < head2.val: cur = head1

2014-07-06 14:00:48 724

原创 【剑指offer】Q16:翻转链表

def reverse(head): if head == None or head.next == None: return head psuhead = ListNode(-1) while head: nexthead = head.next head.next = psuhead.next psuhead.next = head head = nexthead

2014-07-06 13:15:12 634

原创 【剑指offer】Q15:链表中的倒数第K个结点

def FindKthToTail(head, k): if head == None: return None front = head; back = head steps = 0 # get | front - head | = k while front and steps < k: front = front.next steps += 1 # less than

2014-07-06 13:11:54 995

原创 【剑指offer】Q14:调整数组顺序使奇数位于偶数前面

def isOdd(n): return n & 1def Reorder(data, cf = isOdd): odd = 0 even = len( data ) - 1 while True: while not isOdd( data[ even ]) : even -= 1 while isOdd( data[ odd ]) : odd += 1 if odd

2014-07-06 12:48:51 752

原创 [剑指offer]Q13:O(1)时间删除链表的结点

通常我们所说的删除链表的某个结点,是彻底删除该结点的空间,而要这么做就必须知道其前驱结点。这里的想法是,链表中存储的val是同类型的,只要将该结点的val内容删除就可以了。那么就可以用该结点的后继结点的值覆盖当前结点,然后删除其后继结点,而对于其后继结点而言,该结点就是前驱。这里只需要考虑当前删除的结点是否为last node 就可以了,至于是否是头结点,这种情况是可以归为同一种情况的,只是参

2014-07-06 12:18:53 1524

原创 [剑指offer]Q11:数值的整数次方

pow(base, exponent)考虑一下几种情况:base = 0, 那么直接返回0base = 1, 那么直接返回1exponent = 0, 那么直接返回1, 注意base= 0exponent = 1, 那么直接返回 baseexponent  为正为负 的情况主要考察的点是将问题缩减,用折半的思想。这个题细节还是很多的,为了便于验证,leetcode上恰好

2014-07-05 21:24:15 847

原创 【剑指offer】Q9:斐波那契数列

def Fibonacci(n): if n <= 0: return 0 if n <= 1: return n f0 = 0; f1 = 1 for i in range(2, n + 1): fn = f0 + f1 f0 = f1 f1 = fn return fn

2014-07-05 20:56:44 1071

原创 【剑指offer】Q6:重建二叉树

class BTNode: def __init__(self, val): self.left = None self.right = None self.val = val'''@ construct tree by inorder & preorder'''def constructByInPre(inorder, instart, inend, preorde

2014-07-05 20:47:22 758

原创 【剑指offer】Q5:从尾到头打印链表

可以练习下链表的逆置。def PrintListReversingly(head): if head == None: return if head: PrintListReversingly(head.next) print head.valdef reverse(head): if head == None or head.next == None: return

2014-07-05 20:03:43 1738

原创 [leetcode]LRU Cache (python)

LRU:最近最久未使用,为了得到这个最新最久的信息,需要一种策略来进行记录,如果加入类似时间戳式的字段,那么每次删除的时候,就必须通过遍历才能得到时间信息,或者对时间戳进行排序,但是无论哪种,都是需要额外的维护,维护成本都比较高。广泛使用的策略是底层用双端队列来进行维护,双端使得在插入删除时操作更简单。而单单使用双端队列似乎还是不够,比如在get 时,还是需要顺序查找给定的key参数的,所以为

2014-07-05 19:26:14 2672 1

原创 [leetcode] Insertion Sort List(python)

简单的插入排序,总是超时,暂且放在这记录一下。class Solution: # @param head, a ListNode # @return a ListNode def insertionSortList(self, head): if head == None or head.next == None: return head psuhead

2014-07-05 15:29:17 1092

原创 【leetcode】sort list(python)

链表的归并排序超时的代码class Solution: def merge(self, head1, head2): if head1 == None: return head2 if head2 == None: return head1 # head1 and head2 point to the same link list if head1 == he

2014-07-05 11:12:10 881

原创 【leetcode】:Evaluate Reverse Polish Notation (python)

逆波兰式的求解,建立一个类栈容器,遍历给定的逆波兰表达式,遇到数字就push, 遇到操作符就进行出栈,连续出两次,因为给定的四则运算符都是双目的,这里注意下这两个操作数的先后顺序,因为对于加法和乘法没关系,但是对于减法和除法是有先后关系的。然后进行相应的运算,将结果push进栈中。这里附带说明下python中进行除法运算与c,java系列中的除法的不同,就是向下取整的问题。这种不同表现在两个操

2014-07-04 17:52:10 792

原创 【leetcode】Reverse Words in a String (python)

陆陆续续几个月下来,终于把题刷完了,过程中遇到的python的题解很少,这里重新用python实现下,所以题解可能都是总结性的,或者是新的心得,不会仅针对题目本身说的太详细。 def reverseWords(self, s): s = ' '.join(s.split()[::-1]) return s[ : :  -1 ] 是将元素进行翻转

2014-07-04 17:38:43 812

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除