自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(134)
  • 收藏
  • 关注

原创 [leetcode刷题系列]4Sum

囧, 就在3sum上面的基础上改一下就好了class vector_int_hash{ const static int P = 10007; const static int MOD = 1e9 + 7; public: size_t operator() (const vector &v)const{

2013-08-16 18:54:14 841

原创 [leetcode刷题系列]3Sum Closest

- - 开始写模糊了- n^2复杂度class Solution {public: int threeSumClosest(vector &num, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function

2013-08-16 18:46:51 716

原创 [leetcode刷题系列]3Sum

循环枚举前两个, 然后二分查找第三个,嗯, 用hash_map判重class vector_int_hash{ const static int P = 10007; const static int MOD = 1e9 + 7; public: size_t operator() (const vector &v)const{

2013-08-16 18:31:19 658

原创 [leetcode刷题系列]Combination Sum II

- - 继续暴力把class Solution { void dfs(vector> &vp, int p, int target, vector & stk, vector > & ans){ if(target == 0){ ans.push_back(stk); ret

2013-08-16 00:46:43 654

原创 [leetcode刷题系列]Combination Sum

暴力就好了- -leetcode上面的题目就是暴力class Solution { void dfs(vector&v, int p, int target, vector & stk, vector > & ans){ if(target == 0){ ans.push_back(stk);

2013-08-16 00:35:41 673

原创 [leetcode刷题系列]ZigZag Conversion

嗯, 模拟题- - class Solution {public: string convert(string s, int nRows) { // Start typing your C/C++ solution below // DO NOT write int main() function if(nRows ==

2013-08-15 23:40:24 720

原创 [leetcode刷题系列]String to Integer (atoi)

没啥好说的额class Solution {public: int atoi(const char *str) { // Start typing your C/C++ solution below // DO NOT write int main() function while(*str != 0){

2013-08-15 23:29:07 640

原创 [leetcode刷题系列]Sort Colors

题目虽然简单, 但是确实找了好一会的bug,对着数据- -这题值得再写一遍。class Solution {public: void sortColors(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() functi

2013-08-15 00:28:15 695

原创 0813leetcode刷题总结

最近几天工作忙, 因为又要发新版本了。 所以今天只做这三道。而且这三道在leetcode上应该算是比较有价值的了。 不过都还算简单。 有空的话Minimum Window Substring这道题还值得再写一边:)明天继续吧

2013-08-14 00:59:32 971

原创 [leetcode刷题系列]Minimum Window Substring

这个也许算是two pointer类型的题目。 先贴一个简单的版本, 这个的复杂度应该算是256 * nclass Solution{ bool ok(int* a, int * b){ for(int c = 0; c < (1 << 8); ++ c) if(a[c] < b[c]) return fals

2013-08-14 00:38:09 741

原创 [leetcode刷题系列]Word Search

暴力dfs搜索就行了, 应该没什么好的算法const int MAXN = 100 + 10;const int dx[] = {-1, 0, 0, 1};const int dy[] = {0, -1, 1, 0};int n, m;int is[MAXN][MAXN];bool dfs(int r, int c, int p, vector > & board, st

2013-08-13 23:56:26 657

原创 [leetcode刷题系列]Scramble String

这题也没说数据规模, 一开始按照100来的。 跑大数据的时候TLE了。 于是怀疑是不是每次初始化花太多时间了所以就改成了50就过了- -,大概是n^4的算法, 有点暴力了。 不知道有没有其他更好的const int MAXN = 50 + 5;string s1, s2;int dp[MAXN][MAXN][MAXN];int get(int st1, int st2,

2013-08-13 23:43:25 696

原创 0812leetcode刷题总结

囧, 发第八篇的代码的时候, 又说超过了20篇, 只能保存为草稿, 明天再发了 今天刷了8道, 还剩下24道, 接下来每天刷8道, 到周四就刷完咯- -输完后应该会这对不同类型的遇到的问题总结下, 这样比较有价值一些。今天刷的8道里面比较有价值的大概是两道,一道是 Text Justification,之所以说这道比较有价值倒不是说这个的算法有多难。而是这是一道模拟题-

2013-08-13 00:10:38 862

原创 [leetcode刷题系列]Recover Binary Search Tree

这题目以前还真没做过, 这种题在acm中基本上没办法考察, 要求常数空间神马的第一次见到的时候, 没想到好的算法, 放弃了, 然后今天决定剩下不到30道leetcode题目要挨个刷,再次见到这个题目, 只有硬着头皮上了。想了一会其实就想到了。 可见遇到不会的还是要仔细想下- - 其实不难。对于这道题,我们可以中序遍历这棵树, 然后每次访问一个节点的时候就把它的值表示到一个全局变

2013-08-13 00:10:26 762

原创 [leetcode刷题系列]Decode Ways

dp一下就好了const int MAXN = 1e6 + 10;int dp[MAXN];class Solution { bool ok(char c){ return c >= '1' && c <= '9'; } bool ok(char a, char b){ if(a == '1') re

2013-08-12 22:27:19 643

原创 [leetcode刷题系列]Subsets II

递归暴力枚举就好了- -vector > data;class Solution { void dfs(int cur, vector & stk, vector > &ret){ if(cur >= data.size()){ ret.push_back(stk); re

2013-08-12 22:18:30 593

原创 [leetcode刷题系列]Text Justification

- - 模拟题, 不过写的时候要仔细, 而且要理解好题意, 其实不难。 不过我写的时候因为没理解好题意,一开始写错了- -,每行的单词之间至少要有一个空格的。这点特别注意, 其他就没啥了class Solution { string getString(vector &words, int st, int en, int L){ // special

2013-08-12 21:46:27 1005

原创 [leetcode刷题系列]Word Ladder II

这题时间卡的还真是严啊- - 先bfs一遍,标记没个单词属于哪一层, 然后从end开始往回走,就好了string start, end;unordered_map lev;void dfs(string now, vector & stk, vector > & ret){ if(now == start){ vector tmp(stk);

2013-08-12 21:16:35 814

原创 [leetcode刷题系列]Surrounded Regions

囧, 这题用dfs我re了,应该是爆栈了。 于是改成了bfs就过了const int MAXN = 1000 + 10;const int dx[] = {-1, 0, 0, 1};const int dy[] = {0, -1, 1, 0};int n, m;bool vis[MAXN][MAXN];void bfs(int r, int c, vector > &

2013-08-12 19:51:01 583

原创 [leetcode刷题系列]Palindrome Partitioning II

n^2的预处理求出那些子串是回文的,哪些不是, 然后再n^2的dp。const int MAXN = 2500;int n;bool is[MAXN][MAXN];int dp[MAXN];class Solution {public: int minCut(string s) { // Start typing your C/C++ solutio

2013-08-12 19:31:19 564

原创 [leetcode刷题系列]Palindrome Partitioning

暴力枚举const int MAXN = 1000 + 10;string s;int is[MAXN][MAXN];bool isPl(int left, int right){ if(left >= right) return true; int& ret = is[left][right]; if(ret != -1)

2013-08-12 19:12:33 557

原创 0811leetcode刷题总结

回顾了一下- -发现都是简单题, 没事啥好总结的。- - 脑袋晕晕的,今天就到这把明天继续:) 已经100道了, 还差32道题目

2013-08-12 15:38:04 641

原创 [leetcode刷题系列]Trapping Rain Water

- - 嗯, 水题const int MAXN = 1e6 + 10;int dleft[MAXN], dright[MAXN];class Solution {public: int trap(int A[], int n) { // Start typing your C/C++ solution below // DO NOT w

2013-08-12 15:37:58 526

原创 [leetcode刷题系列]Anagrams

表示不是很清楚这题要 干嘛- -class Solution {public: vector anagrams(vector &strs) { // Start typing your C/C++ solution below // DO NOT write int main() function map> hash;

2013-08-12 15:37:46 603

原创 [leetcode刷题系列]Search in Rotated Sorted Array II

- - 这题反正我是没想到确定性的logn的算法。 我的解法最坏情况下是O(n)class Solution {public: bool search(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int main(

2013-08-12 15:37:39 500

原创 [leetcode刷题系列]Simplify Path

- - 模拟题class Solution { vector splite(string s){ vector vs; int last = -1; for(int i = 0; i < s.size(); ++ i){ if(s[i] == '/'){ if(i - las

2013-08-12 15:37:32 631

原创 [leetcode刷题系列]Climbing Stairs

- - 这其实是斐波拉契数列class Solution { int fib[100];public: int climbStairs(int n) { // Start typing your C/C++ solution below // DO NOT write int main() function fib[0]

2013-08-12 15:36:53 508

原创 [leetcode刷题系列]Plus One

- - 模拟题class Solution {public: vector plusOne(vector &digits) { // Start typing your C/C++ solution below // DO NOT write int main() function if(digits.size() <= 0)

2013-08-12 15:36:43 517

原创 [leetcode刷题系列]Unique Paths II

现在他是一个dp题- -const int MAXN = 100 + 10;int n, m;int dp[MAXN][MAXN];int getAns(int r, int c, vector > & grid){ int & ret = dp[r][c]; if(ret != -1) return ret; if(grid[r][c

2013-08-12 15:36:33 500

原创 [leetcode刷题系列]Unique Paths

dp一下就行- - 其实这是一个组合数学题,答案是C(m - 1 + n - 1, m - 1)const int MAXN = 100 + 10;int dp[MAXN][MAXN];class Solution {public: Solution(){ memset(dp, 0xff, sizeof(dp)); for(int i =

2013-08-12 15:36:20 501

原创 [leetcode刷题系列]Spiral Matrix II

- - 基本上和上一道一样的道理const int dx[] = {0, 1, 0, -1};const int dy[] = {1, 0, -1, 0};int n;vector > ans;void dfs(int r, int c, int p, int number){ ans[r][c] = number; for(int i = 0; i < 4

2013-08-12 15:36:10 489

原创 [leetcode刷题系列]Spiral Matrix

- - 模拟题const int MAXN = 1000;const int dx[] = {0, 1, 0, -1};const int dy[] = {1, 0, -1, 0};int n, m;int vis[MAXN][MAXN];vector ans;vector > matrix;void dfs(int r, int c, int p){ vis

2013-08-12 15:35:53 485

原创 [leetcode刷题系列]Length of Last Word

水题- -不过通过这个题意识到了const的一种用法。以前不太了解这个。就是const指针和指向const对象的指针的写法和区别class Solution {public: int lengthOfLastWord(const char *s) { // Start typing your C/C++ solution below //

2013-08-12 15:35:43 551

原创 0810leetcode刷题总结

今天刷了17道题貌似, 基本上多数是二叉树或二叉搜索树相关的。以前做acm时i其实没有太加深过这方面的理解。也没怎么做过这类题目。 毕竟这些题目在acm中不好评判。 不太适合acm。所以今天还是得到了不少锻炼。加强了某些方面的理解和熟练程度。 尤其是写非递归做一些二叉树相关的操作。比较有价值的题目是Symmetric Tree,Binary Tree Inorder Tra

2013-08-12 15:35:22 603

原创 [leetcode刷题总结]Reverse Linked List II

貌似写啰嗦了- - 不过就这样吧,如果将来计划刷第二遍的话, 再精简下/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */

2013-08-11 03:16:41 581

原创 [leetcode刷题系列]Restore IP Addresses

暴力dfs就好了class Solution { bool isValid(string s){ if(s.size() == 1) return true; if(s[0] == '0' || s.size() > 3) return false; int ret = 0;

2013-08-11 02:44:54 469

原创 [leetcode刷题系列]Binary Tree Inorder Traversal

我不知道我写但是不是啰嗦了- -不太清楚比较成熟的写法是怎样的。不过我觉得我写的这个扩展性应该是比较好的。对于非二叉树,只需要多几个status就可以了。/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *righ

2013-08-11 02:29:26 575

原创 [leetcode刷题系列]Unique Binary Search Trees II

暴力就好了- -/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *

2013-08-11 02:13:40 555

原创 [leetcode刷题系列]Unique Binary Search Trees

也许算是dp题目把- -class Solution { int ans[100];public: int numTrees(int n) { // Start typing your C/C++ solution below // DO NOT write int main() function ans[0] = 1;

2013-08-11 02:04:19 445

原创 [leetcode刷题系列]Interleaving String

经典dp题,O(n^2)的状态,O(1)的转移。const int MAXN = 1000 + 10;string s1, s2, s3;int dp[MAXN][MAXN];int is(int i, int j){ int &ret = dp[i][j]; if(i == 0 && j == 0) return 1; if(ret

2013-08-11 01:58:31 516

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除