04 2016 档案

摘要:Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your a 阅读全文
posted @ 2016-04-30 14:14 周洋 阅读(264) 评论(0) 推荐(0)
摘要:Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical a 阅读全文
posted @ 2016-04-30 03:47 周洋 阅读(350) 评论(0) 推荐(0)
摘要:找一下规律就发现,4的倍数都必败,其它都必胜。 阅读全文
posted @ 2016-04-30 03:42 周洋 阅读(281) 评论(0) 推荐(0)
摘要:Invert a binary tree. to Trivia:This problem was inspired by this original tweet by Max Howell: 题解:很简单的递归。 阅读全文
posted @ 2016-04-30 02:42 周洋 阅读(351) 评论(0) 推荐(0)
摘要:以下两点摘自:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431664106267f12e9bef7ee14cf6a8776a479bdec9b9000 1.对于单个字符的 阅读全文
posted @ 2016-04-29 03:01 周洋 阅读(2015) 评论(0) 推荐(0)
摘要:读文件 打开一个文件用open()方法(open()返回一个文件对象,它是可迭代的): r表示是文本文件,rb是二进制文件。(这个mode参数默认值就是r) 如果文件不存在,open()函数就会抛出一个IOError的错误,并且给出错误码和详细的信息告诉你文件不存在: 文件使用完毕后必须关闭,因为文 阅读全文
posted @ 2016-04-28 01:44 周洋 阅读(385010) 评论(2) 推荐(16)
摘要:1.sort() list类型有一个自带的排序函数sort() 参数说明: (1) cmp参数 cmp接受一个函数,来确定比较方式,默认的是: def f(a,b): return a-b 返回负数就是a<b。(升序) 所以我们如果要想按降序排序,可以这么定义cmp: list.sort(cmp=l 阅读全文
posted @ 2016-04-27 23:19 周洋 阅读(3776) 评论(0) 推荐(0)
摘要:enumerate函数用于遍历序列中的元素以及它们的下标,可以非常方便的遍历元素。 比如我在往excel中写数据时就用到了这个函数: 阅读全文
posted @ 2016-04-27 22:54 周洋 阅读(1592) 评论(0) 推荐(0)
摘要:bisect模块用于二分查找,非常方便。 Bisect模块提供的函数有: 1.查找 bisect.bisect_left(a,x, lo=0, hi=len(a)) : 查找在有序列表a中插入x的index。lo和hi用于指定列表的区间,默认是使用整个列表。 bisect.bisect_right( 阅读全文
posted @ 2016-04-26 23:55 周洋 阅读(1480) 评论(0) 推荐(0)
摘要:Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 阅读全文
posted @ 2016-04-26 03:15 周洋 阅读(234) 评论(0) 推荐(0)
摘要:Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s 阅读全文
posted @ 2016-04-25 11:09 周洋 阅读(146) 评论(0) 推荐(0)
摘要:Description: Count the number of prime numbers less than a non-negative number, n. 题解:就是线性筛素数的模板题。 阅读全文
posted @ 2016-04-25 04:16 周洋 阅读(266) 评论(0) 推荐(0)
摘要:Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 阅读全文
posted @ 2016-04-25 03:33 周洋 阅读(151) 评论(0) 推荐(0)
摘要:Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 -- 阅读全文
posted @ 2016-04-25 03:15 周洋 阅读(639) 评论(0) 推荐(0)
摘要:题意:把一个链表倒过来。 题解:有递归和非递归两种方式,主要就是考察指针操作的熟悉程度。 递归的方法: 非递归:就一个一个转过来方向就好了。转的时候需要用两个辅助指针,一个指向该节点的前一个节点,一个指向后一个节点。 阅读全文
posted @ 2016-04-25 02:31 周洋 阅读(195) 评论(0) 推荐(0)
摘要: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 occurrenc 阅读全文
posted @ 2016-04-25 01:50 周洋 阅读(254) 评论(0) 推荐(0)
摘要: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 adjacen 阅读全文
posted @ 2016-04-25 01:36 周洋 阅读(328) 评论(0) 推荐(0)
摘要:Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive intege 阅读全文
posted @ 2016-04-25 01:35 周洋 阅读(224) 评论(0) 推荐(0)
摘要:Write a function that takes a string as input and returns the string reversed. Example:Given s = "hello", return "olleh". 题解:送分题。。。 阅读全文
posted @ 2016-04-23 02:04 周洋 阅读(153) 评论(0) 推荐(0)
摘要:就是一个二分法快速幂。 但是需要注意的问题是这里是实数,而且n可能为负。int的范围是-2,147,483,648 至 2,147,483,647。如果为-2,147,483,648那么直接n=-n就爆int了。所以先要把n换成longlong。 阅读全文
posted @ 2016-04-21 14:37 周洋 阅读(285) 评论(0) 推荐(0)
摘要:Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum pro 阅读全文
posted @ 2016-04-21 02:03 周洋 阅读(3490) 评论(0) 推荐(0)
摘要:Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 - 阅读全文
posted @ 2016-04-20 04:45 周洋 阅读(217) 评论(0) 推荐(0)
摘要:Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. For example, giv 阅读全文
posted @ 2016-04-20 04:23 周洋 阅读(168) 评论(0) 推荐(0)
摘要:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. The update(i, val) function modifies nums by upd 阅读全文
posted @ 2016-04-20 03:35 周洋 阅读(591) 评论(0) 推荐(0)
摘要: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 corner (row2 阅读全文
posted @ 2016-04-19 23:20 周洋 阅读(275) 评论(0) 推荐(0)
摘要:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Note: 前缀和的应用,很简单。 阅读全文
posted @ 2016-04-19 15:32 周洋 阅读(216) 评论(0) 推荐(0)
摘要:判断一个数是不是4的次方。 我们知道num & (num - 1)可以用来判断一个数是否为2的次方数,更进一步说,就是二进制表示下,只有最高位是1,那么由于是2的次方数,不一定是4的次方数,比如8,所以我们还要其他的限定条件,我们仔细观察可以发现,4的次方数的最高位的1都是计数位,那么我们只需与上一 阅读全文
posted @ 2016-04-18 16:05 周洋 阅读(642) 评论(0) 推荐(0)
摘要:Given a linked list, remove the nth node from the end of list and return its head. For example, Note:Given n will always be valid.Try to do this in on 阅读全文
posted @ 2016-04-18 05:53 周洋 阅读(155) 评论(0) 推荐(0)
摘要:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 很简单的水题。把 阅读全文
posted @ 2016-04-18 05:36 周洋 阅读(194) 评论(0) 推荐(0)
摘要:Merge k sorted linked lists and return it as one sorted list. 题意:把k个已经排好序的链表整合到一个链表中,并且这个链表是排了序的。 题解:这是一道经典好题,值得仔细一说。 有两种方法,假设每个链表的平均长度是n,那么这两种方法的时间复杂 阅读全文
posted @ 2016-04-18 05:25 周洋 阅读(3810) 评论(0) 推荐(0)
摘要:Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-neg 阅读全文
posted @ 2016-04-17 20:30 周洋 阅读(428) 评论(0) 推荐(0)
摘要:STL中的string有6个查找函数: 1.find() 2.rfind() 从最后一个字符开始往前找。 3.find_first_of() 4.find_not_first_of() 5.find_last_of() 6.find_not_last_of() 所有这些查找函数返回值都是size_t 阅读全文
posted @ 2016-04-17 16:25 周洋 阅读(704) 评论(0) 推荐(0)
摘要:Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 简单的二进制高精度加法。 阅读全文
posted @ 2016-04-17 15:20 周洋 阅读(216) 评论(0) 推荐(0)
摘要:Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is a 阅读全文
posted @ 2016-04-16 23:21 周洋 阅读(410) 评论(0) 推荐(0)
摘要:就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解。 阅读全文
posted @ 2016-04-16 22:23 周洋 阅读(802) 评论(0) 推荐(0)
摘要:题目: Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that 阅读全文
posted @ 2016-04-16 22:14 周洋 阅读(1113) 评论(0) 推荐(0)
摘要:Model.save(force_insert=False, force_update=False, using=DEFAULT_DB_ALIAS, update_fields=None) id和pk 如果一个model里没有显示声明哪一个字段(field)是主键(即在某个字段里声明primary_ 阅读全文
posted @ 2016-04-16 01:35 周洋 阅读(36913) 评论(0) 推荐(3)
摘要:C语言的字符串要注意最后一位默认是'/0'的问题。这是一个易错点。 strlen()计算长度时不考虑末尾的'\0' string定义的长度是10,str1的最后一位默认为'\0',所以str1其实长度为11。strcpy()函数中,如果第二个串比第一个串长,是没有error的,第一个串的长度会增加, 阅读全文
posted @ 2016-04-07 02:39 周洋 阅读(5121) 评论(0) 推荐(0)
摘要:一个由C/C++编译的程序占用的内存分为以下几个部分 1、栈区(stack)— 由编译器自动分配释放 ,存放函数参数值,局部变量的值等。其操作方式类似于数据结构中的栈。 2、堆区(heap) — 一般由程序员分配释放 , 若程序员不释放,程序结束时可能由OS回收 。注意它与数据结构中的堆是两回事,分 阅读全文
posted @ 2016-04-01 15:35 周洋 阅读(606) 评论(0) 推荐(0)