2016年3月3日

Merge Two Sorted Lists

摘要: 注意空串的处理 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ cl 阅读全文

posted @ 2016-03-03 19:13 RenewDo 阅读(169) 评论(0) 推荐(0) 编辑

2016年3月1日

4Sum

摘要: 注意当有四个时,其实第一个和第二个性质等于三个数和时的第一个。 class Solution { public: vector<vector<int>> fourSum(vector<int>& nums, int target) { vector<vector<int>> res; int i,j 阅读全文

posted @ 2016-03-01 22:26 RenewDo 阅读(157) 评论(0) 推荐(0) 编辑

Letter Combinations of a Phone Number

摘要: 遍历所有的可能。 1 class Solution { 2 private: 3 vector<string> res; 4 map<char,string> num; 5 public: 6 void initial() 7 { 8 9 num['2']="abc"; 10 num['3']="d 阅读全文

posted @ 2016-03-01 19:51 RenewDo 阅读(183) 评论(0) 推荐(0) 编辑

3Sum Closest

摘要: 大致思路还和3Sum一样:先排序,然后固定第一个数,后面的范围用两个指针确定,当大于目标值时,右指针左移,当小于目标值时,左指针右移。 需要注意的:第一每次改变三个数其中一个都要再次计算总和,判断。 1 class Solution { 2 public: 3 int threeSumClosest 阅读全文

posted @ 2016-03-01 16:33 RenewDo 阅读(115) 评论(0) 推荐(0) 编辑

2016年2月27日

3Sum

摘要: 思路: 先排序,从小到大。 三个数的加法,可以先确定一个数,剩下两个数在剩下的范围找,当总和比目标值大,就让最大的范围数减小,小就让最小的数增加。 注意几点就是: 第1个数,后面两个数可以有多种,所以要搜完所有数。 对于重复的数,可以跳过。 class Solution { public: vect 阅读全文

posted @ 2016-02-27 16:53 RenewDo 阅读(164) 评论(0) 推荐(0) 编辑

2016年2月25日

Longest Common Prefix

摘要: 主要是效率问题,对于空串要及早退出降低时间。 还有字符串问题: C++中string不是以‘\0’结束的,C中是的。 如果对一个空串赋值‘\0’,此时串的size()为1。 1 class Solution { 2 3 public: 4 string longestCommonPrefix(vec 阅读全文

posted @ 2016-02-25 16:55 RenewDo 阅读(170) 评论(0) 推荐(0) 编辑

Roman to Integer

摘要: 根据罗马数的规则可以很容易写出: 1 class Solution { 2 public: 3 int romanToInt(string s) { 4 int a[4]={0},i=0; 5 if(s[i]=='M') 6 while(s[i]=='M') {a[0]++;i++;} 7 if(s 阅读全文

posted @ 2016-02-25 15:59 RenewDo 阅读(143) 评论(0) 推荐(0) 编辑

Integer to Roman

摘要: 首先是我自己看完网上的罗马数规则写的又臭又长的代码!(捂脸!!) 1 class Solution { 2 public: 3 string intToRoman(int num) { 4 string s=""; 5 int a[4]={0},i=3; 6 while(num!=0) 7 { 8 阅读全文

posted @ 2016-02-25 13:32 RenewDo 阅读(131) 评论(0) 推荐(0) 编辑

2016年2月24日

Container With Most Water

摘要: 一开始暴力解决,时间没通过 1 class Solution { 2 public: 3 int maxArea(vector<int>& height) { 4 int maxArea=0,eachArea=0; 5 if(height.size()<2) return maxArea; 6 fo 阅读全文

posted @ 2016-02-24 22:03 RenewDo 阅读(170) 评论(0) 推荐(0) 编辑

Regular Expression Matching

摘要: 用动态规划 思想是:每次计算后都保存当前的运行结果 f[i][j]表示串s[0···i-1]与从串p[0···j-1]的比较结果 首先,f[0][0]表示两空串比较结果为真 f[0][0]=true; 其次,当原串不为空时,无论p串是什么结果都为真,即f[i][0]=false; 而当原串为空时,需 阅读全文

posted @ 2016-02-24 19:16 RenewDo 阅读(187) 评论(0) 推荐(0) 编辑

导航