06 2015 档案
摘要:题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。参见Leetcode-Find Minimum in Rotated Sorted ...
阅读全文
摘要:题目:用两个栈实现一个队列。队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能。 1 template 2 class CQueue 3 { 4 public: 5 CQueue(); 6 ...
阅读全文
摘要:题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不包含重复的数字。根据前序和中序遍历结果重构二叉树,参见LeetCode-Construct Binary Tree from Preorder and Inorder Traversal。根据中序...
阅读全文
摘要:题目:输入一个链表的头结点,从尾到头反过来打印链表。链表结点定义如下:struct ListNode{ int val; ListNode* m_pNext; };思路:利用栈 1 void printListReverse(ListNode* pHead) 2 { 3 ...
阅读全文
摘要:题目:请实现一个函数,把字符串中的每个空格替换成”%20“。例如输入”We are happy.",则输出“We%20are%20happy."。 1 void replaceBlank(char string[], int length) 2 {//length为string的总容量 3 ...
阅读全文
摘要:解法一:只适用与单线程环境上述代码通过将构造函数定义为私有函数,并且只有在instance为空时才创建实例来确保只能创建一个实例。解法二:虽然在多线程环境中能工作但效率不高解法一存在的问题:在多线程环境下,如果两个线程同时进行if判断,并且instance的值为空,此时两个线程都会创建实例。解决方案...
阅读全文
摘要:题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。参见LeetCode-Search a 2D Matrix
阅读全文
摘要:题目:如下为类型CMyString的声明,请为该类型添加赋值运算符函数。class CMyString{ public: CMyString(char* pData = NULL); CMyString(const CMyString&str); ~CMyString(void)...
阅读全文
摘要:题目:请完成一个函数,输入一颗二叉树,输出它的镜像。解法
阅读全文
摘要:解法1: 1 int NumberOf1(int n) 2 3 { 4 int count = 0; 5 unsigned int flag = 1; 6 while (flag) 7 { 8 if (n&flag) 9 count++;...
阅读全文
摘要:Description:Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twic...
阅读全文
摘要:Description:Given an array of integers and an integerk, find out whether there there are two distinct indicesiandjin the array such thatnums[i] = nums...
阅读全文
摘要:Description:Find the total area covered by tworectilinearrectangles in a2Dplane.Each rectangle is defined by its bottom left corner and top right corn...
阅读全文
摘要:分析:对于x和y1.首先计算各位相加但不计进位;2.记下进位;3.把前步的结果相加。 1 int add(int num1, int num2) 2 { 3 int sum, carry; 4 do 5 { 6 sum = num1^num2; 7 carry = (nu...
阅读全文
摘要:解法一:把构造函数设为私有 将构造函数定义为私有,然后通过定义公有的静态函数来创建和释放类的实例。 1 class SealedClass 1 2 { 3 public: 4 static SealedClass1* GetInstance() 5 { 6 ret...
阅读全文
摘要:Description:Reverse a singly linked list.Code: 1 ListNode* reverseList(ListNode* head) { 2 ListNode* preNode = NULL; 3 ListNode* p = head; 4 ...
阅读全文
摘要:Description:Remove all elements from a linked list of integers that have valueval.ExampleGiven:1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6,val= 6Return:1 --...
阅读全文
摘要:Description:Given a range [m, n] where 0 =0; i++)15 x = x&i;16 return x;17 }18 }View Code
阅读全文
摘要:Description:Given a 2d grid map of'1's (land) and'0's (water), count the number of islands. An island is surrounded by water and is formed by connecti...
阅读全文
摘要:Description:Given a binary tree, imagine yourself standing on therightside of it, return the values of the nodes you can see ordered from top to botto...
阅读全文
摘要:Description:You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint...
阅读全文
摘要:变换一个整数的符号,即正数变负数,负数变正数。1 int reverseSign(int n) {2 return ~n+1;3 }
阅读全文
摘要:给出一个16位的无符号整数。称这个二进制数的前8位为“高位”,后8位为“低位”。现在写一程序将它的高低位交换。例如,数34520用二进制表示为:1000011011011000将它的高低位进行交换,我们得到了一个新的二进制数:1101100010000110它即是十进制的55430。这个问题用位操作...
阅读全文
摘要:Description:Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as000000101001010000011110100111...
阅读全文
摘要:一个整数为2的幂说明该整数的二进制中只有一个1. bool isSquareOf2 (int n){ return (n&(n-1))==0?true:false;}
阅读全文
摘要:Description:Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the...
阅读全文
摘要:Description:Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any posi...
阅读全文
摘要:Description:Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as theHamming weight).For example, t...
阅读全文
摘要:Description: 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,
阅读全文
摘要:Description: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 larges...
阅读全文
摘要:Description:Given an integern, return the number of trailing zeroes (尾数0) inn!.Note:Your solution should be in logarithmic time complexity.分析:一个数 n 的阶...
阅读全文
摘要:Description:Related to questionExcel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For ex...
阅读全文
摘要:Description: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 ...
阅读全文
摘要:Description:Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example: 1 -> A 2 -> B 3 -> C ...
阅读全文
摘要:Description:A peak element is an element that is greater than its neighbors.Given an input array wherenum[i] ≠ num[i+1], find a peak element and retur...
阅读全文
摘要:Description:Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A...
阅读全文
摘要:Description:Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop(...
阅读全文
摘要:Description:Follow upfor "Find Minimum in Rotated Sorted Array":What ifduplicatesare allowed?Would this affect the run-time complexity? How and why?Su...
阅读全文
摘要:Description:Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).Find the minimum e...
阅读全文
摘要:Description:Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".Code:1 void revers...
阅读全文
摘要:Description:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?Code: 1 bool hasCycle(ListNo...
阅读全文
摘要:Description:Given a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated sequence of one or more dictionary word...
阅读全文
摘要:Dscription:Given an array of integers, every element appearsthreetimes except for one. Find that single one.Note:Your algorithm should have a linear r...
阅读全文
摘要:Description:Given an array of integers, every element appearstwiceexcept for one. Find that single one.Note:Your algorithm should have a linear runtim...
阅读全文
摘要:Description: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 surro...
阅读全文
摘要:Description:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a...
阅读全文
摘要:Description: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, g...
阅读全文
摘要:Description:Given an indexk, return thekthrow of the Pascal's triangle.For example, givenk= 3,Return[1,3,3,1].Note:Could you optimize your algorithm t...
阅读全文
摘要:Description:GivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1],...
阅读全文
摘要:Description:Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous s...
阅读全文
摘要:Description:Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each...
阅读全文
摘要:Description: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 ...
阅读全文
摘要:Description: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 th...
阅读全文
摘要:Description: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 whi...
阅读全文
摘要:Description:Given a binary tree, return thebottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to r...
阅读全文
摘要:Description:Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.Co...
阅读全文
摘要:Description:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.Cod...
阅读全文
摘要:Description: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...
阅读全文
摘要:Description:Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next ...
阅读全文
摘要:Description:Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level).For example:Given binar...
阅读全文
摘要:Description:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: ...
阅读全文
摘要:Description:Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1Code: 1 TreeNode* invertTr...
阅读全文
摘要:Description:Given 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 fo...
阅读全文
摘要:Description: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 ...
阅读全文
摘要:Description:Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 uniqu...
阅读全文
摘要:Description:Given two sorted integer arraysnums1andnums2, mergenums2intonums1as one sorted array.Note:You may assume thatnums1has enough space (size t...
阅读全文
摘要:Description:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinctnumbers from the original list.For example,G...
阅读全文
摘要:Description:Given a sorted linked list, delete all duplicates such that each element appear onlyonce.For example,Given1->1->2, return1->2.Given1->1->2...
阅读全文
摘要:Description:Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are s...
阅读全文
摘要:Description:You are climbing a stair case. It takesnsteps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways c...
阅读全文
摘要:一.冒泡排序(稳定) 算法原理 1.首先比较第一个和第二个数据,将其中较小的数据放到第一个位置,较大的放到第二个位置。然后比较第二个和第三个位置,仍将较大的放到后一个位置。以此类推,直到比较第n-1个数据和第n个数据。这样就将待排序序列中的最大的一个放到第n个位置上,这个过程称为一趟排序。 2.对前
阅读全文
摘要:内部排序:全部待排序记录都可以同时调入内存进行的排序。 外部排序:待排序记录的数量太大,以致无法将其同时调入内存,尚需访问外存的排序过程。 排序中的基本操作: 1. 比较关键字大小 (对大多数排序算法是必须的) 2. 移动记录 (可通过改变记录的存储方式避免) 待排序记录的存储方式: 1. 存储在地
阅读全文
摘要:Description:Given two binary strings, return their sum (also a binary string).For example,a ="11"b ="1"Return"100".Code: 1 string addBinary(string...
阅读全文
摘要:Description:在Vmware Workstation 11上安装了Ubuntu 10.0,画面显示如下所示:Ubuntu系统的屏幕太小。调整方法:调节显示器分辨率即可,下图是将分辨率调节为1920*1080的结果。
阅读全文
摘要:Description: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...
阅读全文
摘要:Description:Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its ...
阅读全文
摘要:Description: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 e...
阅读全文
摘要:Description: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 in...
阅读全文
摘要:Description:Given a sorted array, remove the duplicates in place such that each element appear onlyonceand return the new length.Do not allocate extra...
阅读全文
摘要:Description:Given a linked list, swap every two adjacent nodes and return its head.For example,Given1->2->3->4, you should return the list as2->1->4->...
阅读全文
摘要:Description: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 lis...
阅读全文
摘要:Description:Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321Code: 1 int reverse(int x) { 2 int p=0; 3...
阅读全文
摘要:Description:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices o...
阅读全文
摘要:Description:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes conta...
阅读全文
摘要:Description: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 a...
阅读全文
摘要:Description:Given a stringsconsists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string.If the l...
阅读全文
摘要:Description:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,...
阅读全文
摘要:Description:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?Code: 1...
阅读全文
摘要:Description: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]...
阅读全文
摘要:Description: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...
阅读全文
摘要:Description:Given a string containing just the characters'(',')','{','}','['and']', determine if the input string is valid.The brackets must close in ...
阅读全文
摘要:问题描述Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).For example: Given bi...
阅读全文