- 博客(100)
- 资源 (3)
- 收藏
- 关注
原创 Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.Have
2014-08-08 16:03:09
360
原创 String to Integer (atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input case
2014-08-06 16:43:53
335
原创 Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]You
2014-07-30 14:45:28
399
原创 Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array return it
2014-07-29 16:32:40
330
原创 Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, where
2014-07-28 15:39:49
367
原创 入栈出栈问题
上面就是它的常见公式,很多问题都可以通过计算这个公式直接得到你想要的结果。比如给你了一个这样的题目:已知一个入栈序列为1,2,3,4,5,求所有可能的出栈序列总数有多少?简单的一算就知道了这个就相当于要计算C5 的值了,也许你已经知道答案了。那么我想问:你知道怎么去求出它的所有可能的序列而不是仅仅要出它的总数呢?此时,这个公式将显得无力。那么我们接下来分析一下该如何解决这个问题。当面对比较复杂抽象
2014-07-12 10:31:28
562
转载 布隆过滤器 (Bloom Filter)
布隆过滤器 (Bloom Filter)是由Burton Howard Bloom于1970年提出,它是一种space efficient的概率型数据结构,用于判断一个元素是否在集合中。在垃圾邮件过滤的黑白名单方法、爬虫(Crawler)的网址判重模块中等等经常被用到。哈希表也能用于判断元素是否在集合中,但是布隆过滤器只需要哈希表的1/8或1/4的空间复杂度就能完成同样的问题。布隆过滤器可以插入元
2014-04-06 19:24:42
518
转载 C语言结构体里的成员数组和指针
单看这文章的标题,你可能会觉得好像没什么意思。你先别下这个结论,相信这篇文章会对你理解C语言有帮助。这篇文章产生的背景是在微博上,看到@Laruence同学出了一个关于C语言的题,微博链接。微博截图如下。我觉得好多人对这段代码的理解还不够深入,所以写下了这篇文章。为了方便你把代码copy过去编译和调试,我把代码列在下面:123456
2014-04-05 10:11:48
621
原创 LRU Cache (LRU策略缓存)
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.get(key) - Get the value (will always be positive) of the key if
2014-03-28 15:13:21
551
原创 Surrounded Regions (包围区域)
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region.For example,X X X XX O O XX X
2014-03-27 10:48:21
396
原创 triangle(三角形)
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,
2014-03-26 14:02:48
499
原创 Triangle (三角形)
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,
2014-03-26 13:54:54
202
原创 一道关于fork()函数的题
前几天被室友问到一道题目的输出int main(){ int i = 0; for (i = 0; i < 2; ++i); { fork(); printf("-"); } return 0;}问这个输出多少个“-”, 当时脑袋里没什么思绪,就在linux上跑了一下,输出的个数一定但是次序可能不唯一然后得到了下图。这个结果着实令我费解,因为我算出来的结
2014-03-25 21:19:56
874
1
原创 Copy List with Random Pointer(拷贝带有random指针的链表)
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list. 这道题自己一直没想到比较好的方法
2014-03-25 16:16:05
443
原创 Unix 文件目录与链接
4.1 stat函数与lstat函数的区别在于stat遇到参数为符号链接则会返回原始的被链接的文件的信息,而lstat函数会返回链接文件本身的信息。4.2 umask为777的时候表示所有用户(自己,组,其他)文件没有任何访问权写(读写执行)4.5 目录的大小一定不为0, 至少包含了.和..两个目录,符号链接文件的大小为被链接文件的名字长度(字节)也至少为1
2014-03-24 15:40:28
328
原创 Reverse Linked List II (链表逆置)
Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note:Given m, n satisfy the fol
2014-03-24 14:05:42
464
原创 Binary Tree Zigzag Level Order Traversal (之字访问树节点)
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tr
2014-03-22 19:33:57
622
原创 Recover Binary Search Tree (恢复一棵二叉搜索树)
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a
2014-03-22 10:06:11
650
原创 Flatten Binary Tree to Linked List(平整二叉树)
Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \
2014-03-20 18:33:54
516
原创 Remove Duplicates from Sorted List II (从排序链表中移出重复元素)
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1
2014-03-19 16:01:29
593
原创 Trapping Rain Water (最大盛水量)
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1],
2014-03-18 15:27:53
329
原创 windows环境下static变量的初始化
前几天有看到这么一个问题 说 c++中 static变量的初始化是什么时候, 和室友讨论了一下无果 做了一下实验,发现了一些结论。如果声明了一个static变量,但是没给这个变量赋值, 然后程序运行的时候也没有调用这个static变量,那么这个static变量实际上并没有被分配内存空间,也没有初始化,而被赋予处置了的static变量在main函数执行之前其实就已经分配了内存空间,下图所示变量b
2014-03-17 21:10:16
878
1
原创 static变量
前几天有看到这么一个问题 说 c++中 static变量的初始化是什么时候, 和室友讨论了一下无果 做了一下实验,发现了一些结论。如果声明了一个static变量,但是没给这个变量赋值, 然后程序运行的时候也没有调用这个static变量,那么这个static变量实际上并没有被分配内存空间,也没有初始化,而被赋予处置了的static变量在main函数执行之前其实就已经分配了内存空间,下图所示变量b
2014-03-17 20:59:03
101
原创 Valid Sudoku (九宫格)
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A partially fille
2014-03-17 20:53:02
543
原创 Reorder List (链表重排)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to {1,4
2014-03-13 15:13:02
472
原创 Evaluate Reverse Polish Notation (求逆波兰表达式值)
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: ["2", "1", "+",
2014-03-12 09:57:01
476
原创 Reverse Words in a String (字符串单词逆序)
Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".click to show clarification.Clarification:What constitutes
2014-03-11 09:25:33
515
转载 C++项目中的extern "C" {}
引言在用C++的项目源码中,经常会不可避免的会看到下面的代码:123456789#ifdef __cplusplusextern "C" {#endif /*...*/ #ifdef __cplusplus}
2014-03-10 16:03:06
479
原创 Subsets(子集)
Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,
2014-03-10 10:53:17
413
原创 Length of Last Word(最后一个单词长度)
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is
2014-03-10 10:00:40
555
原创 Set Matrix Zeroes(矩阵置0)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A straight forward solution using O(m
2014-03-10 08:36:19
512
原创 Longest Common Prefix (最长公共前缀)
Write a function to find the longest common prefix string amongst an array of strings.求一个字符串数组中的所有字符串的最长公共前缀,这个就一个一个字符串从头比较就行了。class Solution {public: string longestCommonPrefix(vector &s
2014-03-07 10:25:19
1914
1
原创 Validate Binary Search Tree (判断一颗树是否为二叉排序树)
Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *
2014-03-07 09:47:19
488
原创 Rotate List (链表循环右移)
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.题意是说给定一个k >= 0 把链表循环右移k位, 这
2014-03-06 10:36:08
931
原创 Construct Binary Tree from Inorder and Postorder Traversal(根据中序后序建立二叉树)
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.这个根据中序和后序建立二叉树与根据先序中序建立非常像,只需要把从先序从头开始扫描变成从后序的尾部
2014-03-06 09:53:14
652
原创 Construct Binary Tree from Preorder and Inorder Traversal (先序中序建立二叉树)
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.给定先序序列和中序序列建立二叉树, 递归解决, 按顺序扫描先序序列,每一次扫描的第一个节点作为根,然后在中序序
2014-03-06 09:31:44
606
原创 Palindrome Number (数字回文)
Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of convertin
2014-03-05 17:03:38
468
原创 Convert Sorted List to Binary Search Tree (有序链表转换成平衡二叉排序树)
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.题目描述很简单,就是说把一个升序链表转换成平衡二叉排序树,即 最优二叉排序树, 最优二叉排序树的搜索时间为logn 即树的高度, 因为链表很难处理,直接转换成数组比较容易
2014-03-05 10:29:54
537
原创 Valid Palindrome(有效回文)
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a
2014-03-05 09:51:28
548
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人