c++
文章平均质量分 58
taoqick
这个作者很懒,什么都没留下…
展开
-
LeetCode 363. Max Sum of Rectangle No Larger Than K 红黑树无法用栈取代
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k.Example:Input: matrix = [[1,0,1],[0,-2,3]], k = 2Output: 2 Explanation:Because the sum of rectangle [[0, 1], [-.原创 2020-07-12 11:36:38 · 171 阅读 · 0 评论 -
100亿零1个数找中位数 最少读几次磁盘
2g内存,磁盘上有100亿零1个32bit 的uint,找中位数,要求读磁盘次数最少很容易想到桶排序,问题是桶的宽度。由于统计个数,某个桶内可能超过100亿,所以int不行,必须long long2g内存能放多少long long呢?2g内存 = 2∗210M2*2^{10}M2∗210M=2∗220K2*2^{20}K2∗220K=2∗230bytes2*2^{30} bytes2∗230bytes(刚开始脑子秀逗,认为2g内存 = 2∗23M2*2^{3}M2∗23M=2∗26K2*2^{6}原创 2020-07-08 20:41:18 · 265 阅读 · 0 评论 -
Leetcode 282. Expression Add Operators
Given a string that contains only digits0-9and a target value, return all possibilities to addbinaryoperators (not unary)+,-, or*between the digits so they evaluate to the target value.Examp...原创 2020-03-09 23:19:16 · 159 阅读 · 0 评论 -
STL set multiset map multimap unordered_set unordered_map example
I decide to write to my blogs in English. When I meet something hard to depict, I'll add some Chinese necessarily. The differences between these containers are :The keys of set and map are原创 2013-09-30 17:05:59 · 1198 阅读 · 0 评论 -
strcpy函数 面试题
想起前两天有人发的面试题,其中有strcmp的实现,转这个过来,给大家看看。题目: 已知strcpy函数的原型是: char * strcpy(char * strDest,const char * strSrc); 1.不调用库函数,实现strcpy函数。 2.解释为什么要返回char *。 解说: 1.st转载 2013-09-26 10:53:03 · 821 阅读 · 0 评论 -
快速排序 递归转非递归
递归程序巧妙地完成了回溯,那么如何将递归程序转换成非递归呢?一般来说,递归的时候一定要分清哪些是可以直接由顶进行到底的(不需要回溯,即手头可以处理好的事件),哪些是需要回溯的(这部分可能根据回溯的顺序来排回溯的优先级,需要标注不同的状态,即待办事件),贴网上某人的博文,觉得总结的非常好。通用法则为:以下是如何把快速排序由递归程序转换为非递归程序:递归的版本为: v...原创 2013-09-12 18:50:44 · 1119 阅读 · 0 评论 -
leetcode Sqrt(x) Binary Search
Pay attention to that mid * mid may be larger than int. So long long is enough. class Solution { public: int sqrt(int x) { // Note: The Solution object is instantiated only once and is reus原创 2013-10-24 20:48:03 · 942 阅读 · 0 评论 -
c/c++数据范围
char -128 ~ +127 (1 Byte)short -32767 ~ + 32768 (2 Bytes)unsigned short 0 ~ 65536 (2 Bytes)int -2147483648 ~ +2147483647 (4 Bytes)uns转载 2013-09-05 16:58:42 · 1972 阅读 · 0 评论 -
Leetcode 47. Permutations II next_perm 下一个排列 注意包含重复的情况
I'll introduce a method without using the permutation tree.From the right to the left, find the first num[i-1] < num [i], record ii = i-1;Find the minimal number larger than num[ii] from ii's ...原创 2013-10-27 21:07:00 · 1030 阅读 · 0 评论 -
leetcode Rotate Image
You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the imagein-place, which means you have to modify the input 2D matrix d...原创 2013-10-27 16:46:45 · 952 阅读 · 0 评论 -
leetcode implement strStr
I wanna make a summary for KMP. Next[j] means that max{ Next[j]| needle[j-Next[j]..j-1] == needle[0..Next[j]-1] }, i.e.,at most k characters before needle[j] are matching the first k characters o原创 2013-10-24 00:12:45 · 1211 阅读 · 0 评论 -
正确使用stl map的erase方法
STL的map表里有一个erase方法用来从一个map中删除掉指令的节点eg:mapstring,string> mapTest;typedef mapstring,string>::iterator ITER;ITER iter=mapTest.find(key);mapTest.erase(iter); 像上面这样只是删除单个节点,map的形为不会出现任转载 2013-11-21 15:50:01 · 871 阅读 · 0 评论 -
leetcode Binary Tree Postorder Traversal
Given 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 solut原创 2013-11-23 10:18:00 · 1224 阅读 · 0 评论 -
leetcode String to Integer (atoi)
Requirements for atoi:The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optiona原创 2013-11-10 23:09:37 · 895 阅读 · 0 评论 -
leetcode 143 Reorder List 单链表
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution { public: ListNode* rev原创 2013-11-24 15:59:35 · 990 阅读 · 0 评论 -
leetcode Interleaving String
First, I wrote a DFS program using a stack, however, TLEclass Solution { public: bool isInterleave(string s1, string s2, string s3) { int i = 0, j = 0, k = 0; stack s3stack, s2stack;原创 2013-11-12 21:16:57 · 913 阅读 · 0 评论 -
leetcode Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without an原创 2013-11-26 20:05:45 · 1010 阅读 · 0 评论 -
LeetCode 54. Spiral Matrix 剑指 Offer 29. 顺时针打印矩阵
Take control of the 4 key points:class Solution { public: vector spiralOrder(vector > &matrix) { vector res; int rownum = matrix.size(), colnum = matrix.size() ? matrix[0].size() : 0, i,原创 2013-11-13 13:16:09 · 964 阅读 · 0 评论 -
leetcode Clone Graph
Clone Graph Total Accepted: 2649 Total Submissions: 14045My SubmissionsClone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ's undirecte原创 2013-11-27 15:59:49 · 1236 阅读 · 0 评论 -
leetcode 75. Sort Colors 荷兰国旗
Given an array withnobjects colored red, white or blue, sort themin-placeso that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the in...原创 2013-11-14 20:42:25 · 830 阅读 · 0 评论 -
leetcode Multiply Strings
Multiply Strings Total Accepted: 1717 Total Submissions: 9415My SubmissionsGiven two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers原创 2013-11-28 23:23:34 · 608 阅读 · 0 评论 -
leetcode Single Number II
Single Number II Total Accepted: 4273 Total Submissions: 14218My SubmissionsGiven an array of integers, every element appears three times except for one. Find that single one.Note:Yo原创 2013-12-02 11:02:51 · 871 阅读 · 0 评论 -
leetcode Search Insert Position
Search Insert PositionTotal Accepted:4060Total Submissions:12013My SubmissionsGiven a sorted array and a target value, return the index if the target is found. If not, return the index w...原创 2013-12-03 09:51:32 · 763 阅读 · 0 评论 -
leetcode Container With Most Water
Container With Most Water Total Accepted: 2685 Total Submissions: 9008My SubmissionsGiven n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).原创 2013-12-02 18:48:11 · 884 阅读 · 0 评论 -
leetcode Unique Paths II
Unique Paths II Total Accepted: 2092 Total Submissions: 8049My SubmissionsFollow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths woul原创 2013-12-03 21:03:10 · 793 阅读 · 0 评论 -
boj1924 麻将判胡
递归,深搜!原创 2012-04-04 19:04:26 · 4320 阅读 · 1 评论 -
wikioi 1074 食物链 并查集
一个并查集的问题原创 2013-08-19 14:21:49 · 1101 阅读 · 0 评论 -
POJ 1088 DFS 同时记录深搜过的位置
听说是经典题目了,重点练一下递归如何调试,重点理解自顶向下和自底向上,重点练习递归如何避免重复计算。另外关于DP函数的理解经常是记录从**开始之后的结果,或者走到**步时的结果,具体先上代码。#include#include#include#include#include#include#include#includeusing namespace std;int R,C原创 2012-04-05 15:47:55 · 666 阅读 · 0 评论 -
poj 1985 重点学习图的存储方法
这么存图,对于u-->v,边是和v绑定的,next指向下一条边,adj[u]指向和u相连的众多边的一个边的一个点,存图的代码如下:void add(int u,int v,int w){//u-->v edges[e].v=v; edges[e].w=w; edges[e].next=adj[u]; //边的下标 adj[u]=原创 2012-04-05 16:40:20 · 660 阅读 · 0 评论 -
poj 3273 二分法
老实说这道题开始并没有往2分这个方向想,后来看了网上大牛的攻略之后自己敲的各种弱,先总结一下二分吧。二分最流行的进入下一轮的方法是l=mid+1; r=mid-1; 当然可以和l=mid 和 r=mid 配合,但是需要有一定前提(见后面代码)。纯使用l=mid 和 r=mid会导致死循环,原因就不多讲了。先贴一个特别2的程序:#include#inclu原创 2012-04-07 19:02:31 · 599 阅读 · 0 评论 -
几个大数据的问题
2、搜索引擎会通过日志文件把用户每次检索使用的所有检索串都记录下来,每个查询串的长度为1-255字节。 假设目前有一千万个记录(这些查询串的重复度比较高,虽然总数是1千万,但如果除去重复后,不超过3百万个。一个查询串的重复度越高,说明查询它的用户越多,也就是越热门。),请你统计最热门的10个查询串,要求使用的内存不能超过1G。 典型的Top K算法,还是在这篇文章里头有所阐转载 2014-01-01 17:41:48 · 635 阅读 · 0 评论 -
poj 2342 Anniversary party 树形DP
看了网上很多关于这道题的代码,感觉很多人写的都很长,自己AC了一下,贴到这里。其实拿到这道题感觉DP的思路自己还是可以找到的,没有想到的是如何存树,用邻接表存代码会较长,而且维护起来比较多。比较简洁的方法是用一个vector数组来存,vector[i]保存第i个结点的所有children。还有一点没有想到的是如何较方便地保存DP的数据,用一个二维数据即可。另外有一点就是可以原创 2013-01-10 16:04:28 · 664 阅读 · 0 评论 -
LeetCode 132. Palindrome Partitioning II DP 深搜
Palindrome Partitioning IITotal Accepted:2828Total Submissions:16953My SubmissionsGiven a strings, partitionssuch that every substring of the partition is a palindrome.Return the minim...原创 2013-09-08 17:41:59 · 885 阅读 · 0 评论 -
leetcode Word Ladder II hash BFS
Word Ladder II Total Accepted: 1447 Total Submissions: 17873My SubmissionsGiven two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such...原创 2013-10-09 20:35:00 · 1322 阅读 · 0 评论 -
leetcode Best Time to Buy and Sell Stock II
We should buy the stock at the starting point of ascending status, and sell the stock at the ending point of ascending status. The code is :class Solution {public: int maxProfit(vector &price原创 2013-10-14 14:56:28 · 947 阅读 · 0 评论 -
leetcode 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 ...原创 2013-10-15 15:26:30 · 1083 阅读 · 0 评论 -
leetcode 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.For this problem, a height-balanced binary tree is defined as a binary tree in which the ...原创 2013-10-16 11:11:17 · 1422 阅读 · 0 评论 -
leetcode Decode Ways I & II Divide and Conquer
A message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the...原创 2013-10-18 22:15:08 · 1063 阅读 · 0 评论 -
leetcode Valid Number
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 ambig...原创 2013-10-25 16:50:24 · 773 阅读 · 0 评论 -
leetcode Longest Valid Parentheses
Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring.For"(()", the longest valid parentheses substring is"()", whi...原创 2013-10-26 00:35:12 · 1100 阅读 · 0 评论