leetcode
文章平均质量分 74
hefeiguo
这个作者很懒,什么都没留下…
展开
-
leetcode Longest Substring Without Repeating Characters
题意: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is原创 2013-10-29 21:58:03 · 960 阅读 · 1 评论 -
leetcode Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 朴素匹配好像超时,只能改用kmp了。关于kmp算法就不多说了,是很经典的问题了,我只想补充一下,kmp有两种写法,有一个优化版本的。这两原创 2013-12-27 22:12:45 · 1078 阅读 · 0 评论 -
leetcode Anagrams
最近虽然在做题,但是没有什么心情写博客。冷暖自知。。上面的题目做了135/150,随便贴个题目mark一下。 class Solution { public: vector anagrams(vector &strs) { vector res; map > st; int n=strs.size(); if(n<1) return res原创 2013-12-24 16:06:41 · 1148 阅读 · 0 评论 -
leetcode Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()原创 2013-12-11 21:48:28 · 1260 阅读 · 0 评论 -
leetcode Restore IP Addresses
好久没有更新了。最近在看stl源码解析。所以没有太做题了。顺便说下这本书,乍一看感觉很难,仔细看几天收获还是不错。只能感叹c++泛型机制好流弊。。。leetcode上面也就做了50个。 Given a string containing only digits, restore it by returning all possible valid IP address combinati原创 2013-11-14 19:57:01 · 783 阅读 · 0 评论 -
leetcode Longest Valid Parentheses
这个星期没有做题,跑出去玩了一圈。投个实习感觉被鄙视了,回来继续学习算了。 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the原创 2013-11-27 13:59:17 · 1555 阅读 · 0 评论 -
leetcode Binary Tree Preorder Traversal Binary Tree Postorder Traversal Inorder Traversal
最近几天都没有在leetcode做题了,今天来了做了几个二叉树。发现二叉树的题目真的很多,都是些一般的基础。没有什么说的。找两个最近的题做了贴上吧。 前序遍历二叉树,这个没有什么说的。借助于堆栈可以轻松的实现。题目要求不用递归实现。 class Solution { public: vector preorderTraversal(TreeNode *root) {原创 2013-11-08 15:27:06 · 1076 阅读 · 2 评论 -
leetcode Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", r原创 2013-11-19 21:51:50 · 1416 阅读 · 2 评论 -
leetcode Sort List (Sort a linked list in O(n log n) time using constant space complexity)
链表归并排序。原创 2013-11-18 12:53:17 · 7505 阅读 · 4 评论 -
leetcode Linked List Cycle II 继续写。
上次写了关于寻找环开始的解法,上网了搜了一下解法,发现原来这题是有故事的。具体大家可以去搜一搜,是个英文的blog。 代码就没有什么说的,如果知道了解法。代码是非常简单的。 大概的说一下解法, 1):定义两个指针S,F分别对应每次走一步,两步指针。就是快慢指针。初始都指向头结点。 2):当有环时,S==F。将其中一个指向(比如S)head,同时S和F相同的速度向后移动,当S==F时原创 2013-11-05 22:14:34 · 2850 阅读 · 2 评论 -
leetcode Word Break I II 算法分析
今天就挑两个题,这两个题的解法多种多样。思想就两个dp和dfs。看来oj还是非常的喜欢这两个技术。可能面试的时候,大家都喜欢这类题目吧。 题目1: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of原创 2013-11-03 15:09:13 · 6441 阅读 · 3 评论 -
leetcode Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be non原创 2013-11-15 11:28:20 · 853 阅读 · 0 评论 -
leetcode Linked List Cycle I II
今天抽空做了几题,突然发现下面的题都过去很久了,发现这两天更新了两个链表,就写这两个链表吧。 题目1: 题目就是判断一个链表是否有环。。要求不增加额外空间。如果以前没有做过可能会卡,这个题因为我以前见过就直接写代码了,没有在本地测试,直接写上去了,两次ac。。。 解法的证明我就不多说了,大家搜搜吧。。定义两个指针pre,p每次一个走两步,一个走一步。如果链表有环的话,那么就会pre==p。如原创 2013-10-31 21:52:58 · 1147 阅读 · 3 评论 -
leetcode Palindrome Partitioning I II
每到周五就想放松么,还是写两个题解吧。今天写了几个问题,发现都是动态规划的问题。就说这两个吧。 题1: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.原创 2013-11-01 22:14:57 · 1257 阅读 · 1 评论 -
leetcode Best Time to Buy and Sell Stock I II III
这三个题目是连续的就一同写下自己的想法吧。 题目1: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one an原创 2013-10-30 21:01:33 · 1455 阅读 · 1 评论 -
leetcode Container With Most Water
今天在leetcode上做了一天,随便找了一些题大概做了10几个,感觉面试题真的很能体现个人能力啊。 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the原创 2013-10-29 21:27:10 · 866 阅读 · 0 评论 -
leetcode Reverse Words in a String
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". 题目比较简单,就是倒置一下字符串。中间说的几个细节,开头,末尾,包括中间都可能出现多个空格,都需要忽略。格式化为每个词之间只有一个空格的原创 2014-03-11 10:42:23 · 942 阅读 · 0 评论