bookmark_borderThe difference of “protected” in Java vs C++

在学校的时候一直写C和C++,工作后开始写Java,有些差别是边写边慢慢发现的,比如protected这个权限。

在C++里,protected是只有当前class和subclass可以访问。所有其他的class不行(友元除外)

在Java里,protected是当前class,subclass,和同一package内的其他class可以访问。关键就在于这最后一个区别,在自己写code的时候基本上各个class都在同一个package之内,于是protected的成员在基本所有类都可以访问了,这个访问权限略弱于public(其他package也可以访问),却强过默认的package-private(子类subclass不能访问)。

在Java的访问控制系统内,package有比较重要的地位,总体感觉就是“一个package里面的代码都是你自己管理的,访问权限什么的你拿脑子记住就好了”

bookmark_borderLeetcode笔记(13)Best Time to Buy and Sell Stock III

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 at most two transactions.

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

Continue reading “Leetcode笔记(13)Best Time to Buy and Sell Stock III”

bookmark_borderLeetcode笔记(12)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 constant extra space.

For example,
Given the following binary tree,

         1
       /  \
      2    3
     / \    \
    4   5    7

After calling your function, the tree should look like:

         1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \    \
    4-> 5 -> 7 -> NULL

Continue reading “Leetcode笔记(12)Populating Next Right Pointers in Each Node II”

bookmark_borderLeetcode笔记(11)Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

Return all such possible sentences.

For example, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].

A solution is ["cats and dog", "cat sand dog"].

Continue reading “Leetcode笔记(11)Word Break II”

bookmark_borderLeetCode笔记(9)Largest Rectangle in Histogram

Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].

The largest rectangle is shown in the shaded area, which has area = 10 unit.

For example,
Given height = [2,1,5,6,2,3],
return 10.

Continue reading “LeetCode笔记(9)Largest Rectangle in Histogram”

bookmark_borderLeetCode笔记(8)Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

For example,
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

Your algorithm should run in O(n) complexity.

Continue reading “LeetCode笔记(8)Longest Consecutive Sequence”

bookmark_borderLeetCode笔记(6)Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake.

Recover the tree without changing its structure.

Note:
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? Continue reading “LeetCode笔记(6)Recover Binary Search Tree”