12 2015 档案
摘要:class Solution {public: vector<vector<int>> generate(int numRows) { vector<vector<int>>result; if(numRows<=0) return result; int n = numRows; for(int
阅读全文
摘要:class Solution {public: vector plusOne(vector& digits) { int val= 1; int length = digits.size();//注意数字的高位为digits[0],低位在digits[length-1] for(int i = le...
阅读全文
摘要:class Solution { /*本质上在一列数组中取出一个或多个不相邻数,使其和最大。那么我们对于这类求极值的问题首先考虑动态规划Dynamic Programming来解,我们维护一个一位数组dp,其中dp[i]表示到i位置时不相邻数能形成的最大和,经过分析,我们可以得到递推公式dp[i] ...
阅读全文
摘要:class Solution {public: int removeDuplicates(vector& nums) { int length = nums.size(); int size= 0; if(length<=1) return length; for(int i= 1;i<length...
阅读全文
摘要:/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(N...
阅读全文
摘要: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...
阅读全文
摘要:/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(N...
阅读全文
摘要: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 th...
阅读全文
摘要:class Solution {public: bool isPowerOfTwo(int n) { //一定要处理n为0的情况 if(n==0) return false; while(n%2==0)n=n/2; if (n==1) return true; else return false; ...
阅读全文
摘要:/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/class Solution {pu...
阅读全文
摘要:class Queue {public: stacksta1; stacksta2; // Push element x to the back of queue. /* 入栈:把元素push到sta1中; 出栈:sta2作为辅助栈,如果sta2不为空,则把sta2中的元素挨个出站,然后把sta1的...
阅读全文
摘要:Ugly numbers are positive numbers whose prime factors only include2, 3, 5. For example,6, 8are ugly while14is not ugly since it includes another prime...
阅读全文
摘要: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...
阅读全文
摘要: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...
阅读全文
摘要:/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/class Solution {pu...
阅读全文
摘要:class Solution {public: int romanToInt(string s) { int length = s.size(); int result = 0; int pre =getnum (s[0]); for (int i = 1 ;i<length ;i++) {int ...
阅读全文
摘要:For example, the 32-bit integer ’11' has binary representation00000000000000000000000000001011, so the function should return 3.class Solution {public...
阅读全文
摘要:_______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 ...
阅读全文
摘要:Given an array of sizen, find the majority element. The majority element is the element that appears more than⌊ n/2 ⌋times.class Solution {public: int...
阅读全文
摘要:A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 class Solution {public: int titleToNumber(string s) { int sum = 0,temp=0; for...
阅读全文
摘要:class Solution {public: bool containsDuplicate(vector& nums) { int length = nums.size(); sort(nums.begin(),nums.end());//比较相等的数据可以先对数组进行排序 for (int i...
阅读全文
摘要: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...
阅读全文
摘要:nvert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1/*** Definition for a binary tree node.*...
阅读全文
摘要:/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(N...
阅读全文
摘要: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...
阅读全文
摘要: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 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 non-negative integernum, repeatedly add all its digits until the result has only one digit.For example:Givennum = 38, the process is like:3 + ...
阅读全文
摘要:You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 ston...
阅读全文