10 2015 档案
摘要:Given a matrix ofmxnelements (mrows,ncolumns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ],...
阅读全文
摘要: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 maximu...
阅读全文
摘要: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 "adjace...
阅读全文
摘要:Given an integer array of sizen, find all elements that appear more than⌊ n/3 ⌋times. The algorithm should run in linear time and in O(1) space.求主元素,这...
阅读全文
摘要: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,...
阅读全文
摘要:Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possibl...
阅读全文
摘要:Given an array ofnintegers wheren> 1,nums, return an arrayoutputsuch thatoutput[i]is equal to the product of all the elements ofnumsexceptnums[i].Solv...
阅读全文
摘要:Follow up for "Remove Duplicates":What if duplicates are allowed at mosttwice?For example,Given sorted arraynums=[1,1,1,2,2,3],Your function should re...
阅读全文
摘要:Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete a...
阅读全文
摘要:Given an arraySofnintegers, are there elementsa,b,c, anddinSsuch thata+b+c+d= target? Find all unique quadruplets in the array which gives the sum of ...
阅读全文
摘要:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?题目要就地算法,那么可以通过折叠矩阵上...
阅读全文
摘要:Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from l...
阅读全文
摘要:Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the ord...
阅读全文
摘要:Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place.这题要注意的就是应该是就地执行,所以可以先采取一个数组将所有0元素的位置记下来,然后一次性的全部置成0即可: 1 clas...
阅读全文
摘要:Given an array of sizen, find the majority element. The majority element is the element that appears more than⌊ n/2 ⌋times.You may assume that the arr...
阅读全文
摘要:Given two sorted integer arraysnums1andnums2, mergenums2intonums1as one sorted array.Note:You may assume thatnums1has enough space (size that is great...
阅读全文
摘要:Given an indexk, return thekthrow of the Pascal's triangle.For example, givenk= 3,Return[1,3,3,1].还是帕斯卡三角,只不过这里指定的是某一个特定的层,然后直接返回,这个就可以使用从后往前更新数组的方法,其...
阅读全文
摘要:GivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]...
阅读全文
摘要:Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.首先是O(N)空间的方法,用递归: 1 /** 2 * Definit...
阅读全文
摘要:Given 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...
阅读全文
摘要:Given a binary tree, return thepostordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[3,2,1].No...
阅读全文
摘要:Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution stil...
阅读全文
摘要:Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointe...
阅读全文
摘要:Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1...
阅读全文
摘要:Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or ...
阅读全文
摘要: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 and sum
阅读全文
摘要:Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. Note: Recur
阅读全文
摘要:Given 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 whic
阅读全文
摘要: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
阅读全文
摘要:Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree.同样是给出了不会有重复数字
阅读全文
摘要:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.从前序以及中序的结果中构造二叉...
阅读全文
摘要:Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree...
阅读全文
摘要:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.讲一个排序好的数组转换成二叉搜索树,这题没想出来,基本上是参考别人的,边界条件应该注意一下: 1 /** ...
阅读全文
摘要:Given acompletebinary tree, count the number of nodes.Definition of a complete binary tree fromWikipedia:In a complete binary tree every level, except...
阅读全文
摘要: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
阅读全文
摘要:Given a binary search tree, write a functionkthSmallestto find thekth smallest element in it.Note:You may assume k is always valid, 1 ≤ k ≤ BST's tota...
阅读全文
摘要:Given a binary tree, imagine yourself standing on therightside of it, return the values of the nodes you can see ordered from top to bottom.For exampl...
阅读全文
摘要:Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Callingnext()will return the next...
阅读全文
摘要:Given a binary tree, return thebottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For exa...
阅读全文
摘要:Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree{3,9,2...
阅读全文
摘要:Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1Trivia:This problem was inspired bythis...
阅读全文
摘要:Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to thedefinition of LCA on Wikipedia: ...
阅读全文
摘要: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 le...
阅读全文
摘要: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.Fo...
阅读全文
摘要: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 / \ ...
阅读全文
摘要: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 an...
阅读全文
摘要:Given 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"...
阅读全文
摘要: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, wh...
阅读全文
摘要:Sort a linked list inO(nlogn) time using constant space complexity.题目要求在常数控件内以O(nlogn)的事件复杂度来排序链表。常数空间内我没有实现,O(nlogn)的话使用归并排序就可以了吗, 下面是代码: 1 /** 2 * ...
阅读全文
摘要:Given a list of non negative integers, arrange them such that they form the largest number.For example, given[3, 30, 34, 5, 9], the largest formed num...
阅读全文
摘要: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 fol...
阅读全文
摘要: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 i...
阅读全文
摘要:A robot is located at the top-left corner of amxngrid (marked 'Start' in the diagram below).The robot can only move either down or right at any point ...
阅读全文
摘要:在往上看到这样的题目:大体的有如下几个思路: 1一个栈维护队列的内容,当入队列的操作的时候就直接入s1栈,当出队列的时候就现将s1栈底以外的内容都push到s2中保存起来,将s1栈底返回后将s2内容在弹回来。 2 s1作为压栈之用,s2作为出栈之用。入队列的时候就直接检查s1中有无对象,没有检查...
阅读全文
摘要:Given a stringsconsists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string.If the last word doe...
阅读全文
摘要:Implement a basic calculator to evaluate a simple expression string.The expression string may contain open(and closing parentheses), the plus+or minus...
阅读全文
摘要:Given a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3} 1 \ 2 / 3return[1,2,3].前序遍历二叉...
阅读全文
摘要:Given a sorted integer array without duplicates, return the summary of its ranges.For example, given[0,1,2,4,5,7], return["0->2","4->5","7"].就是概括区间的方式...
阅读全文
摘要: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...
阅读全文
摘要:Given a collection of integers that might contain duplicates,nums, return all possible subsets.Note:Elements in a subset must be in non-descending ord...
阅读全文
摘要:Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be
阅读全文
摘要:Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return
阅读全文
摘要: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 yo...
阅读全文
摘要:Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path.Note:Yo...
阅读全文
摘要: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 le...
阅读全文
摘要:Rotate an array ofnelements to the right byksteps.For example, withn= 7 andk= 3, the array[1,2,3,4,5,6,7]is rotated to[5,6,7,1,2,3,4].将数组的内容倒置,看例子就知道是...
阅读全文
摘要: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...
阅读全文
摘要:如果operator new接收到的参数除了size_t之外还有其他的话,那么这个operator new实际上就是一个placement new,所以考虑下下面这样的情况:一个可以用来记录信息的placement new:1 class Widget{2 public:3 ...4 ...
阅读全文
摘要:几种最常见的这么做的理由: 1.用来检测运行上的错误:可以在分配的内存空间的起始以及结束分别放置单独的签名 2.为了强化性能 3.为了收集使用上的统计数据按照第一点就可以举一个例子: 1 static const int signature = 0XDEADBEEF; 2 typedef us...
阅读全文
摘要:C++中delete一个指针之后,只是回收指针指向位置的空间,而指针本身的值不变。你需要手工将其赋值为NULL。注意的一点是delete NULL指针的时候不会有任何的事情发生小结: operator new内应该有个个无穷循环,若在其中无法返回内存需求,就调用new-handler,这里面也应该...
阅读全文
摘要:多线程下的内存管理与单线程下是完全不同的,因为heap是一个可以被全局改动的资源,所以所有的线程都有可能去访问这一资源,这回导致很多的race_conditions。当operator new未取得想要的内存的时候,会调用一个用户指定的处理函数,new_handler。 这个函数可以使用set_ne...
阅读全文
摘要:1 void adance(std::list::iterator& iter, int d) 2 { 3 if(typeid(std::iterator_traits::iterator>::iterator_catagory) 4 ]== typeid(std::ra...
阅读全文
摘要:在stl的算法中,我们的希望往往是根据不同的迭代器类型进行不同的更有效率的操作: 1 template 2 void advance(IterT iter, DistT dis) 3 { 4 if(iter is a random access iterator) 5 ite...
阅读全文
摘要:看看下面这个例子: 1 template 2 class Rational{ 3 public: 4 Rational(const T & numerator, const T & denominator); 5 const T numerator()const; 6 con...
阅读全文
摘要:首先看看下面的真实的指针与模板创建的智能指针之间的区别:1 class Top{...};2 class Middle : public Top{...};3 class Bottom : public Middle{...};4 Top * p1 = new Bottom();5 Top * p2...
阅读全文
摘要:使用template时,不小心的时候可能就会带来代码膨胀的问题:1 template3 class SquareMatrix{4 public:5 void invert();6 };7 //而对于下面两份模版实例化8 SquareMatrix ma10;9 SquareMatrix ma5...
阅读全文
摘要:typename在很多种情况下与class是完全相同的,例如下面的使用:1 templame2 ......3 template4 ......条款42:了解typename的双重含义 1 template 2 void print2nd(const C containter) 3 { 4 ...
阅读全文
摘要:相对于继承体系来说,template实际上也使用接口与多态,而继承体系往往使用到的是显式接口以及运行期多态,而template实际上用的是隐式接口以及编译期多态。隐式接口实际上就是类似下面这样的东西:1 template 2 int myTypes(T & tmp)3 {4 tmp.size...
阅读全文
摘要:多重继承很容易带来的一个问题就是歧义的问题: 1 class BorrowAble{ 2 public: 3 void checkOut() const; 4 ... 5 }; 6 class ElectronicGadget{ 7 private: 8 bool check...
阅读全文
摘要:首先看一下private继承的法则:class之间的继承关系如果是private的话,那么编译器不会将一个derived对象自动当作为一个base class对象。从base class继承而来的所有方法以及属性,在derived class都会变成是private的。private继承的底层含义实...
阅读全文
摘要:在继承一个virtual函数的时候,如果这个virtual函数同样是有默认值的话,那么其表明这次继承既存在动态绑定也存在静态绑定:例如下面这个例子: 1 class Shape{ 2 public: 3 enum shapeColor{red, green, blue}; 4 vir...
阅读全文
摘要:重新定义一个继承而来的non-virtual函数可能会使得导致当函数被调用的时候,被调用的函数不是取决于调用的函数究竟属于的对象,而是取决于调用函数的指针或者引用的类型。所以一般的说主要有两种观点在这方面:1. 如果D非要重新继承而来的函数的话,那么说明他们的关系可能是不适合public继承的,因为...
阅读全文
摘要:有一部分人总是主张virtual函数几乎总应该是private:例如下面这个例子,例子时候游戏,游戏里面的任务都拥有健康值这一属性:class GameCharacter{public: int healthValue()const{ ... int retVal ...
阅读全文
摘要:在普通的public继承中,实际上也分为接口以及实现继承。首先看下面这个例子: 1 class Shape{ 2 public: 3 virtual void draw() const = 0; 4 virtual void error(const std::string & msg...
阅读全文
摘要:首先看下下面这个例子:class Base{private: int x;public: virtual void mf1() = 0; virtual void mf2(); void mf3(); ...};class Derived : public Base{p...
阅读全文
摘要:Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3,4], [2,3],...
阅读全文
摘要:Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is1 -> 2 -> 3 -> ...
阅读全文
摘要:Given an array of integers, find out whether there are two distinct indicesiandjin the array such that the difference betweennums[i]andnums[j]is at mo...
阅读全文
摘要:Given an array of integers and an integerk, find out whether there are two distinct indicesiandjin the array such thatnums[i] = nums[j]and the differe...
阅读全文
摘要:Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the arr...
阅读全文
摘要: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...
阅读全文
摘要:Given two stringssandt, write a function to determine iftis an anagram ofs.For example,s= "anagram",t= "nagaram", return true.s= "rat",t= "car", retur...
阅读全文
摘要:Given an absolute path for a file (Unix-style), simplify it.For example,path="/home/", =>"/home"path="/a/./b/../../c/", =>"/c"简化路径,linux下面简单的路径操作而已,代码...
阅读全文
摘要:Evaluate the value of an arithmetic expression inReverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer or another express...
阅读全文
摘要:教研室的项目,就是用Qt做个图形界面能收发数据就可以了,但是创建数据管理类的时候需要各种new, delete,很小心了但是内存使用量在不断开关程序之后函数会长,由于用的是gcc 4.7.* 所以好像没有shared_ptr可以用,上网查了一下原来QT也有智能指针。常用的有 QScopedPoint...
阅读全文
摘要:Say you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie...
阅读全文
摘要:Given a sorted array, remove the duplicates in place such that each element appear onlyonceand return the new length.Do not allocate extra space for a...
阅读全文
摘要:Given a sorted linked list, delete all duplicates such that each element appear onlyonce.For example,Given1->1->2, return1->2.Given1->1->2->3->3, retu...
阅读全文
摘要:Implement pow(x, n). 幂运算,简单的方法snag然很好实现,直接循环相乘就可以了,但是这里应该不是那种那么简单,我的做法使用到了一点递归: 这里有个注意点就是考虑到INT_MIN的情况,做出相应的处理 下面是java写的版本:
阅读全文
摘要:Sort a linked list using insertion sort.用插入排序来排序一个list,额, 我写的好麻烦啊, debug了好久,至少提交了5次。。。写吐了快,先贴代码,写的也好乱啊: 1 class Solution { 2 public: 3 ListNode* i...
阅读全文
摘要:Reverse a singly linked list. 做II之前应该先来做1的,这个倒是很简单,基本上不用考虑什么,简单的链表反转而已: java版本的如下所示,基本上没什么区别,就是普通解法: 其实翻转链表还可以使用一种递归的方式,代码如下:
阅读全文
摘要:1.信号函数调用的时候仅仅会发送出信号,所以不需要执行,所以对于信号声明就行,但是不需要进行定义。2.只有槽函数可以声明为public,private,或者是protected的,而信号不行。可以将slot仅仅看成是一个与信号相关联的成员函数。3.Qt的元对象指的是QMetaObject的实例,Qt...
阅读全文
摘要:对于P126的哈希表构成: 1 struct nlist *install(char *name, char *defn) 2 { 3 struct nlist *np; 4 unsigned hashval; 5 if ((np = lookup(name)...
阅读全文
摘要:4-12.写一个函数itoa,通过递归调用将整数转换成为字符串。 1 #include 2 #include 3 void Itoa(int num, char * s); 4 int main(void) 5 { 6 char s[20]; 7 Itoa(1234567, s)...
阅读全文
摘要:基本的排序算法有如下特点: 1.几种容易的算法都是以O(N2)排序的 2.Shell排序编程简单,其也是以O(N2)排序的,在实践中用的很多 3.复杂的排序算法往往都是按照O(NlogN)尽心排序的 4.任何通用的排序算法都需要NlogN次比较。 还有两个定理应该记一下,证明略: 1. N个互异数组
阅读全文
摘要:1 class DisjSets 2 { 3 public: 4 explicit DisjSets(int numElements); 5 int find(int x)const; 6 int find(int x); 7 void unionSets(int ...
阅读全文
摘要:这种情况一般都是,在使用的时候没有include而导致的,加上就可以正确编译通过
阅读全文
摘要:首先在源码目录下面新建一个 myapp.rc的文件,在里面填写如下:IDI_ICON1 ICON DISCARDABLE "myappico.ico"(名字看自己的图片,注意图片也要复制到源码目录下面)然后再pro文件中加上下面的语句就可以了:RC_FILE...
阅读全文
摘要:图片的大小是由listWidget的setIconSize设定的,而不是ListWidgetItem的SetIcon设定的
阅读全文
摘要:两条语句就能够实现了:1 this->newNC.setWindowFlags(Qt::WindowStaysOnTopHint);2 this->newNC.show();mark一下,防止以后忘了
阅读全文
摘要:Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL.No...
阅读全文
摘要:You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality c...
阅读全文
摘要:首先考虑的是一个很典型的关系,就是矩形与正方形的关系: 1 class Recantagle{ 2 virtual void setHeight(int); 3 virtual void setWidth(int); 4 virtual int height(int)cons...
阅读全文
摘要:inline可以带来各种好处:首先其可以使得消除函数调用带来的开销,再者编译器对这种非函数的代码可以做出更多的优化策略。但是inline函数首先肯定是会导致程序代码的大小更加的庞大,这样会带来代码膨胀,造成的损害首先就是会导致计算机额外的换页行为,降低指令告诉缓存的集中率,这要就会带来效率上的损失。...
阅读全文
摘要:首先看看下面这个例子: 1 class Point{ 2 public: 3 point(int x, int y); 4 ... 5 void setX(int newVal); 6 void setY(int newVal); 7 ... 8 }; 9 s...
阅读全文
摘要:Given two binary strings, return their sum (also a binary string).For example,a ="11"b ="1"Return"100".简单的二进制相加而已,只不过传入的参数是字符串而已。为了方便,先将string reverse...
阅读全文
摘要:Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at...
阅读全文
摘要:Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-nega...
阅读全文
摘要:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single ...
阅读全文
摘要:在leetCode上做的第一个难度是hard的题,题目如下:Given an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[3,4,-1,1]re...
阅读全文
摘要:Given an array containingndistinct numbers taken from0, 1, 2, ..., n, find the one that is missing from the array.For example,Givennums=[0, 1, 3]retur...
阅读全文
摘要:首先是C++提供的四种转型操作:1. const_cast:常量性的转除。2. dynamic_cast:安全的向derived class进行转型,可能会带来很高的开销3. reinterpret_cast:低级转型,例如可讲pointer转成int,不建议使用4. static_cast: 强迫...
阅读全文
摘要:Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Each number ...
阅读全文
摘要:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in or...
阅读全文
摘要:Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeat
阅读全文
摘要:首先说下标准库的swap算法:1 namespace std{2 template3 void swap(T & a, T & b)4 {5 T tmp = a;6 a = b;7 b = tmp;8 }9 }显然的,标...
阅读全文
摘要:首先还是下面这个class;class Rational{public: Rational(int numirator = 0, int denominator = 1); int numurator() const; int denominator() ...
阅读全文
摘要:Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?判断一个链表是否为回文链表,有很多方法都可以进行判断,可以先遍历链表然后...
阅读全文
摘要:Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a p
阅读全文
摘要:Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 给定一个整数,将他转换到罗马字符。同样这里的罗马字符不会超过1000(M),PS:关
阅读全文
摘要:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.首先是罗马字符串的特点:羅馬數字共有7個,即I(1)、V(5)、X(10)、L(50)、...
阅读全文
摘要:考虑下面这种经常出现的使用方式:class webBroswer{public: ... void clearCache(); void clearHistory(); void removeCookies(); ...};那么很自然的就会想到增加这么一种清理方式:cl...
阅读全文
摘要:Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to thedefinition of LCA on Wikipedia: “The lowest ...
阅读全文
摘要:Given a non-negative integernum, repeatedly add all its digits until the result has only one digit. For example: Givennum = 38, the process is lik...
阅读全文
摘要:Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer...
阅读全文
摘要:protected成员变量的封装性并非高于public变量。如果有个public的成员变量,一旦其需要改变,那么所有使用它的代码都需要改变。如果有个protected的成员变量,一点其需要改变,那么所有的继承自他的derived class都需要重新改变。这与上面孰轻孰重其实不重要所以说,往往只有两...
阅读全文
摘要://先看看下面这个例子class Rational{public: Rational(int num, int denu) :numirator(num), denumirator(denu); const Rational operator*(const Rational & l...
阅读全文
摘要:Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include2, 3, 5. For examp...
阅读全文
摘要:Count the number of prime numbers less than a non-negative number,n.计算小于n的质数的个数,当然就要用到大名鼎鼎的筛法了,代码如下,写的有点乱不好意思。 1 class Solution { 2 public: 3 int ...
阅读全文
摘要:注意一下,c++的底层编译器而言,引用类型往往都是由指针来实现出来的,所以pass-by-reference实际意义上往往是传递了一个指针,这对于c++的内置类型来说往往传递一个reference更加昂贵一点。但是这不能成为对所有的小对象不使用pass-by-reference的理由。小结:1. 尽...
阅读全文
摘要:下面的条框应该是谨记的:1. 新的type应该如何创建与销毁2. 对象的初始化与赋值应该有什么样的区别3. 新type的对象如果被pass-by-value,有什么影响?4. 什么事新type的合法值5. 新的type需要什么样的转换6. 什么样的操作符和函数对于这个type而言是合理的7. 什么样...
阅读全文
摘要:先考虑下面这个接口:1 class Data{2 ...3 public:4 Data(int month, int day, int year);5 };虽然声明是比较合理的,但是有时候人们不一定会这样做,例如做下面这样调用对于客户来说也是完全可能的:Data(2, 30, 1995);可...
阅读全文
摘要:先可考虑下下面这个语句:1 int prioriy();2 processWidget(shared_ptr(new Widget), priority());上面这条语句看似比较正常,不会泄露资源,但是实际上并非如此。调用上面这个语句的时候基本上分成三步:1. new Widget2. 根据Wid...
阅读全文
摘要:首先思考下面的代码:1 string * stringArray = new string[100];2 ...3 delete stringArray; 这是最常见的错误之一了,new与delete不配对,但是仔细想想new与delete为什么一定要配对呢? 可以想到一种可能就在分配数组的时候...
阅读全文
摘要:Given a positive integern, find the least number of perfect square numbers (for example,1, 4, 9, 16, ...) which sum ton.For example, givenn=12, return...
阅读全文
摘要:Given an arraySofnintegers, find three integers inSsuch that the sum is closest to a given number, target. Return the sum of the three integers. You m...
阅读全文
摘要:即使我们对一些资源使用了RAII类进行管理,但是还是不能避免很多的API需要你去提供原始的资源来对他们进行调用:例如对于条款13中的例子来说:1 shardPtr pInv(createInvestment);2 //看看下面这个函数3 int daysHold(const Investment *...
阅读全文
摘要:首先假设对于一个mutex互斥器对象,有lock以及unlock两个函数可用:1 void lock(Mutex * pm);2 void unlock(Mutex * pm);那么为了防止资源忘记被释放,首先得想法就是创建一个RAII类来进行资源的管理,像下面这样:1 class Lock{2 ...
阅读全文
摘要:首先考虑一个工厂函数Investment * createInvestment();void f(){ Investment * pInv = createInvestment(); ... delete pInv;}至少上面这个函数是不安全的,例如如果...里面包含ret...
阅读全文
摘要:Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of
阅读全文
摘要:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two nu...
阅读全文
摘要:首先要说的是当对一个已经定义了构造函数以及赋值运算符(拷贝)的class增加了成员的时候,一定要记得为这个class重新改写构造函数以及拷贝赋值运算符。 下一个容易遗忘的点在继承中,看下面的代码: 1 class Customer 2 { 3 public: 4 //....
阅读全文
摘要:本来的版本是这样的:1 Widget & Widget::operator=(Widget rhs)2 {3 delete pb;//这里可能直接将rhs的pb删除了4 pb = new (*rhs.pb);5 return *this;6 }这里的代码完全无法处理自赋值的情...
阅读全文
摘要:例如对象x,y,z。要实现连锁赋值(假设operator=已经重载过了):x = y = z,那么operator=则必须返回一个*this。 注意这个条款不仅仅适合于operator=,对于operator+=与赋值相关的操作也是同样适合的。所以的想要连环赋值的情况都应该这样去设计,尽管编译...
阅读全文
摘要:在构造以及析构函数期间不要调用virtual函数,因为这类调用从不下降到derived class中。例如说下面这个例子: 1 class Transaction{ 2 public: 3 Transaction(); 4 virtual void logTransactions()...
阅读全文
摘要:Remove all elements from a linked list of integers that have valueval.ExampleGiven:1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6,val= 6Return:1 --> 2 --> 3 --...
阅读全文
摘要:Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't mat...
阅读全文
摘要:Given an arraynums, write a function to move all0's to the end of it while maintaining the relative order of the non-zero elements.For example, givenn...
阅读全文
摘要:举个例子:class DBConnection{ public: ... static DBConnection create(); void close();};//这个class负责数据库连接。//为了防止用户忘了close这个数...
阅读全文
摘要:任何的类只要带有一个virtual函数那么就集合可以确定其应该有一个virtual析构函数。 同样的如果一个函数不含有virtual函数,那么通常意味着其不是一个基类函数,而且为一个不是基类的类声明virtual的析构函数是十分糟糕的事情,不要这样做。具体原因在下面:1.首先,想要实现出vir...
阅读全文
摘要:有些情况自己是不希望生成拷贝构造函数以及拷贝赋值运算符的,这种时候不能只是自己不去编写这些函数,因为这样编译器会自动的去生成这些函数。保险一点的做法是将拷贝构造函数以及拷贝赋值运算符都声明为private的。这样既阻止了编译器生成默认的版本,而且又阻止了别人去调用它。 注意上面的这条“将成员函数...
阅读全文
摘要:如果想在一个内含reference成员的class内支持赋值操作,必须自己定义copy assignment操作符。而且面对“内含有const成员的”class,编译器的反应也是相同的,由于更改const成员是非法的,所以编译器不知道如何在其自己生成的赋值函数之中去面对他们。templatecl...
阅读全文
摘要:构造函数使用本体括号进行复制的情况不叫做初始化而是叫做赋值语句,只有在成员初始化列表中直接进行的 才叫做初始化。ABentry::AbEntry(const std::string & name, const std::string & address, ...
阅读全文
摘要:const成员函数的一般好处有:它使得class接口比较容易理解。它使得操纵const对象成为可能。使用的过程中应该在const与non const成员函数之间避免代码重复:class TextBlock{ public: ... const char & operator[...
阅读全文
摘要:这里说的意思其实相当于,宁可以用编译器来替换预处理器因为使用预处理器可能使得被处理过的东西无法进入符号表,例如 #define MAXLEN 16 这里的MAXLEN并没有进入符号表,这样有编译错误出现的时候,提示的都是16而并不是MAXLEN,这样就会带来很多的错误。对于上面的那个式子,可以尝试的...
阅读全文
摘要:c++是一个多重泛型编程语言,其所支持的泛型有:面向过程编程(procedual)面向对象编程(object-oriented)面向函数编程(functional)泛型编程(generic)元编程模式(metaprogramming):元编程模式是什么模式,没读懂就是了
阅读全文
摘要:这个问题是vs准备弃用strcpy带来的,因为觉得他不太安全可以尝试在main函数前面加上#pragma warning(disable:4996)即可解决这个问题
阅读全文
摘要:说的有点夸装,实际上并不只是巴航代码,加上前面的变量声明之类的一共有40多行的样子吧,好像是在知乎上看到的,现在有时间再把它写下来: 其中用到了一些c++11特性,例如lambda 以及给予范围的 for循环。 其他的没什么好说的,看代码,上面也有注释的。 1 #include 2 #in...
阅读全文
摘要:话不多说,看代码:#include #include int main(void){ char s1[180] = "a-z0-9hahah-a-z-s-d"; char s2[180]; expand(s1, s2); printf("Now the s2 is : %s"...
阅读全文
摘要:题目如下所示:返回的结果是一个Node的Vector:Givenn, generate all structurally uniqueBST's(binary search trees) that store values 1...n.For example,Givenn= 3, your prog...
阅读全文
摘要:Given a string of numbers and operators,return all possible results from computing all the different possible waysto group numbers and operators. The ...
阅读全文

浙公网安备 33010602011771号