自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 字符串模式匹配

字符串模式匹配问题:在目标串TTT中查找是否有与模式串PPP匹配的子串。已经有很多算法用来解决这个问题(参考string matching algorithms),这里主要介绍朴素的模式匹配算法和KMP算法。...

2018-06-08 01:12:35 519

原创 八皇后问题(Eight Queens Puzzle)

八皇后问题八皇后问题是由国际象棋棋手马克斯·贝瑟尔(Max Bezzel)于1848年提出的:在8×8格的国际象棋棋盘上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。八皇后问题可以推广为更一般的nnn皇后问题:在n×nn×nn×n的棋盘上摆放nnn个皇后,使其不能互相攻击(仅当n=2n=2n=2和n=3n=3n=3时无解)。解题...

2018-05-24 00:46:40 2220

原创 基本TCP套接字编程

基本套接字函数

2017-10-27 21:45:42 355

原创 套接字编程简介

套接字地址结构IPv4套接字地址结构通常也称为“网际套接字地址结构”,以sockaddr_in命名,定义在netinet/in.h头文件中:struct in_addr { in_addr_t s_addr; /*32位的IPv4地址,网络字节序*/};struct sockaddr_in { uint8_t sin_len; /**/

2017-09-17 20:18:59 368

转载 Python 生成器

Python 生成器生成器(generator)是一种迭代器,它是生成迭代器(generator iterator)的简称。通常用来生成一个值的序列,以便在迭代中使用。另外,对于生成器的特殊语法支持使得编写一个生成器比自定义一个常规的迭代器要简单不少。从Python 2.5开始,[PEP 342:通过增强生成器实现协同程序]的实现为生成器加入了更多的特性,这意味着生成器还可以完成更多的工作。使用生成

2016-01-24 12:50:17 362

原创 Python 闭包

Python 闭包函数对象函数在 Python 中是第一类对象,即函数可以作为一个变量的值以及另一个函数的参数或返回值。除此之外,函数还可以嵌套定义。把函数当作数据处理时,它将显式地携带与定义该函数的周围环境的相关信息。这将影响到函数中自由变量的绑定方式。例如,在 foo.py 文件中定义了一个变量 x 和 一个函数 callf:# foo.pyx = 42 # a global variabl

2016-01-22 13:34:57 590

原创 Python 装饰器( Decorator)

Python 装饰器( Decorator)Decorator 是 Python 中最重要的特性之一。本文尝试着从零开始解释 Decorator 是怎么工作的.函数(Function)是一个对象在 Python 中,一切皆是对象。这意味着即使一个函数被其他对象所包装(wrap),我们仍然可以通过这个对象名来进行调用。def traveling_function(): print "Here

2016-01-19 14:05:22 621

转载 Python 中的 *args 和 **kwargs 参数

Python 中的 *args 和 **kwargs 参数*args 语法使用 *args 语法,一个 python 函数可以在它的形式参数列表中声明接受零个或多个位置参数。 *args 将所有可变的位置参数组合到一个元组(tuple)里,这个元组可以在函数中访问。 # 形式参数示例def function_with_many_arguments(arg1, *args): print a

2016-01-18 23:32:09 454

原创 LeetCode 191: Number of 1 Bits

Number of 1 BitsWrite a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11’ has binary representati

2015-10-30 16:22:30 325

原创 LeetCode 190: Reverse Bits

Reverse BitsReverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00

2015-10-30 16:19:54 221

原创 LeetCode 169: Majority Element

Majority ElementGiven 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 the array is non-empty and the majority

2015-10-30 15:30:43 228

转载 LeetCode 137: Single Number II

Single Number IIGiven an array of integers, every element appears three times except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it

2015-10-30 15:17:04 209

原创 LeetCode 136: Single Number

Single NumberGiven an array of integers, every element appears twice except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it without u

2015-10-30 15:02:09 276

原创 LeetCode 78: Subsets

Given a set of distinct integers, nums, 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, If num

2015-10-30 14:57:55 244

原创 LeetCode 53: Maximum Subarray

Maximum SubarrayFind 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

2015-10-30 13:49:35 244

原创 LeetCode 63: Unique Paths II

Unique Paths IIFollow 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 as 1 and 0 respectively in

2015-10-30 13:37:44 209

原创 LeetCode 62: Unique Paths

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the botto

2015-10-30 10:41:31 231

原创 LeetCode 64: Minimum Path Sum

Minimum Path SumGiven a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down

2015-10-30 10:33:41 242

原创 LeetCode 70: Climbing Stairs

Climbing StairsYou are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?解题思路class Solution {p

2015-10-30 10:25:10 215

转载 LeetCode 91: Decode Ways

Decode WaysA message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total

2015-10-30 10:20:34 199

原创 LeetCode 120: Triangle

TriangleGiven 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

2015-10-29 22:32:01 214

转载 LeetCode 152: Maximum Product Subarray

Maximum Product SubarrayFind the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4], the contiguous subarray [2,

2015-10-29 22:13:35 234

原创 LeetCode 123: Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock IIISay you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most two tr

2015-10-29 21:24:21 288

原创 LeetCode 122: Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock IISay you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transac

2015-10-29 20:45:53 386

转载 LeetCode 121: Best Time to Buy and Sell Stock

Best Time to Buy and Sell StockSay you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sel

2015-10-29 20:27:24 225

转载 LeetCode 213: House Robber II

House Robber IINote: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention.

2015-10-29 19:29:30 222

原创 LeetCode 198: House Robber

House RobberYou are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adj

2015-10-29 19:01:01 223

原创 LeetCode 221: Maximal Square

Maximal SquareGiven a 2D binary matrix filled with 0’s and 1’s, find the largest square containing all 1’s and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0

2015-10-29 15:46:12 248

转载 LeetCode 264: Ugly Number II

Ugly Number IIWrite a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the

2015-10-29 15:18:20 258

原创 LeetCode 279: Perfect Squares

Perfect SquaresGiven a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.For example, given n = 12, return 3 because 12 = 4 + 4 + 4; giv

2015-10-29 14:42:33 242

原创 LeetCode 236: Lowest Common Ancestor of a Binary Tree

Lowest Common Ancestor of a Binary TreeGiven a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ance

2015-10-29 13:51:47 315

转载 LeetCode 235: Lowest Common Ancestor of a Binary Search Tree

Lowest Common Ancestor of a Binary Search TreeGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The

2015-10-29 10:40:15 215

原创 LeetCode 257: Binary Tree Paths

Binary Tree PathsGiven a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]解题思路代码如下:/**

2015-10-29 10:15:04 267

转载 LeetCode 230: Kth Smallest Element in a BST

Kth Smallest Element in a BSTGiven a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.Follo

2015-10-29 10:02:48 301

转载 LeetCode 145: Binary Tree Postorder Traversal

Binary Tree Postorder TraversalGiven a binary tree, return the postorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3},1 \ 2 /3return [3,2,1].Note: Recursive solution i

2015-10-28 22:20:12 232

原创 LeetCode 144: Binary Tree Preorder Traversal

Binary Tree Preorder TraversalGiven a binary tree, return the preorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3},1 \ 2 /3return [1,2,3].Note: Recursive solution is

2015-10-28 21:56:29 224

原创 LeetCode 129: Sum Root to Leaf Numbers

Sum Root to Leaf NumbersGiven a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.

2015-10-28 21:24:30 237

转载 LeetCode 124: Binary Tree Maximum Path Sum

Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-

2015-10-28 21:00:03 253

原创 LeetCode 117: Populating Next Right Pointers in Each Node II

Populating Next Right Pointers in Each Node IIFollow up for problem “Populating Next Right Pointers in Each Node”.What if the given tree could be any binary tree? Would your previous solution still wor

2015-10-28 19:52:32 346

原创 LeetCode 116: Populating Next Right Pointers in Each Node

Given a binary treestruct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;}Populate each next pointer to point to its next right node. If there is no next right node,

2015-10-28 19:09:30 421

空空如也

空空如也

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

TA关注的人

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