摘要:
https://leetcode.com/problems/unique-paths/递归问题转换成动态规划问题。每个问题可以分解成p[m][n]=p[m-1][n]+p[m][n-1]动态规划:做一个动态二维数组,用于存放每步的解。最终的循环就是矩阵的所有点遍历一遍。时间复杂度为m*n的矩阵遍历。...
阅读全文
posted @ 2015-07-13 10:48
阿怪123
阅读(128)
推荐(0)
摘要:
https://leetcode.com/problems/merge-two-sorted-lists/ 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ...
阅读全文
posted @ 2015-07-13 10:31
阿怪123
阅读(127)
推荐(0)
摘要:
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/由于是二叉排序树,所以可以先确定一个搜索函数,方便构造出一个数组用于存放搜寻路径之后再搜索路径中查找到最后重复的节点即可。 1 /** 2 * ...
阅读全文
posted @ 2015-07-12 16:01
阿怪123
阅读(113)
推荐(0)
摘要:
https://leetcode.com/problems/climbing-stairs/ 1 class Solution { 2 public: 3 int climbStairs(int n) { 4 if(n==1) 5 return 1; ...
阅读全文
posted @ 2015-07-12 15:57
阿怪123
阅读(129)
推荐(0)
摘要:
https://leetcode.com/problems/remove-duplicates-from-sorted-list/ 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int va...
阅读全文
posted @ 2015-07-11 08:38
阿怪123
阅读(145)
推荐(0)
摘要:
https://leetcode.com/problems/search-insert-position/1 class Solution {2 public:3 int searchInsert(vector& nums, int target) {4 int index=...
阅读全文
posted @ 2015-07-11 08:31
阿怪123
阅读(90)
推荐(0)
摘要:
https://leetcode.com/problems/unique-binary-search-trees/ 1 class Solution { 2 public: 3 int numTrees(int n) { 4 //典型的动态规划问题 5 //先...
阅读全文
posted @ 2015-07-10 20:56
阿怪123
阅读(126)
推荐(0)
摘要:
https://leetcode.com/problems/binary-tree-inorder-traversal/递归中序遍历 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int v...
阅读全文
posted @ 2015-07-10 16:41
阿怪123
阅读(128)
推荐(0)
摘要:
https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ 1 /** 2 * Definition for binary tree with next pointer. 3 * struct TreeLi...
阅读全文
posted @ 2015-07-09 11:23
阿怪123
阅读(131)
推荐(0)
摘要:
https://leetcode.com/problems/minimum-depth-of-binary-tree/ 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 ...
阅读全文
posted @ 2015-07-09 11:09
阿怪123
阅读(147)
推荐(0)
摘要:
https://leetcode.com/problems/majority-element/ 1 class Solution { 2 public: 3 int majorityElement(vector& nums) { 4 map mymap; 5 ...
阅读全文
posted @ 2015-07-08 11:05
阿怪123
阅读(160)
推荐(0)
摘要:
https://leetcode.com/problems/excel-sheet-column-number/ 1 class Solution { 2 public: 3 int titleToNumber(string s) { 4 int size=s.size();...
阅读全文
posted @ 2015-07-08 11:04
阿怪123
阅读(142)
推荐(0)
摘要:
https://leetcode.com/problems/reverse-bits/ 1 class Solution { 2 public: 3 uint32_t reverseBits(uint32_t n) { 4 uint32_t res=0; 5 ...
阅读全文
posted @ 2015-07-08 11:03
阿怪123
阅读(106)
推荐(0)
摘要:
https://leetcode.com/problems/happy-number/快乐数 1 class Solution { 2 public: 3 bool isHappy(int n) { 4 int res; 5 int temp; 6 ...
阅读全文
posted @ 2015-07-08 10:58
阿怪123
阅读(188)
推荐(0)
摘要:
动态规划问题,定义个数组,将现阶段的所有走过的步数的最优解保存到数组中对应下标处。在进行下一步的时候,通过调用已知数组和当前节点的情况进行讨论,做出决策。 1 class Solution { 2 public: 3 int rob(vector& nums) { 4 int...
阅读全文
posted @ 2015-07-08 10:57
阿怪123
阅读(130)
推荐(0)
摘要:
删除链表元素 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(...
阅读全文
posted @ 2015-07-08 10:54
阿怪123
阅读(124)
推荐(0)
摘要:
查找是否存在重复的数,直接定义map进行记录。或者用哈希也可以。 1 class Solution { 2 public: 3 bool containsDuplicate(vector& nums) { 4 map mymap; 5 int size=num...
阅读全文
posted @ 2015-07-08 10:50
阿怪123
阅读(117)
推荐(0)
摘要:
同样,使用两个队列进行层间循环和层内循环。最后使用一个栈进行转置。PS:特别注意的是,对于vector的使用,在赋值的阶段会发生内存报错的情况:vector vec1;vec1[0]=1直接使用下标赋值是错误的,应使用的是push_back()函数进行数值添加。而,在调用已经赋值的变量时,可以使用下...
阅读全文
posted @ 2015-07-08 10:46
阿怪123
阅读(118)
推荐(0)
摘要:
基本思想:定义一个队列q,用于作大循环,表示的每层的循环,定义一个队列q2,用作层内循环,表示每层的循环关系,只存储每层内的节点。之后就可以在二维数组中将层次关系表现出来了。 1 /** 2 * Definition for a binary tree node. 3 * struct Tree...
阅读全文
posted @ 2015-07-08 10:00
阿怪123
阅读(121)
推荐(0)
摘要:
主要思想:将罗马数字的范围分块,其中,最左部分可分为13个点。对于目标数字,看它在哪个范围内,则它可以拆分为左边节点对应的字符串+(数字-左边界值)的对应罗马字符的组合。用一个递归算法来实现即可。 1 class Solution { 2 public: 3 string intToRoma...
阅读全文
posted @ 2015-07-08 09:51
阿怪123
阅读(94)
推荐(0)
摘要:
链表的插入排序算法,其中大循环是,从原始链表中挨个读取每个元素。取出的每个元素用插入排序建立新表即可 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * Lis...
阅读全文
posted @ 2015-07-07 23:40
阿怪123
阅读(124)
推荐(0)
摘要:
回文,先提取有效字符,之后进行前后项匹配 1 class Solution { 2 public: 3 bool isPalindrome(string s) { 4 vector change; 5 int size=s.length(); 6 ...
阅读全文
posted @ 2015-07-07 22:18
阿怪123
阅读(131)
推荐(0)
摘要:
首先讨论最端头的两种情况。之后再进行中间部分的比较 1 class Solution { 2 public: 3 int findPeakElement(vector& nums) { 4 int size=nums.size(); 5 if(size==1)...
阅读全文
posted @ 2015-07-07 22:00
阿怪123
阅读(125)
推荐(0)
摘要:
主要思想:申请两个辅助栈,一个用于进in,一个用于出out,倒腾一之后,在出之前变为FIFO即是队列 1 class Queue { 2 stack in,out; 3 public: 4 // Push element x to the back of queue. 5 v...
阅读全文
posted @ 2015-07-07 21:41
阿怪123
阅读(121)
推荐(0)
摘要:
设置两个游标start和end,用于控制搜寻的段长,保持段长最长为k,之后在每个段内进行map匹配,看是否出现了重复元素 1 class Solution { 2 public: 3 bool containsNearbyDuplicate(vector& nums, int k) { 4 ...
阅读全文
posted @ 2015-07-07 19:18
阿怪123
阅读(139)
推荐(0)
摘要:
递归算法 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * ...
阅读全文
posted @ 2015-07-07 12:08
阿怪123
阅读(115)
推荐(0)
摘要:
基本思想就是:二叉树的中序非递归遍历 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *...
阅读全文
posted @ 2015-07-07 00:28
阿怪123
阅读(149)
推荐(0)
摘要:
1 class Solution { 2 public: 3 bool isPowerOfTwo(int n) { 4 int temp; 5 if(n==0) 6 return false; 7 if(n==1) 8...
阅读全文
posted @ 2015-07-06 23:17
阿怪123
阅读(120)
推荐(0)
摘要:
1 class Solution { 2 public: 3 int romanToInt(string s) { 4 char temp; 5 int leng=s.length(); 6 if(leng==0) 7 ...
阅读全文
posted @ 2015-07-04 11:39
阿怪123
阅读(137)
推荐(0)
摘要:
基本思想:首先统计两个链表的长度,并且在循环的过程中,看是否最终是会聚到同一个位节点,若是,则表示两个表有交汇,若否,这返回NULL之后,对长表进行修剪,剪去多余的表头部分,之后两表并列循环向后查找,找到相同项即是交汇节点。 1 /** 2 * Definition for singly-link...
阅读全文
posted @ 2015-07-04 10:43
阿怪123
阅读(133)
推荐(0)
摘要:
1 //递归方法 2 /** 3 * Definition for a binary tree node. 4 * struct TreeNode { 5 * int val; 6 * TreeNode *left; 7 * TreeNode *right; 8 ...
阅读全文
posted @ 2015-07-03 23:58
阿怪123
阅读(123)
推荐(0)
摘要:
1 class Solution { 2 public: 3 int trailingZeroes(int n) { 4 int ret = 0; 5 while(n) 6 { 7 ret += n/5; 8 ...
阅读全文
posted @ 2015-06-09 10:24
阿怪123
阅读(104)
推荐(0)
摘要:
1 class Solution { 2 public: 3 vector getRow(int rowIndex) { 4 vector> tri; 5 if(rowIndex==0) 6 { 7 vector c;...
阅读全文
posted @ 2015-06-08 21:30
阿怪123
阅读(165)
推荐(0)