ZMR小站

很高兴遇见你~


  • 首页

  • 标签

  • 分类

  • 归档

p199

发表于 2016-09-11 | 分类于 blog

LeetCode p199 Binary Tree Right Side View 题解

1.题目:

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
For example:
Given the following binary tree,

1 <—
/ \
2 3 <—
\ \
5 4 <—

You should return [1, 3, 4].

阅读全文 »

p145

发表于 2016-09-10 | 分类于 blog

LeetCode p145 Binary Tree Postorder Traversal 题解

1.题目:

Given a binary tree, return the postorder traversal of its nodes’ values.
For example:
Given binary tree {1,#,2,3},

1
\
2
/
3

return [3,2,1].

阅读全文 »

p121

发表于 2016-09-10 | 分类于 blog

LeetCode p121 Best Time to Buy and Sell Stock 题解

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 and sell one share of the stock), design an algorithm to find the maximum profit.
Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 5
max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:
Input: [7, 6, 4, 3, 1]
Output: 0
In this case, no transaction is done, i.e. max profit = 0.

阅读全文 »

p153

发表于 2016-09-10 | 分类于 blog

LeetCode p153 Find Minimum in Rotated Sorted Array 题解

1.题目:

Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
Find the minimum element.
You may assume no duplicate exists in the array.

阅读全文 »

p53

发表于 2016-09-10 | 分类于 blog

LeetCode p53 Maximum Subarray 题解

1.题目:

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
the contiguous subarray [4,-1,2,1] has the largest sum = 6.

阅读全文 »

p235

发表于 2016-09-09 | 分类于 blog

LeetCode p235 Lowest Common Ancestor of a Binary Search Tree 题解

1.题目:

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”

    _______6______
   /              \
___2__          ___8__

/ \ / \
0 _4 7 9
/ \
3 5
For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

阅读全文 »

p70

发表于 2016-09-09 | 分类于 blog

LeetCode p70 Climbing Stairs 题解

1.题目:

You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

阅读全文 »

1…192021…33
ZhangMengRou

ZhangMengRou

ZMR小站

230 日志
4 分类
14 标签
© 2019 ZhangMengRou
由 Hexo 强力驱动
|
主题 — NexT.Gemini v5.1.4