10 2013 档案

摘要:static 变量 头文件中的static会在引用该头文件的cpp中分别生成副本//H.h#ifndef _H_H_#define _H_H_static int a = 0;#endif//Ex_2.c#include "H.h"void fun_ex2 (){ a++; printf ("%d", a);//这时会打印1}//Ex_3#include "H.h"void fun_ex3 (){ printf ("%d", a);//这时打印的依然是0,即便先被Ex_2中的fun_ex2()函数修改过,因为他们 阅读全文
posted @ 2013-10-18 11:44 懒猫欣 阅读(151) 评论(0) 推荐(0)
摘要:windows默认使用\\linux默认使用/可以都用/ 阅读全文
posted @ 2013-10-16 10:53 懒猫欣 阅读(476) 评论(0) 推荐(0)
摘要:class A{ int a; char c; char b;};class B{ char c; int a; char b;};int main(int argc, char* argv[]){ cout<<sizeof(A)<<" "<<sizeof(B)<<endl; return 0;}输出结果为8,12class A1{};class A2{};class A3{};class A4{int a;};class A:A1,A2,A3{};class B:A4,A2,A3{};class C:A2,A4,A3{};i 阅读全文
posted @ 2013-10-14 23:20 懒猫欣 阅读(200) 评论(0) 推荐(0)
摘要:1.给一个长为n的链表,如何只扫描链表一次从中随机算出m个数(m..< 阅读全文
posted @ 2013-10-10 14:53 懒猫欣 阅读(123) 评论(0) 推荐(0)
摘要:Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "A 阅读全文
posted @ 2013-10-09 00:16 懒猫欣 阅读(183) 评论(0) 推荐(0)
摘要: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 one candy.Children with a higher rating get more candies than their neighbors.What is the minimum candies you 阅读全文
posted @ 2013-10-08 23:33 懒猫欣 阅读(362) 评论(0) 推荐(0)
摘要:Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"click to show corner cases.Corner Cases:Did you consider the case where path = "/../"?In this case, you should r 阅读全文
posted @ 2013-10-08 20:56 懒猫欣 阅读(188) 评论(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:boo 阅读全文
posted @ 2013-10-08 20:28 懒猫欣 阅读(339) 评论(0) 推荐(0)
摘要: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 O XX O X XAfter running your function, the board should be:X X X XX X X 阅读全文
posted @ 2013-10-08 15:08 懒猫欣 阅读(194) 评论(0) 推荐(0)
摘要:'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).The function prototype should be:bool isMatch(const char *s, const char *p)Some examples:isMatch("aa","a") → falseisM 阅读全文
posted @ 2013-10-08 14:19 懒猫欣 阅读(206) 评论(0) 推荐(0)
摘要:Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.首 阅读全文
posted @ 2013-10-08 00:32 懒猫欣 阅读(182) 评论(0) 推荐(0)
摘要:Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s. For example, given s = "aab",Return [ ["aa","b"], ["a","a","b"] ]输出总共2^n种,硬做就可以了class Solution {publi 阅读全文
posted @ 2013-10-07 23:05 懒猫欣 阅读(142) 评论(0) 推荐(0)
摘要:The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132""213""231""312""321"Given n and k, return the kth permutation se 阅读全文
posted @ 2013-10-07 21:32 懒猫欣 阅读(142) 评论(0) 推荐(0)
摘要:Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.目标是找出所有字母集合相同的词,例如aabc和abca通过hash就可以了class Solution {public: vector anagrams(vector &strs) { // Note: The Solution object is instantiated only once and is reused by each test ca... 阅读全文
posted @ 2013-10-07 20:56 懒猫欣 阅读(184) 评论(0) 推荐(0)
摘要:Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each 阅读全文
posted @ 2013-10-07 20:38 懒猫欣 阅读(203) 评论(0) 推荐(0)
摘要:Say 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 transactions.Note:You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).总共可以买卖 阅读全文
posted @ 2013-10-07 15:10 懒猫欣 阅读(153) 评论(0) 推荐(0)
摘要:Say 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 transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions 阅读全文
posted @ 2013-10-07 13:18 懒猫欣 阅读(150) 评论(0) 推荐(0)
摘要:Say 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 sell one share of the stock), design an algorithm to find the maximum profit.题目的意思是所有天加起来只能最多买一次买一次class Solution {public: int max... 阅读全文
posted @ 2013-10-07 13:09 懒猫欣 阅读(130) 评论(0) 推荐(0)
摘要:Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.据说没有递推公式class Solution {public: int check(int* X,int k){ for(int i=0;i=0) { X[j]++; if(X[j]==n) { j--; ... 阅读全文
posted @ 2013-10-07 00:02 懒猫欣 阅读(149) 评论(0) 推荐(0)
摘要:Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats", "and", "sand", "dog 阅读全文
posted @ 2013-10-06 23:10 懒猫欣 阅读(915) 评论(0) 推荐(0)
摘要:Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", return false.动态规划,前i个s1和前j个s2能否组成前i+jclass Solution {public: bool isInterleave 阅读全文
posted @ 2013-10-06 22:13 懒猫欣 阅读(163) 评论(0) 推荐(0)
摘要:Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BANC".Note:If there is no such window in S that covers all characters in T, return the 阅读全文
posted @ 2013-10-06 20:51 懒猫欣 阅读(190) 评论(0) 推荐(0)
摘要:Given a set of non-overlapping intervals, 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], 阅读全文
posted @ 2013-10-06 20:13 懒猫欣 阅读(167) 评论(0) 推荐(0)
摘要:Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].class Solution { struct IntvTreeNode{ int start; int end; int max; IntvTreeNode* left; IntvTreeNode* right; IntvTree... 阅读全文
posted @ 2013-10-06 19:10 懒猫欣 阅读(151) 评论(0) 推荐(0)
摘要: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 "()", which has length = 2.Another example is ")()())", where the lo 阅读全文
posted @ 2013-10-06 15:58 懒猫欣 阅读(163) 评论(0) 推荐(0)
摘要:Divide two integers without using multiplication, division and mod operator.思路是使用人做除法时的运算式 每次取前除数长度或加1位还原成数字,做减法求出该位的商,需要注意很多特殊情况。。class Solution {public: //a/b void Sub(string& a,string& b,string&ret,long long ib,stringstream& ss){ int i=0; string a1; bool all=false; ... 阅读全文
posted @ 2013-10-06 15:06 懒猫欣 阅读(171) 评论(0) 推荐(0)
摘要:Implement 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 order (ie, sorted in ascending order).The replacement must be in-place, do not allocate extra memory.He 阅读全文
posted @ 2013-10-06 11:13 懒猫欣 阅读(174) 评论(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 阅读全文
posted @ 2013-10-06 10:46 懒猫欣 阅读(108) 评论(0) 推荐(0)
摘要:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.应用直方图找矩形的函数 将每一行每一位上方的1的个数化为直方图,面积最大的矩形即都为1且面积最大的矩形class Solution {public: int largestRectangleArea(vector &height) { if(height.size()==0)return 0; stack > sta; ... 阅读全文
posted @ 2013-10-06 02:20 懒猫欣 阅读(208) 评论(0) 推荐(0)
摘要:Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].The largest rectangle is shown in the shaded area, which ha 阅读全文
posted @ 2013-10-06 02:04 懒猫欣 阅读(163) 评论(0) 推荐(0)
摘要:Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens = "leetcode",dict = ["leet", "code"].Return true because "leetcode" can be segmented as "l 阅读全文
posted @ 2013-10-06 00:40 懒猫欣 阅读(285) 评论(0) 推荐(0)
摘要: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 should return [1,2,3,6,9,8,7,4,5].class Solution {public: vector spiralOrder(vector > &matrix) { // No... 阅读全文
posted @ 2013-10-05 23:11 懒猫欣 阅读(166) 评论(0) 推荐(0)
摘要:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]n,n-1,n-1,n-2,n-2......,2,2,1,1class Solution {public: vector > generateMatrix(int n) { // ... 阅读全文
posted @ 2013-10-05 22:55 懒猫欣 阅读(142) 评论(0) 推荐(0)
摘要: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 tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its zigzag level order traversal as:[ ... 阅读全文
posted @ 2013-10-05 22:37 懒猫欣 阅读(214) 评论(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: void Plus(string& a,string& b,string& temp){ int small=min(a.length(),b.length()); bool flag=false; ... 阅读全文
posted @ 2013-10-05 21:58 懒猫欣 阅读(162) 评论(0) 推荐(0)
摘要: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.class Solution {public: RandomListNode *copyRandomList(RandomListNode *head) { // Note: The Solution object is instantiated only... 阅读全文
posted @ 2013-10-05 19:23 懒猫欣 阅读(330) 评论(0) 推荐(0)
摘要:Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?class Solution {public: vector getRow(int rowIndex) { // Note: The Solution object is instantiated only once and is r... 阅读全文
posted @ 2013-10-05 18:46 懒猫欣 阅读(120) 评论(0) 推荐(0)
摘要:Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]class Solution {public: vector > generate(int numRows) { // Note: The Solution object is instantiated only once and is reused by each... 阅读全文
posted @ 2013-10-05 18:35 懒猫欣 阅读(113) 评论(0) 推荐(0)
摘要:Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each row is greater than the last integer of the previous row.For example,Consider the following matrix:[ [1, .. 阅读全文
posted @ 2013-10-05 18:27 懒猫欣 阅读(175) 评论(0) 推荐(0)
摘要:Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].第一个版本,加了一个set去除重复class Solution {public: void sub(vector > & ret,vector& num,int index){ if(index= 阅读全文
posted @ 2013-10-05 18:14 懒猫欣 阅读(167) 评论(0) 推荐(0)
摘要:Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].class Solution {public: void sub(vector > & ret,vector& num,int index){ if(index==num.size()){ ret.push_b... 阅读全文
posted @ 2013-10-05 17:41 懒猫欣 阅读(162) 评论(0) 推荐(0)
摘要:Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the two partitions. For example,Given 1->4->3->2->5->2 and x = 3,return 1->2->2-&g 阅读全文
posted @ 2013-10-05 17:21 懒猫欣 阅读(162) 评论(0) 推荐(0)
摘要: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 any intervening characters. For example, given:S: "barfoothefoobarman"L: ["foo", & 阅读全文
posted @ 2013-10-05 01:52 懒猫欣 阅读(170) 评论(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 of every node never differ by more than 1.class Solution {public: int sub(TreeNode* root){ if(root==NULL)return 0; ... 阅读全文
posted @ 2013-10-04 17:09 懒猫欣 阅读(129) 评论(0) 推荐(0)
摘要: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 \ 2 \ 3 \ 4 \ 5 \ 6click to show hints.Hints:If ... 阅读全文
posted @ 2013-10-04 16:36 懒猫欣 阅读(148) 评论(0) 推荐(0)
摘要:Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be only one unique solution. A sudoku puzzle......and its solution numbers marked in red.简单的回溯搜索即可class Solution {public: void solveSudoku(vector 阅读全文
posted @ 2013-10-04 16:16 懒猫欣 阅读(242) 评论(0) 推荐(0)
摘要: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 filled sudoku which is valid.class Solution {public: bool isValidSudoku(vector > &board) { // Note: The 阅读全文
posted @ 2013-10-04 14:28 懒猫欣 阅读(156) 评论(0) 推荐(0)
摘要:You 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 {public: int climbStairs(int n) { // Note: The Solution object is instantiated only once and is reused by each ... 阅读全文
posted @ 2013-10-04 13:56 懒猫欣 阅读(118) 评论(0) 推荐(0)
摘要:Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]class Solution {public: void sub(vector >& ret,int n,int k){ if(n >copy=ret; for(int i=0;i > comb... 阅读全文
posted @ 2013-10-04 12:36 懒猫欣 阅读(157) 评论(0) 推荐(0)
摘要:Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / ... 阅读全文
posted @ 2013-10-04 12:16 懒猫欣 阅读(196) 评论(0) 推荐(0)
摘要:Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 ... 阅读全文
posted @ 2013-10-04 03:03 懒猫欣 阅读(165) 评论(0) 推荐(0)
摘要:Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) Insert a characterb) Delete a characterc) Replace a characterclass Solution {public: int minDistan... 阅读全文
posted @ 2013-10-04 02:05 懒猫欣 阅读(147) 评论(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 阅读全文
posted @ 2013-10-04 00:39 懒猫欣 阅读(124) 评论(0) 推荐(0)
摘要: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 not matter)class Solution {public: void sub(string &s,string&ret,i 阅读全文
posted @ 2013-10-04 00:28 懒猫欣 阅读(187) 评论(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 3Return 6./** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNo... 阅读全文
posted @ 2013-10-03 23:38 懒猫欣 阅读(142) 评论(0) 推荐(0)
摘要:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree./** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NU... 阅读全文
posted @ 2013-10-03 22:54 懒猫欣 阅读(159) 评论(0) 推荐(0)
摘要:Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree./** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(N... 阅读全文
posted @ 2013-10-03 21:46 懒猫欣 阅读(192) 评论(0) 推荐(0)
摘要:Given 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 without using extra memory?class Solution {public: void toTribit(vector& bits,int val){ if(val0){... 阅读全文
posted @ 2013-10-03 21:27 懒猫欣 阅读(406) 评论(0) 推荐(0)
摘要:There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.Return the sta 阅读全文
posted @ 2013-10-03 16:58 懒猫欣 阅读(216) 评论(0) 推荐(0)
摘要:Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.As an example, consider the seria 阅读全文
posted @ 2013-10-03 00:58 懒猫欣 阅读(263) 评论(0) 推荐(0)
摘要:Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water 阅读全文
posted @ 2013-10-02 18:10 懒猫欣 阅读(145) 评论(0) 推荐(0)
摘要: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 neighboring. The same letter cell may not be used more than once. For example,Given board = [ 阅读全文
posted @ 2013-10-02 17:21 懒猫欣 阅读(152) 评论(0) 推荐(0)
摘要:Given an array of integers, all elements are repeated twice except for one. Find that single one.Could you implement it without using extra memory?牢记异或,愿异或母亲忽悠着你class Solution {public: int singleNumber(int A[], int n) { // Note: The Solution object is instantiated only once and is reused b... 阅读全文
posted @ 2013-10-02 16:35 懒猫欣 阅读(162) 评论(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], return true.A = [3,2,1,0,4], return fal 阅读全文
posted @ 2013-10-02 10:29 懒猫欣 阅读(143) 评论(0) 推荐(0)