leetcode
文章平均质量分 67
bladeLight
这个作者很懒,什么都没留下…
展开
-
Surounded Regions
这道题目是将被‘X’字符包围的‘O’改为‘X’。最初的想法是,扫描每行,如果发现‘O’字符,则进行dfs,dfs过程中如果发现边界上的‘O’则标记flag=ture,dfs返回后,如果flag为true,不进行操作,若flag为false则,重新递归一次,将每个点改成‘X’。这种想法,递归的层数太多了,返回的错误是Runtime Error。这是第一次做leetcode,很多地方写起来不太适应,我原创 2013-04-09 13:38:42 · 775 阅读 · 0 评论 -
Search in Rotated Sorted Array
class Solution {public: int search(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function int left=0,right=n-1;原创 2013-09-12 23:14:18 · 561 阅读 · 0 评论 -
reverse Integer
class Solution {public: int reverse(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function bool flag=true; if(x<0) flag=false;原创 2013-09-13 23:32:09 · 687 阅读 · 0 评论 -
Palindrome Number
思路:将元素从右向左用霍纳法则相加,得到结果。如果有溢出,r和x不会相等。class Solution {public: bool isPalindrome(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function i原创 2013-09-15 19:16:48 · 601 阅读 · 0 评论 -
Jump Game
dp[i]表示到达i这个位置还能最多想右走几步,则有dp[i]=max(dp[i-1]-1,A[i]);class Solution {public: bool canJump(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main()原创 2013-09-15 13:44:14 · 718 阅读 · 0 评论 -
N-Queens
做这道题除了很多小问题:1、v[i][col[i]]=‘Q’ 写成了str[col[i]]=‘Q’ ,殊不知这样不会修改向量中的值2、回溯的时候,col.pop_back写成了push_backclass Solution {public: vector col; vector > solveNQueens(int n) { // Start ty原创 2013-09-15 00:52:18 · 755 阅读 · 0 评论 -
Best Time to Buy and Sell Stock II
一开始考虑的动态规划的想法,dp[i]=max(dp[j]+a[i]-a[j]),结果超时。其实这是没有必要的,因为如果abc三个数,当ac的时候,只需要考虑ab;如果aclass Solution {public: int maxProfit(vector &prices) { // Start typing your C/C++ solution below原创 2013-09-30 21:47:29 · 1051 阅读 · 0 评论 -
Best Time to Buy and Sell Stock III
很经典啊class Solution {public: int maxProfit(vector &prices) { // Start typing your C/C++ solution below // DO NOT write int main() function int len=prices.size();原创 2013-10-01 00:35:29 · 929 阅读 · 0 评论 -
Jump Game II
一开始写了一个O(n^2)的方法,大数据果断超时代码如下:class Solution {public: int jump(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function int dp[原创 2013-09-15 15:54:39 · 936 阅读 · 0 评论 -
Remove Duplicates from Sorted List
这是个比较简单的题目,关键要细心/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {publ原创 2013-09-15 16:20:40 · 682 阅读 · 0 评论 -
Remove Duplicates from Sorted List II
用map记录每个整数出现的次数,重建链表/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {原创 2013-09-15 16:40:52 · 731 阅读 · 0 评论 -
Same Tree
class Solution {public: bool isSameTree(TreeNode *p, TreeNode *q) { // Start typing your C/C++ solution below // DO NOT write int main() function if(!p&&!q) return true;原创 2013-09-12 22:45:09 · 1937 阅读 · 0 评论 -
Balanced Binary Tree
class Solution {public: bool isBalanced(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function bool flag=true; height(root原创 2013-09-13 00:29:47 · 698 阅读 · 0 评论 -
Longest Substring Without Repeating Characters
class Solution {public: int lengthOfLongestSubstring(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function if(s=="") return 0;原创 2013-09-13 00:18:45 · 635 阅读 · 0 评论 -
Palindrome Partitioning II
设dp[i]表示从0到i位置最少需要切割的刀数。则这道题目的状态转移方程为:dp[i]=min(dp[j])+1,其中j>=0&&j需要注意的是:提交到leetcode的时候需要用向量,将p和dp替换成向量,否则会Runtime Error.#include#include#include#includeusing namespace std;int p[1600][1600原创 2013-04-09 22:40:51 · 954 阅读 · 0 评论 -
Sum Root to Leaf Numbers
和剑指offer里和为K的相似,这儿使用了霍纳法则来做的,很方便。/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL),原创 2013-04-10 17:51:14 · 1252 阅读 · 0 评论 -
Longest Consecutive Sequence
最初的想法是使用一个数组dp[1000]记录每个数字是否出现过,然后从小到大遍历一遍查找,看了测试数据后(数据太大),感觉这种方法不可行。实在想不出来了,参看了discuss,发现他们都是用容器实现的,首先将元素映射过去,然后判断左边和右边元素是否在map中,并且和以前的元素是否连续,这样进行之后,将访问过的元素标记为0.z这种方法是可以达到O(n)的。所以,以后想不出来的时候,试着去用一下容器。原创 2013-04-10 20:39:09 · 6546 阅读 · 5 评论 -
Merge Sorted Array
直接用插入排序做就行了,题目中说有序,但没有说明是递增还是递减,我是按照递增做的class Solution {public: void merge(int A[], int m, int B[], int n) { // Start typing your C/C++ solution below // DO NOT write int main()原创 2013-04-11 21:45:31 · 1393 阅读 · 0 评论 -
Palindrome Partitioning
回溯来做class Solution {public: int dp[200][200]; vector v; vector> vec; vector> partition(string s) { // Start typing your C/C++ solution below // DO NOT write int main原创 2013-04-11 23:36:02 · 874 阅读 · 0 评论 -
Valid Palindrome
这道题目挺简单的class Solution {public: bool isPalindrome(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function int i=0,j=s.length()-1;原创 2013-04-12 17:53:28 · 1974 阅读 · 0 评论 -
Two Sum
没有重复元素的情况下,用map做的class Solution {public: map mp; vector twoSum(vector &numbers, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function原创 2013-04-12 21:59:05 · 2044 阅读 · 1 评论 -
Word Ladder II
先写一下超时代码,练一下dfsclass Solution {public: vector path; unordered_set visited; vector> res; vector> findLadders(string start, string end, unordered_set &dict) { // Start typing原创 2013-04-16 22:47:50 · 600 阅读 · 0 评论 -
Word Ladder
一开始的做法是构造一个map记录每个单词是否访问过,然后从第一个单词进行广搜,如果相差一个字母并且没有访问过,则添加到队列中。这样的坏处是每次都要遍历一遍dict表,结果超时。后来参考别人的代码,对原始单词进行修改,然后查看是否在dict中,这样做没有超时class Solution {public: struct Node{ string word;原创 2013-04-12 17:31:26 · 4359 阅读 · 0 评论 -
二叉树-最近公共祖先(LCA)
思路:递归的思想,如果当前节点的左子树和右子树各包括一个节点,则该节点就为最近公共祖先;如果当前节点等于其中的一个节点,则当前节点为最近公共祖先;如果当前节点的左子树或者右子树包括两个节点,则需要递归求该节点的左子树或者有子树。struct Node{ int data; Node* left, *right;};//count the number of p and q//原创 2013-08-31 00:39:20 · 859 阅读 · 0 评论 -
划分
三路划分,参考了前令的博客。class Solution {public: void sortColors(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function int left=0,mid=0,原创 2013-09-15 20:11:49 · 755 阅读 · 0 评论