![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
文章平均质量分 66
普通网友
这个作者很懒,什么都没留下…
展开
-
Unique Paths
原来我对斐波那契数列的动态规划不是那么熟悉啊二位动态规划使用,不用递归,用循环class Solution {public: int uniquePaths(int m, int n) { // Start typing your C/C++ solution below // DO NOT write int main() functi原创 2013-08-07 20:53:24 · 183 阅读 · 0 评论 -
candy
Candy AC Rate: 222/1879My SubmissionsThere are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the follow原创 2013-10-02 10:30:48 · 449 阅读 · 0 评论 -
leetcode Integer to Roman
原来以为数字到罗马数字规律会挺复杂,没想到还是挺简单的。找到规律了就那么容易了啊!!class Solution {public: string intToRoman(int num) { // Start typing your C/C++ solution below // DO NOT write int main() functio转载 2013-09-03 21:21:11 · 194 阅读 · 0 评论 -
Edit Distance
知道是动态规划,但是脑袋短路竟然一直没有想出代码>想出了就很简单了。class Solution {public: int minDistance(string word1, string word2) { // Start typing your C/C++ solution below // DO NOT write int main(原创 2013-09-03 19:12:07 · 181 阅读 · 0 评论 -
leetcode- Text Justification
主要也就注意各种情况吧。首先找到每行能放多少单词,再合理安排空格。如果多余空格应该前面单词空格多一点。如果是最后一行必须特殊对待。目测扫描了2次。考虑庆祝耐心点就可以解决吧。class Solution {public: void justify(vector &words,int idx, vector &r, int L) {原创 2013-09-03 14:09:57 · 513 阅读 · 0 评论 -
leetcode-Insert Intervals
一遍扫描数组,遇到新的有交叉的更新newInterval没有交叉后就直接插入。/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s,原创 2013-09-03 13:35:28 · 196 阅读 · 0 评论 -
leetcode-wild matching
一开始用深度优先,超时然后觉得是深度遍历过的没有记录结果,开了个vector> 内存超出界限倒数第三个题目,懒得纠结了,就网上找解决方案虽然不喜欢goto,但是不管了。class Solution {public: bool isMatch(const char *s, const char *p) { // Start typing转载 2013-09-03 21:09:50 · 166 阅读 · 0 评论 -
leetcode-Valid Number
一开始就知道这个题目很无聊,但是找工作压力好大,希望尽快把leetcode所有题目都做完几道题目不做放在那里感觉就不爽,所以还是努力做这种很无聊的仔细注意的题目。果然就是注意各种临界情况。class Solution {public: bool isNumber(const char *s) { // Start typing your C/C++ s原创 2013-09-03 19:38:47 · 192 阅读 · 0 评论 -
leetcode gray code
class Solution {public: bool getCode(int n,vector &used,vectot r) { if(r.size()==used.size()) return true; int r_bit=0; int top=r.back(); in原创 2013-09-03 13:00:24 · 154 阅读 · 0 评论 -
leetcode Decode Ways
原来用深度优先搜索,结果超时了。看看觉得和斐波那契数列如此相似,动态规划。果然。class Solution {public: int numDecodings(string s) { // Start typing your C/C++ solution below // DO NOT write int main() fun原创 2013-09-02 21:06:20 · 149 阅读 · 0 评论 -
Longest Palindromic Substring
我觉得能想到这个算法的人真是牛!!详情参照这里http://blog.csdn.net/hopeztm/article/details/7932245class Solution {public: string longestPalindrome(string s) { // Start typing your C/C++ solution转载 2013-08-17 15:11:07 · 164 阅读 · 0 评论 -
Sudoku Solver
原来我还真的按照我一般做数独的思路去,先找出能确定填什么数字的,直到不能找到能填的数字后再一个个尝试。果然计算机完成就是不一样,可以backtracking多少都可以记住。按照原来想法计算机找到能确定的就很麻烦。class Solution {public: bool validGrid(vector > &board,int i,int j) {转载 2013-08-17 17:44:03 · 218 阅读 · 0 评论 -
Substring with Concatenation of All Words
class Solution {public: vector findSubstring(string S, vector &L) { // Start typing your C/C++ solution below // DO NOT write int main() function map mapL; for(int i=0;i<L.size();i++) {原创 2013-08-17 16:29:20 · 157 阅读 · 0 评论 -
Median of Two Sorted Arrays
虽然知道大概思路,但是被我自己的多种情况分析搞得头大,想出来的代码也是很复杂的,好多if,else,所以就懒得按照我自己想法做。看了别人思路,终于发现有段时间我实在太依赖vector,数组的优点A+m就可以直接指向后续的元素。哎,想的太复杂了!!实际代码可以很简单的。比如目前的realbrush,想的好麻烦结果代码是一团糟。结果完全就是乱起八糟,没有理想效果。class S转载 2013-08-17 10:11:58 · 198 阅读 · 0 评论 -
String to Integer (atoi)
字符串转换到数字,我觉得这种题目就是坑爹的,注意各种临界情况!!无语的麻烦!!尤其是leetcode网站总显示服务器忙,还要等一会才有反应告诉我这不对。看july博客的程序员编程艺术,专门有讲这个的,我还是去做了==class Solution {public: int atoi(const char *s) { // Start typi原创 2013-08-13 15:12:39 · 238 阅读 · 0 评论 -
LeetCode 面试题 Regular expression matching(正则匹配)
一开始题目没题解清楚就用动态规划做,挂了。后来搜了网上解决方案,好简单啊!我就是不可能想到!#include class Solution {public: bool isMatch(const char *s, const char *p) { // Start typing your C/C++ solution below // DO NOT write转载 2013-08-13 16:25:57 · 270 阅读 · 0 评论 -
Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to string原创 2013-08-10 10:27:11 · 172 阅读 · 0 评论 -
Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4原创 2013-08-10 10:26:05 · 233 阅读 · 0 评论 -
Maximal Rectangle
果然又是动态规划。一开始也觉得是动态规划,但是对什么进行动态规划呢。原来认为的2种结果证明都是错的:1.记录每个元素左上角有多少个1,可这要m*n,难道再对每个矩形计算是否包含全是1?显然这是暴力的,不对!2.对每个(i,j)记录其高宽有多少,乘一下,但是这显然不行,不知道哪个是0啊!还想过递归,但是显然不行。网上搜了下,果然又是动态规划,但是其记录的是每列前面有多少个连续原创 2013-08-08 15:18:17 · 175 阅读 · 0 评论 -
Largest Rectangle in Histogram
有难度的题目,很难想到是用2个栈实现的,网上搜了解决方案恍然大悟啊!!来了新的元素如果比当前小,就pop其余栈,索引栈是塞入最前的比它大的元素的索引。来了新的元素,和当前数字一样大,也是压入栈的,不然会出现错误。总之是个好题目。原来以为用动态规划做,想啊想,就是没想到怎么用动态规划做,看来不是所有题目都是可以用动态规划解决的。class Solution原创 2013-08-08 14:06:59 · 215 阅读 · 0 评论 -
Unique Paths II
在Unique Path的基础上增加了有些不能增加的石块方法还是动态规划,没有改变class Solution {public: int uniquePathsWithObstacles(vector > &obstacleGrid) { // Start typing your C/C++ solution below // DO NOT wri原创 2013-08-07 21:06:45 · 221 阅读 · 0 评论 -
Set Matrix Zeroes
一直没想到怎么用常量控件,后来网上搜了解决方案是可以用矩阵原来的存储空间,膜拜!为什么我总是没有想到呢。难道是今天做了一整天的题目变白痴了==头脑好涨奋斗是为了什么class Solution {public: void setZeroes(vector > &matrix) { // Start typing your C/C++ sol原创 2013-08-07 20:41:16 · 174 阅读 · 0 评论 -
4Sum
和3Sum很类似,外面多了一层循环。考虑数字重复的情况。首先,对所有数据进行排序,然后基本代码可以使用3Sum的。class Solution {public: void findTwoNums(vector &num,int left,int neg_num,vector > &result, int &first_num,int &second_num)原创 2013-08-07 14:40:47 · 186 阅读 · 0 评论 -
Letter Combinations of a Phone Number
又是坑爹简单的题目==递归调用。。class Solution {public: vector result; void genSub(string &prev,string &digits,int l,vector &dic) { if(l>digits.length()) return ; if(l==dig原创 2013-08-07 16:16:22 · 256 阅读 · 0 评论 -
AddBinary
又是一个很简单的题目,比较每个末尾字符串是0还是1,有没有进位看来真是我太缺少挑战难题的勇气了!class Solution {public: string addBinary(string a, string b) { // Start typing your C/C++ solution below // DO NO原创 2013-08-07 15:45:26 · 176 阅读 · 0 评论 -
Search for a Range
又是一个二分的很简单的题目。递归调用。class Solution {public: vector idx_r; vector searchRange(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int原创 2013-08-07 11:55:02 · 186 阅读 · 0 评论 -
3Sum Closest
我还是用挺暴力方法找的,竟然过了。class Solution {public: int final_sum; void findTwoNums(vector &num,int left,int first_num,int target) { if(final_sum==target) return; if(原创 2013-08-07 13:35:28 · 206 阅读 · 0 评论 -
Implement strStr()
真心觉得这道题目很无聊,就是找子字符串还出这么诡异的名字,又是一道很简单的题目!!!和C++标准库的说明函数很类似,直接调用标准库好了。class Solution {public: char *strStr(char *haystack, char *needle) { // Start typing your C/C++ solution below原创 2013-08-07 12:20:17 · 168 阅读 · 0 评论 -
Remove Element
移除重复元素,这么简单的题目真不知道为了啥出的==class Solution {public: int removeElement(int A[], int n, int elem) { // Start typing your C/C++ solution below // DO NOT write int main() funct原创 2013-08-07 12:08:58 · 188 阅读 · 0 评论 -
Search Insert Position
注意边界情况,就是二分搜索的变形呗。又是一个简单题目class Solution {public: int searchSub(int A[],int l,int r,int target) { if(l>r) return l; if(l==r) { if(A[l]>=t原创 2013-08-07 11:06:01 · 176 阅读 · 0 评论 -
First Missing Positive -leetcode
首先想到的就是用set,每次插入就需要排序。很简单的一个题目。class Solution {public: int firstMissingPositive(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() func原创 2013-08-07 09:52:30 · 166 阅读 · 0 评论 -
Count and Say
一开始不知道题目什么意思,后来明白这样啊。所以就递归调用一开始还害怕是否有什么规律没有发现,会不会大集合时间超标,然后过了。怎么又是一个那么简单的题目、class Solution {public: string countAndSay(int n) { // Start typing your C/C++ solution b原创 2013-08-07 19:05:22 · 388 阅读 · 0 评论 -
Longest Common Prefix
不知道为什么这么简单的寻找公用字串在这里出现class Solution {public: string findPrefix(string &s1,string &s2) { string s=""; for(int i=0;i<s1.length() && i<s2.length();i++) {原创 2013-08-07 14:48:43 · 174 阅读 · 0 评论 -
3Sum
需要求三个数和为0,分解为求2个数能否何为一个固定数。先排序,两个数能否求某个解就是简单的了。需要注意的就是重复数字应该怎么样不都加入到结果中。class Solution {public: void findTwoNums(vector &num,int left,int neg_num,vector > &result) {原创 2013-08-07 13:04:35 · 226 阅读 · 0 评论 -
Minimum Depth of Binary Tree
这么点点代码就搞定的题目好简单!!!这是为了增长自信心吗!!int minDepth(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(root==NULL)return 0;原创 2013-08-08 15:23:59 · 161 阅读 · 0 评论 -
Word Ladder
以为是广度优先,结果有环的图的递归果然还是包括深度优先了!所以以下代码悲剧错了!!!class Solution {public: int min_depth; void dfs(string &start,string &end, unordered_set &dict,int depth) { if(min_depth!=INT_MAX) ret原创 2013-08-08 16:13:49 · 244 阅读 · 0 评论 -
N-Queens
又是简单深度优先问题class Solution {public: vector useCol; vector rowNum; vector> result; void dfs(vector &board,int n) { if(board.size()==n) {原创 2013-08-10 08:06:39 · 321 阅读 · 0 评论 -
Scramble String
果然又是动态规划,但是一直没有想明白这个应该是怎么动态规划的!!牛掰啊!!class Solution {public: bool isScramble(string s1, string s2) { // Start typing your C/C++ solution below // DO NOT write int main() f转载 2013-08-10 11:37:15 · 184 阅读 · 0 评论 -
Remove Nth Node From End of List
虽然书中看过若干次关于这个问题的,但是总是会报错,所以特别小心啊链表问题就是纠结。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} *原创 2013-08-10 10:27:25 · 147 阅读 · 0 评论 -
Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BAN原创 2013-08-10 10:27:21 · 186 阅读 · 0 评论