ZMR小站

很高兴遇见你~


  • 首页

  • 标签

  • 分类

  • 归档

p334

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

LeetCode p334 Increasing Triplet Subsequence 题解

1.题目:

Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.
Formally the function should:

Return true if there exists i, j, k
such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false. 

Your algorithm should run in O(n) time complexity and O(1) space complexity.

Examples:
Given [1, 2, 3, 4, 5],
return true.

Given [5, 4, 3, 2, 1],
return false.

阅读全文 »

p77

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

LeetCode p77 Combinations 题解

1.题目:

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],
]

阅读全文 »

p116

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

LeetCode p116 Populating Next Right Pointers in Each Node 题解

1.题目:

Given a binary tree

struct TreeLinkNode {
  TreeLinkNode *left;
  TreeLinkNode *right;
  TreeLinkNode *next;
}

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL.

Note:

You may only use constant extra space.
You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

For example,
Given the following perfect binary tree,

     1
   /  \
  2    3
 / \  / \
4  5  6  7

After calling your function, the tree should look like:

     1 -> NULL
   /  \
  2 -> 3 -> NULL
 / \  / \
4->5->6->7 -> NULL
阅读全文 »

p59

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

LeetCode p59 Spiral Matrix II 题解

1.题目:

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]

阅读全文 »

p69

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

LeetCode p69 Sqrt(x) 题解

1.题目:

Implement int sqrt(int x).
Compute and return the square root of x.

阅读全文 »

p367

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

LeetCode p367 Valid Perfect Square 题解

1.题目:

Given a positive integer num, write a function which returns True if num is a perfect square else False.
Note: Do not use any built-in library function such as sqrt.
Example 1:
Input: 16
Returns: True
Example 2:
Input: 14
Returns: False

阅读全文 »

p173

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

LeetCode p173 Binary Search Tree Iterator 题解

1.题目:

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
Calling next() will return the next smallest number in the BST.
Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.

阅读全文 »

1…181920…33
ZhangMengRou

ZhangMengRou

ZMR小站

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