leetcode
文章平均质量分 78
facactus
这个作者很懒,什么都没留下…
展开
-
Reverse Words in a String
字符串处理应该属class Solution {public: void reverseWords(string &s) { if(s==""){ return; } s=s+" "; string str=""; string ret=""; int flag=1; for(原创 2014-03-19 11:46:01 · 307 阅读 · 0 评论 -
[leetcode]Pow(x, n)&&Sqrt(x)
Pow(x,n) 求x的n次方。需要用到分治法,把x的n次方hu原创 2014-05-22 11:12:26 · 301 阅读 · 0 评论 -
[leetcode]Minimum Path Sum
思路:基础动态规划问题f代码:class Solution {public: int minPathSum(vector > &grid) { int m=grid.size(); int n=grid[0].size(); int f[m][n]; f[0][0]=grid[0][0]; f原创 2014-06-05 17:36:14 · 192 阅读 · 0 评论 -
[leetcode]Set Matrix Zeroes
题目描述:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.思路:原创 2014-06-09 16:54:53 · 250 阅读 · 0 评论 -
[leetcode]Flatten Binary Tree to Linked List
题目描述:Given a binary tree, flatten it to a linked list in-place.思路:遍历二叉树,将原创 2014-05-26 22:55:58 · 190 阅读 · 0 评论 -
[leetcode]Jump Game &&Jump Game II
题目描述:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.原创 2014-06-12 22:04:09 · 223 阅读 · 0 评论 -
[leetcode]Trapping Rain Water
题目描述:Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1原创 2014-06-12 10:44:08 · 235 阅读 · 0 评论 -
[leetcode]Surrounded Regions
题目描述:Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region.For example,X X原创 2014-06-12 11:32:34 · 225 阅读 · 0 评论 -
[leetcode]Palindrome Partitioning
题目描述:Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab",Return [原创 2014-05-30 22:46:16 · 203 阅读 · 0 评论 -
[leetcode]Surrounded Regions
题目描述:Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region.For example,X X原创 2014-08-18 11:26:00 · 257 阅读 · 0 评论 -
[leetcode]Binary Tree Maximum Path Sum
题目描述:Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3R原创 2014-08-31 16:58:30 · 221 阅读 · 0 评论 -
[leetcode]Path Sum&&Path Sum II(剑指offer面试题25)
题目描述:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tr原创 2014-06-04 12:04:09 · 236 阅读 · 0 评论 -
[leetcode]Minimum Depth of Binary Tree
题目描述:求二叉树的最小深度,即从根节点到yejiedian原创 2014-06-04 11:57:53 · 315 阅读 · 0 评论 -
Search a 2D Matrix
观察矩阵规律,从右上角开始找,根据比较结果向下或者向左遍历。class Solution {public: bool searchMatrix(vector > &matrix, int target) { int m=matrix.size(); int n=matrix[0].size(); if(m==0||n==0){原创 2014-03-19 11:55:09 · 285 阅读 · 0 评论 -
Construct Binary Tree from Inorder and Preorder(Postorder) Traversal
通过前序中序序列构造二叉树以及通过后序中序序列构造二叉树。用递归,每次通过寻找根节点,改变peorder和inorder的起止位置,实现递归。前序中序序列构造二叉树代码:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNo原创 2014-03-19 20:49:25 · 335 阅读 · 0 评论 -
二叉树的非递归前序、中序、后序遍历
递归的前序、中序、后序遍历与概念基本一致,简单明了。而非递归遍历稍微麻烦,确实最容易被问到的东西。首先看非递归前序遍历:用栈实现,先访问根节点,然后压入右子树,再压入左子树。Binary Tree Preorder Traversal /** * Definition for binary tree * struct TreeNode { * int va原创 2014-03-24 19:49:24 · 257 阅读 · 0 评论 -
Copy List with Random Pointer
剑指offer上的题目,实现也是按照书上的方法。/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label; * RandomListNode *next, *random; * RandomListNode(i原创 2014-03-24 19:59:02 · 210 阅读 · 0 评论 -
[leetcode]Spiral Matrix&&Spiral Matrix II
Spiral Matrix 给定一个m*n的矩阵,按照顺时针方向,返回螺旋方向上的元素列表。原创 2014-05-07 20:48:14 · 317 阅读 · 0 评论 -
[leetcode]Edit Distance
编辑距离问题,典型的动态规划解决的问题。原创 2014-05-07 21:02:55 · 208 阅读 · 0 评论 -
Best Time to Buy and Sell Stock
第一题:只有一次股票交易,从第一个开始设为min,遍历过程中每次比较更新min值,同时计算股票收益,比较之前的股票收益,最大的即为最大收益。Best Time to Buy and Sell Stock class Solution {public: int maxProfit(vector &prices) { int max=0; in原创 2014-03-26 22:03:05 · 240 阅读 · 0 评论 -
[Leetcode]Sudoku Solver&&Valid Sudoku
数独游戏。规则很简单,每行原创 2014-04-09 18:13:33 · 305 阅读 · 0 评论 -
[leetcode]Reorder List
题目描述:Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reo原创 2014-06-04 17:02:15 · 221 阅读 · 0 评论 -
[leetcode]Multiply Strings
题目描述:Given two numbers represented as strings, return multiplication of the numbers as a string.分析原创 2014-06-24 11:31:20 · 181 阅读 · 0 评论 -
[leetcode][java]Merge Two Sorted Lists 合并两个排序的链表
递归: public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) { return l2; } if (l2 == null) { return l1;原创 2017-03-26 22:24:21 · 237 阅读 · 0 评论