- 博客(1069)
- 资源 (3)
- 收藏
- 关注
原创 Linux进程间通信--mmap共享内存(一)
共享内存可以说是最有用的进程间通信方式,也是最快的IPC形式。两个不同进程A、B共享内存的意思是,同一块物理内存被映射到进程A、B各自的进程地址空间。进程A可以即时看到进程B对共享内存中数据的更新,反之亦然。由于多个进程共享同一块内存区域,必然需要某种同步机制,互斥锁和信号量都可以。采用共享内存通信的一个显而易见的好处是效率高,因为进程可以直接读写内存,而不需要任何数据的拷贝。对于像管道和消息
2015-04-20 20:03:19 6220
原创 实现一个内存池管理的类
模拟STL中的freelist,有这个思想在内。union obj{ union obj* next; char p[1]; };class MemoryPool{ public: MemoryPool() { union obj* temp; m_
2015-04-16 21:13:14 1194
原创 STL源码剖析--空间配置器
STL的设计非常巧妙,组件间互取短长,形成了一个世界,这是这个世界里的组件:1. containers(容器):所谓容器,是指存放数据的地方,将数据以一定的方法组织存放。根据不同的组织方式,可以把容器分为顺序容器,如vector、deque、list,关联容器,如set、map。Container是一种class template。2. algorithm(算法):各种常用不常用的算法如s
2015-04-16 20:59:39 1054
原创 Implement strStr()--LeetCode
对于最好的方法是KMP,但是KMP很麻烦。#include int strStr(string& haystack, string& needle) { if(haystack.length()==0 || needle.length()==0) return 0; int i,j; for(i=0;i<=haystack.length()-needle.
2015-04-14 16:48:25 688
原创 Restore IP Addresses--LeetCode
Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given "25525511135",return ["255.255.11.135", "255.255.111.35"]. (Order does
2015-04-14 16:34:15 628
原创 Word Ladder--LeetCode
这道题看似一个关于字符串操作的题目,其实要解决这个问题得用图的方法。我们先给题目进行图的映射,顶点则是每个字符串,然后两个字符串如果相差一个字符则我们进行连边。接下来看看这个方法的优势,注意到我们的字符集只有小写字母,而且字符串长度固定,假设是L。那么可以注意到每一个字符可以对应的边则有25个(26个小写字母减去自己),那么一个字符串可能存在的边是25*L条。接下来就是检测这些边对应的字符串是否在
2015-04-14 16:20:58 856
原创 Gas Station--LeetCode
There are N gas stations along a circular route, where the amount of gas at stationi is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from stationi to it
2015-04-14 16:15:32 636
原创 Best Time to Buy and Sell Stock IV--LeetCode
Say you have an array for which the ith element is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete at most k transactions.Note:You may not
2015-04-14 15:47:39 1275
原创 Repeated DNA Sequences--LeetCode
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.Write
2015-04-14 15:32:30 766
原创 Copy List with Random Pointer--LeetCode
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.这是一道链表操作的题目,要求复制一个链表,不过链表的每个结点
2015-04-14 15:06:18 770
原创 House Robber--LeetCode
You 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 adjacent house
2015-04-14 11:47:56 1614
原创 Binary Search Tree Iterator--LeetCode
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next() and
2015-04-14 11:30:10 923
原创 LRU Cache--LeetCode
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 t
2015-04-14 11:09:52 969
原创 Compare Version Numbers--LeetCode
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 version2 return -1, otherwise return 0.You may assume that the version strings are non-empty and cont
2015-04-14 09:20:43 757
原创 Candy--LeetCode
There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least on
2015-04-13 21:04:47 589
原创 Binary Tree Zigzag Level Order Traversal--LeetCode
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
2015-04-13 20:39:45 577
原创 Word Search--LeetCode
Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically n
2015-04-13 20:13:42 632
原创 Scramble String--LeetCode
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great / \ gr
2015-04-13 19:52:05 566
原创 N-Queens II--LeetCode
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.思路:使用组合的方式来判断,加入每一列排放一个,那么看行怎么排序,如果0~n的数,进行全排列,第i个位置上的数是什么代表第I个皇后放到这个列。
2015-04-13 16:13:21 557
原创 Wildcard Matching--LeetCode
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
2015-04-13 14:59:38 652
原创 3Sum Closest--LeetCode
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly
2015-04-13 12:28:16 554
原创 Next Permutation--LeetCode
mplement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible or
2015-04-13 11:46:44 691
原创 Merge Sorted Array--LeetCode
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 (size that is greater or equal to m +n) to hold additional elements from B
2015-04-13 11:37:24 679
原创 Swap Nodes in Pairs--LeetCode
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 algorithm should use only constant space. You m
2015-04-13 10:53:43 655
原创 Merge Two Sorted Lists--LeetCode
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.List* mergeTwoLists(List* l1,List* l2){ List* head
2015-04-13 10:01:41 550
原创 Reverse Words in a String--LeetCode
Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".Update (2015-02-12):For C programmers: Try to solve it in-place in O(1
2015-04-13 09:39:23 1116
原创 Regular Expression Matching--LeetCode
这个题目比较常见,但是难度还是比较大的。我们先来看看brute force怎么解决。基本思路就是先看字符串s和p的从i和j开始的子串是否匹配,用递归的方法直到串的最后,最后回溯回来得到结果。假设现在走到s的i位置,p的j位置,情况分为下列两种: (1)p[j+1]不是'*'。情况比较简单,只要判断当前s的i和p的j上的字符是否一样(如果有p在j上的字符是'.',也是相同),如果不同,返回fal
2015-04-12 21:16:36 801
原创 Longest Consecutive Sequence--LeetCode
题目:Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is
2015-04-12 21:03:16 663
原创 Surrounded Regions--LeetCode
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. 例如:输入:X X X XX O O
2015-04-12 20:53:02 726
原创 Max Points on a Line--LeetCode
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.思路:二维坐标上的点,最多的共线的点数。共线:要么是垂直X轴,要么垂直与Y轴,要么就是斜线,一个坐标点,按照X排序,比较垂直与X轴的最多的点,按照Y轴排序,比较垂直Y轴的最多的点。至于怎么看
2015-04-12 15:03:00 547
原创 Evaluate Reverse Polish Notation--LeetCode
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", "+",
2015-04-12 13:17:31 491
原创 Maximum Product Subarray--LeetCode
Find 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,3] has the largest pr
2015-04-12 12:43:29 560
原创 Excel Sheet Column Number--LeetCode
Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB 思路:进制之间
2015-04-12 11:06:00 568
原创 Excel Sheet Column Number--LeetCode
Given a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 Credits:思路:相
2015-04-12 10:23:42 608
原创 Factorial Trailing Zeroes--LeetCode
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity. 思路:对于一个数的阶乘后面有多少个0,一个数 n 的阶乘末尾有多少个 0 取决于从 1 到 n 的各个数的因子中 2 和 5 的个数,
2015-04-12 10:00:06 684
原创 Binary Tree Right Side View--LeetCode
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary tree, 1
2015-04-12 09:36:44 769
原创 Majority Element--LeetCode
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 the array is non-empty and the majority element alw
2015-04-12 09:16:24 666
原创 Number of 1 Bits--LeetCode
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as theHamming weight).For example, the 32-bit integer ’11' has binary representation 000000000
2015-04-11 23:21:13 442
原创 Reverse Bits--LeetCode
Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as0011100101
2015-04-11 23:16:39 653
原创 Rotate Array--LeetCode
Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to[5,6,7,1,2,3,4]. 思路:三次翻转。#include #include using namespace std
2015-04-11 21:52:44 470
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人