摘要: Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis 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.Note: You 阅读全文
posted @ 2014-03-23 23:58 Awy 阅读(107) 评论(0) 推荐(0) 编辑
摘要: 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:You can only move either down or right at any point in time.思路:典型的动态规划问题。result[i][j]表示(0,0)到(i,j)的最小值。result[i][j]=min(result[i][j-1],result[i-1][j])+g 阅读全文
posted @ 2014-03-23 22:42 Awy 阅读(120) 评论(0) 推荐(0) 编辑
摘要: Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"思路:这道题给你n对括号,生成所有合法的括号规则。有如下规则:左括号和右括号的数量都为n时,将生成的括号push到re 阅读全文
posted @ 2014-03-23 09:41 Awy 阅读(111) 评论(0) 推荐(0) 编辑
摘要: 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 in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).How many possible uni 阅读全文
posted @ 2014-03-22 23:50 Awy 阅读(253) 评论(0) 推荐(0) 编辑
摘要: You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?思路:这道题关键在于数组的操作问题上,如何保存中间变量,还有图片的旋转方法。首先将数组分为(行数/2)层,然后一层一层的进行旋转。class Solution {public: void rotate(vector > &matrix) { if(matrix.size()==0||matrix[0].size()=... 阅读全文
posted @ 2014-03-22 10:21 Awy 阅读(105) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?思路:这道题有点难度,需要用到数学推理。如下图,需要设置慢指针slow,一次走一步和快指针fast,一次走两步,我们设环开始位置距离头结点的距离为K,当快指针追到慢指针时候的位置设为慢指针走的距离为x,快指针走的距离为y。链表的总长度为L。那么我们可以有如下推算过程:y=2x;y=L+x-k;这样可以退出 阅读全文
posted @ 2014-03-22 00:17 Awy 阅读(182) 评论(0) 推荐(0) 编辑
摘要: 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, white and blue.Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.Note:You are not suppose to use the l 阅读全文
posted @ 2014-03-20 10:54 Awy 阅读(129) 评论(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].思路:这题数组的全排列,我们使用分而治之的。将数组分成两部分:第一部分为首元素,第二部分为剩下的元素。总体来说,两步走:首先求所有可能出现第一个位置的元素,即把第一个元素和后面所有的元素交换,求后面所有元素的排列。这个时候我们仍把后面的所有元素分成两部分:后面元素的 阅读全文
posted @ 2014-03-20 08:22 Awy 阅读(210) 评论(0) 推荐(0) 编辑
摘要: static_cast 任何具有明确定义的类型转换,只要不包含底层const,都可以使用static_cast。例如,通过将一个运算对象强制转换成double类型就能表达式浮点数除法://进行强制类型转换以便执行浮点数处罚double slope = static_cast(j)/i; 当需要把一个较大的算术类型赋值给较小的类型时,static_cast非常有用。此时,强制类型转换告诉程序的读者和编译器:我们知道并且不在乎潜在的精度损失。一般来说,如果编译器发现一个较大的算术类型试图赋值给较小的类型,就会给出警告信息;但是当我们执行了显式的类型转换后,警告信息就会被关闭了。 static... 阅读全文
posted @ 2014-03-19 23:13 Awy 阅读(581) 评论(0) 推荐(0) 编辑
摘要: 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 example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its bottom-up level order traversal as:[ [15,7] [9,20], [3],]conf... 阅读全文
posted @ 2014-03-18 21:29 Awy 阅读(168) 评论(0) 推荐(0) 编辑
摘要: 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,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its level order traversal as:[ [3], [9,20], [15,7]]confused what"{1,#,2,3}"means?> read more . 阅读全文
posted @ 2014-03-18 21:04 Awy 阅读(179) 评论(0) 推荐(0) 编辑
摘要: 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, buy one and sell one share of the stock), design an algorithm to find the maximum profit.思路:这道题就是让自己能得到最大的利润,举个例子{6,1,3,2,4,7},元素1和元素3得到利润为2,先保存起来,然 阅读全文
posted @ 2014-03-17 22:38 Awy 阅读(170) 评论(0) 推荐(0) 编辑
摘要: The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integernrepresenting the total number... 阅读全文
posted @ 2014-03-17 22:05 Awy 阅读(198) 评论(0) 推荐(0) 编辑
摘要: 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]]思路:杨辉三角形,解决这道题就是要知道什么是杨辉三角形。杨辉三角以正整数构成,数字左右对称,每行由1开始逐渐变大,然后变小,回到1。第行的数字个数为个。第行的第个数字为组合数。第行数字和为。除每行最左侧与最右侧的数字以外,每个数字等于它的左上方与右上方两个数字之和(也就是说,第行第个数字等于第行的... 阅读全文
posted @ 2014-03-15 23:15 Awy 阅读(142) 评论(0) 推荐(0) 编辑
摘要: 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->3.Your algorithm should use only constant space. You maynotmodify the values in the list, only nodes itself can be changed.思路:这道题主要就是依次进行两个结点的判断 阅读全文
posted @ 2014-03-15 22:37 Awy 阅读(123) 评论(0) 推荐(0) 编辑
摘要: 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 tom+n) to hold additional elements from B. The number of elements initialized in A and B aremandnrespectively.思路:将两个给定排序好的数组A和B,将B合并到A中去,意思应该就是不能借用中间 阅读全文
posted @ 2014-03-14 23:29 Awy 阅读(132) 评论(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 ofeverynode never differ by more than 1.思路:首先我们先熟悉平衡二叉树的定义,平衡二叉树或为空树,或为如下性质的二叉排序树:(1)左右子树深度之差的绝对值不超过1;(2)左右子树仍然为平衡二叉树.平衡因子BF=左子树深 阅读全文
posted @ 2014-03-14 22:11 Awy 阅读(134) 评论(0) 推荐(0) 编辑
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST.解题思路:此题意思就是给你一组排序好的数组转化为平衡二叉树,既然都是排列好的数组了,我们取中间的元素作为根结点,去中间元素的左边区域的中间元素作为左子结点,去中间元素的右边区域的中间元素作为右子结点。以此类推,使用递归,解之!!!/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left... 阅读全文
posted @ 2014-03-14 08:22 Awy 阅读(157) 评论(0) 推荐(0) 编辑
摘要: 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 another array, you must do this in place with constant memory.For example,Given input array A =[1,1,2],Your function should return length =2, and A is 阅读全文
posted @ 2014-03-14 00:10 Awy 阅读(289) 评论(0) 推荐(0) 编辑
摘要: 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 / \ 2 2 / \ / \3 4 4 3But the following is not: 1 / \ 2 2 \ \ 3 3Note:Bonus points if you could solve it both recursively and iterati... 阅读全文
posted @ 2014-03-12 22:47 Awy 阅读(231) 评论(0) 推荐(1) 编辑
摘要: 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.分析:这道题我们新建新的链表,并且此题运用递归手法还是很好做的。首先我们从合并两个链表的头结点开始。链表1的头结点的值小于链表2的头结点的值,因此链表1的头结点将是合并后链表的头结点,反之,链表2的头结点将是合并后链表的头结点。我们继续合并两个链表中剩余的结点。在两个链表中剩下的结点依然是排序的,因此合并这两个链表的 阅读全文
posted @ 2014-03-12 21:03 Awy 阅读(151) 评论(0) 推荐(0) 编辑
摘要: Given an array of integers, every element appearsthreetimes except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?题目思路一:这道题要求时间复杂度是O(n),而且要求你不借用空间来实现。但是这道题我能想到的就是兼用map来实现。遍历数组,找出那个只出现一次的元素。class Solution {p 阅读全文
posted @ 2014-03-12 08:56 Awy 阅读(205) 评论(0) 推荐(0) 编辑
摘要: Neutron/ML2Neutron ML2模块层2(ml2)插件是一种允许OpenStack网络同时地利用在复杂现实数据中心发现的各种第二层网络技术的框架。目前它与存在的openvswitch、linuxbridge和hyperv L2代理共同存在,而且想要替换和否决与那些L2代理相关联的巨大插件。ml2框架也想要大大简化增加对新L2网络技术的支持,并且比那些要求添加新的巨大核心插件需要更少的初始和持续的努力。模块化代理可能作为后续开发工作。1.1. ML2驱动ml2驱动分别实现可扩展的网络类型和访问那些类型网络机制的集合。与metaplugin不同的是,多种机制被同时地用于访问相同虚拟网络 阅读全文
posted @ 2014-03-11 21:33 Awy 阅读(3115) 评论(0) 推荐(0) 编辑
摘要: Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.思路:暂时没有请参考这一个吧:http://blog.csdn.net/lanxu_yy/article/details/11703321?reloadclass Solution {public: string intToRoman(int num) { if(num=0;i--) { int skip=data[i]/5; ... 阅读全文
posted @ 2014-03-10 22:37 Awy 阅读(151) 评论(0) 推荐(0) 编辑
摘要: Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.首先,学习一下罗马数字罗马数字是最古老的数字表示方式,比阿拉伯数组早2000多年,起源于罗马罗马数字有如下符号:基本字符IVXLCDM对应阿拉伯数字1510501005001000计数规则:相同的数字连写,所表示的数等于这些数字相加得到的数,例如:III = 3小的数字在大的数字右边,所表示的数等于这些数字相加得到的数,例如:VIII = 8小的数字,限于(I、X和C)在大的数字左边, 阅读全文
posted @ 2014-03-10 21:43 Awy 阅读(216) 评论(0) 推荐(0) 编辑
摘要: 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 matter what you leave beyond the new length.分析:本题是要去除数组中指定的元素,并更新数组长度。这道题可以借用一个变量nLength(初始化为0)来更新数组的长度,循环查找指定元素,如果没有,则nLength加1,同时进行更新数组。代码片段如下。clas 阅读全文
posted @ 2014-03-09 20:54 Awy 阅读(102) 评论(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, try 阅读全文
posted @ 2014-03-09 20:33 Awy 阅读(94) 评论(0) 推荐(0) 编辑
摘要: 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 can you climb to the top?分析:这道题就相当于青蛙跳,就是一个费波那契数列。如果只有1级台阶,那显然只有一种跳法,如果有2级台阶,那么有两种跳的方法了:一种分两次条,每次跳一个;另外就是一次跳2级。我们把n级台阶的跳法看成是n的函数记为f(n)。当n>2时,f(n)=f(n-1)+f(n-2); 阅读全文
posted @ 2014-03-09 00:11 Awy 阅读(165) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3].Note:Recursive solution is trivial, could you do it iteratively?分析:这道题与之前那道中序遍历是一样的题型,借助栈stack数据结构。先在stack中push进root,由于是前序遍历,故根结点的访问先于左结点和右结点,因此pop出一个结点,... 阅读全文
posted @ 2014-03-07 21:25 Awy 阅读(262) 评论(0) 推荐(0) 编辑
摘要: 引用为对象起了另外一个名字(注,引用并非对象,相反的,它只是为一个已经存在的对象所起的另外一个名字),引用类型引用另外一个类型。通过将声明符写成&d的形式来定义引用类型,其中d是声明的变量名。且引用必须被初始化(一般在初始化变量时,初始化会被拷贝到新建的对象中。然后定义引用时,程序把引用和它的初始值绑定在一起,而不是将初始值拷贝给引用。一旦初始化完成,引用将和它的初始值对象一直绑定在一起。因为无法零引用重新绑定到另一个对象,因此引用必须初始化。) 定义一个引用后,对其进行的所有操作都是在与之绑定的对象上进行的:refVal=2;//把2赋给refVal指向的对象,此处即是赋给了iVal 阅读全文
posted @ 2014-03-07 09:34 Awy 阅读(331) 评论(0) 推荐(0) 编辑