Algorithms
文章平均质量分 58
LadyFeline
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
【Leetcode长征系列】Palindrome Number
原型:Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of conver原创 2014-07-31 12:02:20 · 900 阅读 · 5 评论 -
【Leetcode长征系列】Length of Last Word
原题:Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word原创 2014-08-08 10:56:19 · 656 阅读 · 0 评论 -
【Leetcode长征系列】Construct Binary Tree from Preorder and Inorder Traversal
原题:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.思路:通过先序我们可以找到根节点,再到中序序列中去找到根节点,根节点的左边便为左子树,右边为右子原创 2014-08-21 14:54:06 · 825 阅读 · 0 评论 -
【Leetcode长征系列】Single Number II
原题:Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it withou原创 2014-08-15 11:47:09 · 694 阅读 · 0 评论 -
【Leetcode长征系列】Construct Binary Tree from Inorder and Postorder Traversal
原题:Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.思路:和上一题一样,后续我们可以通过最后一个值得到根的值,同样可以通过定位根的值得到左右子树的原创 2014-08-21 15:03:07 · 667 阅读 · 0 评论 -
【Leetcode长征系列】Insertion Sort List
原题:Sort a linked list using insertion sort.知识把原创 2014-08-01 14:18:08 · 610 阅读 · 0 评论 -
【Leetcode长征系列】Reverse Integer
原题:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321zhedao原创 2014-07-14 15:58:48 · 728 阅读 · 0 评论 -
【Leetcode长征系列】Maxmium depth of binary tree
原题:Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.原创 2014-07-14 13:52:34 · 560 阅读 · 0 评论 -
【Leetcode长征系列】Same tree
原题:Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.原创 2014-07-14 14:15:28 · 557 阅读 · 0 评论 -
【Leetcode长征系列】Single Number
为这个问题纠结了好久表示自己太菜了。这个问题主要应该是二进制思想的英语原创 2014-07-14 13:02:14 · 632 阅读 · 0 评论 -
【Leetcode长征系列】Letter Combinations of a Phone Number
原题:Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input原创 2014-08-21 17:14:35 · 887 阅读 · 0 评论 -
【Leetcode长征系列】Binary Tree Preorder Traversal
原题:Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive solutio原创 2014-07-15 16:51:35 · 500 阅读 · 0 评论 -
【Leetcode长征系列】 Divide Two Integers
原题:Divide two integers without using multiplication, division and mod operator.思路: 记录除数与被除数的符号跑原创 2014-07-30 14:32:01 · 715 阅读 · 1 评论 -
【Leetcode长征系列】Validate Binary Search Tree
原题:Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's k原创 2014-08-06 13:19:34 · 731 阅读 · 0 评论 -
【Leetcode长征系列】Linked List Cycle
原题:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?原创 2014-07-15 14:50:44 · 476 阅读 · 0 评论 -
【Leetcode长征系列】Valid Parentheses
原题:Say you have an array for which the ith element is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the s原创 2014-07-30 15:24:23 · 686 阅读 · 0 评论 -
【Leetcode长征系列】Minimum Depth of Binary Tree
原题:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.思路:深度优先原创 2014-07-30 14:48:01 · 695 阅读 · 0 评论 -
【Leetcode长征系列】Sum Root to Leaf Numbers
原题:Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number123.Find the to原创 2014-07-30 13:54:24 · 597 阅读 · 0 评论 -
【Leetcode长征系列】Plus One
原题:Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.思路:原创 2014-07-29 15:44:35 · 726 阅读 · 0 评论 -
【Leetcode长征系列】Path Sum II
原题:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \原创 2014-08-04 12:00:32 · 593 阅读 · 0 评论 -
【Leetcode长征系列】Convert Sorted Array to Binary Search Tree
原题:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.【复习时间】:平衡二叉树(Balanced Binary Tree)具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。常用算原创 2014-08-11 10:15:11 · 615 阅读 · 0 评论 -
Reverse Linked List
Reverse a singly linked list.解析:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class原创 2015-05-06 13:43:10 · 487 阅读 · 0 评论 -
Rotate Array
Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].class Solution {public: void rotate(int n原创 2015-05-01 13:16:40 · 397 阅读 · 0 评论 -
【Leetcode长征系列】Merge k Sorted Lists
原题:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.思路:两条两条地合并。时间复杂度为O(n),n为所有链表节点和。代码:/** * Definition for singly-linked list. * struct List原创 2014-08-14 16:49:15 · 1091 阅读 · 2 评论 -
【Leetcode长征系列】Pow(x, n)
原题:Implement pow(x, n).思路:递归计算pow。class Solution {public: double pow(double x, int n) { long long int mid = n/2; int d = n%2; if(n==0) return 1; if(n==1) return原创 2014-08-14 13:53:10 · 861 阅读 · 0 评论 -
【Leetcode长征系列】Sqrt(x)
原题:Implement int sqrt(int x).Compute and return the square root of x.==============================以下为引用====================================牛顿迭代法 为了方便理解,就先以本题为例: 计算x2 = n的解,令f(原创 2014-08-14 14:58:58 · 1075 阅读 · 0 评论 -
【Leetcode长征系列】Convert Sorted List to Binary Search Tree
原题:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.思路:和处理数组的思想类似,用递归。代码:原创 2014-08-12 11:23:35 · 710 阅读 · 4 评论 -
【Leetcode长征系列】Longest Common Prefix
原题:Write a function to find the longest common prefix string amongst an array of strings.思路:pre赋给第一个strs值原创 2014-08-11 16:52:09 · 791 阅读 · 0 评论 -
【Leetcode长征系列】Valid Sudoku
原题:Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character'.'.A partially f原创 2014-08-03 17:11:19 · 531 阅读 · 0 评论 -
【Leetcode长征系列】Best Time to Buy and Sell Stock
原题:Say you have an array for which the ith element is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the s原创 2014-08-10 17:51:55 · 800 阅读 · 0 评论 -
【Leetcode长征系列】Evaluate Reverse Polish Notation
原题:Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: ["2", "1",原创 2014-08-10 20:45:13 · 571 阅读 · 0 评论 -
【Leetcode长征系列】Symmetric Tree
原题:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the fo原创 2014-08-10 11:09:45 · 560 阅读 · 0 评论 -
【Leetcode长征系列】Unique Binary Search Trees II
原题:Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return all 5 unique BST's shown below. 1原创 2014-08-10 16:07:30 · 599 阅读 · 0 评论 -
【Leetcode长征系列】Populating Next Right Pointers in Each Node II
原题:Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use consta原创 2014-07-18 15:08:57 · 545 阅读 · 0 评论 -
【Leetcode长征系列】Add Binary
原题:Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".原创 2014-08-10 19:45:45 · 1078 阅读 · 0 评论 -
【Leetcode长征系列】Best Time to Buy and Sell Stock II
原题 :Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (原创 2014-07-14 16:08:24 · 589 阅读 · 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".Clarification:What constitutes a word?A sequence of non-s原创 2014-08-10 16:34:51 · 544 阅读 · 0 评论 -
【Leetcode长征系列】Linked List Cycle II
原题:Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?先上代码:/** * Definition for singly-原创 2014-07-17 15:35:52 · 737 阅读 · 1 评论 -
【Leetcode长征系列】Remove Duplicates from Sorted Array II
原题:Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1原创 2014-07-22 17:17:42 · 672 阅读 · 0 评论 -
【Leetcode长征系列】Pascal's Triangle II
原题:Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?思路:哟原创 2014-07-22 18:09:49 · 520 阅读 · 0 评论
分享