12 2013 档案

摘要:Givennpoints on a 2D plane, find the maximum number of points that lie on the same straight line.思考:考虑以下几种情况:1.points中0、1、2个点;2.points含相同点如{[0,0],[0,0]},{[1,1],[1,1],[2,2],[2,2]}};3.斜率为无穷。/** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Poi... 阅读全文
posted @ 2013-12-29 19:40 七年之后 阅读(218) 评论(0) 推荐(0)
摘要:一.套接字(socket)函数 图1给出了在一个TCP客户与服务器通信的流程。服务器首先启动,稍后某个客户启动,它试图连接到服务器。假设客户给服务器发送一个请求,服务器处理该请求,并且给客户发回一个相应。这个过程一直持续下去,知道客户关闭连接的客户端,从而给服务器发送一个EOF(文件结束)通知为止... 阅读全文
posted @ 2013-12-27 17:49 七年之后 阅读(3204) 评论(0) 推荐(0)
摘要:通过配置nginx.conf文件来实现对Nginx状态信息的监控。1.配置nginx.confvim /usr/local/nginx/conf/nginx.conf再server块配置项中添加状态监控代码:location /nginx-status { stub_status on; #Nginx状态监控配置 access_log off;}2.编译模块上述代码中的stub_status模块是用来查看Nginx的状态信息,但是它默认是不会编译进Nginx的,所以要在编译安装Nginx时指定:./configure --prefix=/usr/local/nginx --... 阅读全文
posted @ 2013-12-22 16:47 七年之后 阅读(334) 评论(0) 推荐(0)
摘要:Given a set ofnon-overlappingintervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Example 1:Given intervals[1,3],[6,9], insert and merge[2,5]in as[1,5],[6,9].Example 2:Given[1,2],[3,5],[6,7],[8 阅读全文
posted @ 2013-12-20 14:37 七年之后 阅读(253) 评论(0) 推荐(0)
摘要:如果在shell脚本中处理数据文件,那么我们就必须熟悉正则表达式。正则表达式是用来过滤数据流中文本的模式模板,模式由标准文本字符和特殊字符组成。正则表达式用特殊字符来匹配一系列一个或多个字符,要想掌握正则表达式,必须深刻认识每个特殊字符: .*^${}+?|()下面分别解释一下: 脱字符 ^ 定义从数据流中文本行的行首开始的字符; 美元符 $ 指明数据必须以该文本模式结尾; 点字符 . 用来匹配任意单字符,除了换行符; 字符组 [Yy] 用来匹配字符组中某个字符(比如y,但不清楚其大小写)出现在数据流中; 排除字符组 [^ch] 用来排除字符组中出现过字符(比如ch)的文本; 区间 [0-.. 阅读全文
posted @ 2013-12-19 20:08 七年之后 阅读(167) 评论(0) 推荐(0)
摘要:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to reach the last index in the minimum number of jumps.For example:Given array A =[2,3,1,1,4]The minimum n 阅读全文
posted @ 2013-12-19 14:50 七年之后 阅读(132) 评论(0) 推荐(0)
摘要:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index.For example:A =[2,3,1,1,4], returntrue.A =[3,2,1,0,4], returnfalse.cl 阅读全文
posted @ 2013-12-19 14:11 七年之后 阅读(180) 评论(0) 推荐(0)
摘要:Implement wildcard pattern matching with support for'?'and'*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cover the entire input string (not partial).The function prototype should be:bool i 阅读全文
posted @ 2013-12-19 11:47 七年之后 阅读(232) 评论(0) 推荐(0)
摘要:一.sed编辑器 shell脚本最常见的用途就是处理文本文件,sed和gawk能够极大的简化需要进行的数据处理任务。sed编辑器是流编辑器,跟普通交互式文本编辑器(如vim)不同。流编辑器在编辑器处理数据前基于预先提供的一组规则来编辑数据流。由于命令都是一行一行顺序处理,sed编辑器必须一次就完成对... 阅读全文
posted @ 2013-12-18 21:05 七年之后 阅读(728) 评论(0) 推荐(0)
摘要:Validate if a given string is numeric.Some examples:"0"=>true" 0.1 "=>true"abc"=>false"1 a"=>false"2e10"=>trueNote:It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementin 阅读全文
posted @ 2013-12-18 15:48 七年之后 阅读(333) 评论(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-negative.思考:大数相乘,模拟算数运算。class Solution {public: string multiply(string num1, string num2) { if(num1=="0"||num2=="0") return "0&qu 阅读全文
posted @ 2013-12-18 14:56 七年之后 阅读(179) 评论(0) 推荐(0)
摘要:Given a number represented as an array of digits, plus one to the number.思考:大数相加的思想。flag是进位标记。class Solution {public: vector plusOne(vector &digits) { int len=digits.size(); bool flag=false; digits[len-1]+=1; int i=len-1; if(digits[i]>9) flag=true; while(... 阅读全文
posted @ 2013-12-16 14:10 七年之后 阅读(225) 评论(0) 推荐(0)
摘要:Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100".思考:大数相加的思想。class Solution {public: string addBinary(string a, string b) { if(a.length()==0) return b; if(b.length()==0) return a; int len=a.length()0;i--) { ... 阅读全文
posted @ 2013-12-16 13:35 七年之后 阅读(210) 评论(0) 推荐(0)
摘要:Given a matrix ofmxnelements (mrows,ncolumns), 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 should return[1,2,3,6,9,8,7,4,5].class Solution {public: vector spiralOrder(vector > &matrix) { vector ans; ... 阅读全文
posted @ 2013-12-15 14:03 七年之后 阅读(167) 评论(0) 推荐(0)
摘要:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray[4,−1,2,1]has the largest sum =6.思考:最大字串和。class Solution {public: int maxSubArray(int A[], int n) { int last=A[... 阅读全文
posted @ 2013-12-15 00:13 七年之后 阅读(187) 评论(0) 推荐(0)
摘要:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?思考:两步,先对角线交换再上下交换。class Solution {public: void rotate(vector > &matrix) { int n=matrix.size(); int i,j; for(i=0;i<n;i++) { for(... 阅读全文
posted @ 2013-12-13 20:46 七年之后 阅读(179) 评论(0) 推荐(0)
该文被密码保护。
posted @ 2013-12-13 18:43 七年之后 阅读(9) 评论(0) 推荐(0)
摘要:Givennnon-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.Fo... 阅读全文
posted @ 2013-12-12 22:31 七年之后 阅读(205) 评论(0) 推荐(0)
摘要:Given an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[3,4,-1,1]return2.Your algorithm should run inO(n) time and uses constant space.思考:不使用额外空间的一种途径就是利用已存在的空间。此题数组中的数放到A数组中对应的位置。class Solution {public: int firstMissingPositive(int A[], int n)... 阅读全文
posted @ 2013-12-12 18:47 七年之后 阅读(195) 评论(0) 推荐(0)
摘要:Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Each number inCmay only be usedoncein the combination.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, … ,ak) must 阅读全文
posted @ 2013-12-12 17:18 七年之后 阅读(166) 评论(0) 推荐(0)
摘要:Given a set of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Thesamerepeated number may be chosen fromCunlimited number of times.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, … ,ak 阅读全文
posted @ 2013-12-12 16:12 七年之后 阅读(208) 评论(0) 推荐(0)
摘要: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 constant space solution?/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode... 阅读全文
posted @ 2013-12-12 15:25 七年之后 阅读(168) 评论(0) 推荐(0)
摘要:Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keysless thanthe node's key.The right subtree of a node contains only nodes with keysgreater thanthe node's key.Both the left and ri 阅读全文
posted @ 2013-12-12 10:56 七年之后 阅读(214) 评论(0) 推荐(0)
摘要:Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path1->2->3which represents the number123.Find the total sum of all root-to-leaf numbers.For example, 1 / \ 2 3The root-to-leaf path1->2represents the number12.T 阅读全文
posted @ 2013-12-10 16:50 七年之后 阅读(189) 评论(0) 推荐(0)
摘要:Given a binary tree, return the inorder traversal of its nodes' values.For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Note: Recursive solution is trivial, could you do it iteratively?confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on 阅读全文
posted @ 2013-12-09 22:44 七年之后 阅读(223) 评论(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.思考:3指针。从后往前遍历。class Solution {public: void merge(int A[], int m, int B[], ... 阅读全文
posted @ 2013-12-09 16:56 七年之后 阅读(218) 评论(0) 推荐(0)
摘要:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as1and0respectively in the grid.For example,There is one obstacle in the middle of a 3x3 grid as illustrated below.[ [0,0,0], [0,1, 阅读全文
posted @ 2013-12-09 15:07 七年之后 阅读(158) 评论(0) 推荐(0)
摘要:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray[4,−1,2,1]has the largest sum =6.click to show more practice.More practice:If you have figured out the O(n) solution, try 阅读全文
posted @ 2013-12-09 11:47 七年之后 阅读(162) 评论(0) 推荐(0)
摘要:Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.Note:You are not suppose to use the l 阅读全文
posted @ 2013-12-08 14:02 七年之后 阅读(185) 评论(0) 推荐(0)
摘要:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofeverynode never differ by more than 1.思考:求二叉树高的变形,加上判断即可。/** * Definition for binary tree * struct TreeNode { * int val; * ... 阅读全文
posted @ 2013-12-08 10:08 七年之后 阅读(197) 评论(0) 推荐(0)
摘要: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"isnota palindrome.Note:Have you consider that the string might be empty? This is a good question to 阅读全文
posted @ 2013-12-08 09:45 七年之后 阅读(177) 评论(0) 推荐(0)
摘要:Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3Return6./** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode... 阅读全文
posted @ 2013-12-06 12:24 七年之后 阅读(176) 评论(0) 推荐(0)
摘要:Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.思考:DFS。/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) ... 阅读全文
posted @ 2013-12-06 09:34 七年之后 阅读(155) 评论(0) 推荐(0)
摘要:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the following is not: 1 / \ 2 2 \ \ 3 3Note:Bonus points if you could solve it both recursively and iterati... 阅读全文
posted @ 2013-12-05 22:25 七年之后 阅读(177) 评论(0) 推荐(0)
摘要:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.思考:考虑[1,2]这种情况,返回树最低高度为2。所以分两种情况,有一棵子树不在时,返回另一棵子树树高+1,否则返回矮子树高+1./** * Definition for binary tree * struct TreeNode { * int val; * Tre... 阅读全文
posted @ 2013-12-05 12:35 七年之后 阅读(164) 评论(0) 推荐(0)
摘要:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1return[ [5,4,11,2]... 阅读全文
posted @ 2013-12-05 12:10 七年之后 阅读(181) 评论(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 and the nodes have the same value.思考:DFS./** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right... 阅读全文
posted @ 2013-12-05 10:50 七年之后 阅读(122) 评论(0) 推荐(0)
摘要:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ ... 阅读全文
posted @ 2013-12-04 22:02 七年之后 阅读(188) 评论(0) 推荐(0)
摘要: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).Note:Bonus point if you are a... 阅读全文
posted @ 2013-12-04 21:28 七年之后 阅读(219) 评论(0) 推荐(0)
摘要:Given a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should preserve the original relative order of the nodes in each of the two partitions.For example,Given1->4->3->2->5->2andx= 3,return1->2->2->4->3- 阅读全文
posted @ 2013-12-04 20:37 七年之后 阅读(213) 评论(0) 推荐(0)
摘要:“选择了错误的算法,便注定了失败的命运”。最近对这句话感触颇深,经常因为一开始思路错误,修改半天,到头来却都是无用功,所以学好算法势在必行。算法的泛化过程如何设计一个算法,使他适用于任何(大多数)数据结构呢?先看一个算法泛华的实例。假设我们要写一个find()函数,在array中寻找特定值。面对整数array,我们很快能写出:int *find(int *array,int size,int target){ for(int i=0;iT *find(T *begin,T *end,T& value){ while(begin!=end&&*begin!=value) 阅读全文
posted @ 2013-12-04 17:18 七年之后 阅读(556) 评论(0) 推荐(0)
摘要: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.Note: The sequence of inte 阅读全文
posted @ 2013-12-04 00:37 七年之后 阅读(250) 评论(0) 推荐(0)
摘要:1.为什么选择NginxNginx是当前最流行的Web服务器之一,与Apache相比,Nginx在高并发情况下具有巨大的性能优势。Nginx具有高扩展性,高可靠性,低内存消耗等特点,并且Nginx拥有无数官方功能模块,第三方功能模块,这些功能模块可以叠加以实现更加强大/复杂的功能。选择Nginx的核心理由是它能在支持高并发请求的同时保持高效的服务。2.Nginx的安装与运行Nginx官方下载地址:http://nginx.org/en/download.html解压:$ tar -zxvf nginx-1.4.4.tar.gz进入nginx目录,执行如下命令:$ ./configure$ ma 阅读全文
posted @ 2013-12-02 17:37 七年之后 阅读(311) 评论(0) 推荐(0)