0%

How does the writer like to treat young people?

People are always talking about ‘the problem of youth’. If there is one—which I take leave to doubt—then it is older people who create it, not the young themselves. Let us get down to fundamentals and agree that the young are after all human beings—people just like their elders. There is only one difference between an old man and a young one: the young man has a glorious future before him and the old one has a splendid future behind him: and maybe that is where the rub is.

When I was a teenager, I felt that I was just young and uncertain—that I was a new boy in a huge school, and I would have been very pleased to be regarded as something so interesting as a problem. For one thing, being a problem gives you a certain identity, and that is one of the things the young are busily engaged in seeking.

I find young people exciting. They have an air of freedom, and they have not a dreary commitment to mean ambitions or love of comfort. They are not anxious social climbers, and they have no devotion to material things. All this seems to me to link them with life, and the origins of things. It’s as if they were, in some sense, cosmic beings in violent and lovely contrast with us suburban creatures. All that is in my mind when I meet a young person. He may be conceited, ill-mannered, presumptuous or fatuous, but I do not turn for protection to dreary cliches about respect for elders—as if mere age were a reason for respsect. I accept that we are equal, and I will argue with him, as an equal, if I think he is wrong.

题目

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.

Example 1:

1
2
3
4
5
6
7
8
9
10
11
12
Input: 
1
/ \
0 2

L = 1
R = 2

Output:
1
\
2

Example 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Input: 
3
/ \
0 4
\
2
/
1

L = 1
R = 3

Output:
3
/
2
/
1

难度

Easy

方法

采用递归的方法。如果root为空,则直接返回root; 如果root的值<L,表示root及其左子树所有节点都<L,那么需要改变root节点,从root.right中重新寻找root节点。同理,当root的值>R时,需要从root.left中重新寻找root节点。当L<=root.val<=R时,则递归处理root的左右子树。

python代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None

class Solution(object):
def trimBST(self, root, L, R):
if root == None:
return None

if root.val < L:
return self.trimBST(root.right, L, R)
if root.val > R:
return self.trimBST(root.left, L, R)

root.left = self.trimBST(root.left, L, R)
root.right = self.trimBST(root.right, L, R)

return root

root = TreeNode(1)
root.left = TreeNode(0)
root.right = TreeNode(2)
assert Solution().trimBST(root, 3, 4) == None

root = TreeNode(3)
root.left = TreeNode(0)
root.right = TreeNode(4)
root.left.right = TreeNode(2)
root.left.right.left = TreeNode(1)
root = Solution().trimBST(root, 1, 3)
assert root.val == 3
assert root.left.val == 2
assert root.right == None
assert root.left.left.val == 1

晚上11点前睡,你能做到吗?

1

前几天,北京大学法学院2017迎新典礼上,白建军教授给新生的建议火遍全网:“早睡早起+把微信朋友圈控制在10人以内+拒绝以陪读为目的的表白。这个办法能不能把你弄成精英中的精英,我不知道。但试过的,都说好。”

这3条建议中,我试过“早睡早起”这一条。抓住早睡这个突破点后,早起也能轻松上手,投资小,回报大,性价比高。

但我观察周围,这大约是个年轻人全民熬夜的年代。有人工作分工没办法,有人夜晚干活效率高,有人单纯性晚睡拖延症。许多人在睡觉方面缺乏自制力,晚上不想睡,手机像长在手上似的;早上起不来,醒来就跟闹钟置气,一天到晚觉得困。

原来在我们这个时代,早睡早起,真的变成了最知易行难的自律。

虽然网上不时会曝出因为熬夜导致健康受损甚至猝死的案例,不管当事人如何苦口婆心地劝诫一定要规律作息,千万不要熬夜,可还是有许多人会说,道理我们都知道的,可就是忍不住熬夜啊。

2

以前我在深圳工作时,有段时间早上五点多就起床,去附近的荔香公园快走。我发现这个时段公园里老年人的密度极高,他们神采奕奕地进行着做操、跑步、挥鞭等各种锻炼。

而我看到年轻人密度最高的时段是晚上。我有时加班到晚上十点多,在赶回家睡觉的路上,看到写字楼里还有大片灯光,商场周围还有K歌聚会,便利店里还有人进人出。

这好像一幕滑稽戏,早睡早起的老年人和晚睡晚起的年轻人,处于不同的时区。结果,老年人状态越来越燃,年轻人却越活越没精神。

其实,早睡早起,是性价比最高的自律。

前东家大领导30多岁那年,大病一场之后,作息习惯大换血,他把工作模式从没日没夜改为早睡早起,工作绩效不减反增;

我婶婶婚后越长越好看,随着年纪增长,她的皮肤却比初见时细腻白嫩了很多,她说因为自己单身时熬的夜,结婚后都戒了;

我早上五点多去公园的事迹被同事们听说后,都叉着腰笑我过着老年人的生活,可我那时的工作效率和身体状态都处于巅峰级别。

3

我最近采访了一位服装品牌的女创始人。采访前,我没想到作为一个张罗创业公司大小业务、高频参加品牌活动和电视节目、照顾一儿一女起居饮食的妈妈,竟然这么活力四射、声音带感。

我掘地三尺地问她如何保持这么赞的状态,她归因为早睡早起,作息规律。

她告诉我,她之前刚生完孩子那阵,起夜奶娃导致睡不好觉,眼圈深黑、披头散发,整天抱着负面情绪的洋快餐在啃啊啃。

这几年,她的生活质量因睡眠改善而改头换面。她晚上八九点把小儿子哄睡,然后接着给大女儿讲睡前故事,通常女儿还没睡着,她倒先催眠了自己。

晚上九点十点就入睡,第二天早上七点起床,能最大限度地保证自己电力满格。

同一个人的两个对照组也证明,每天这种早睡早起的节律感,能让人心情愉悦,办事高效。但凡是眼不拙的人,都能看出老天在打赏敬重生物钟的人。

4

以前,一位文笔有灵气的女作者,她白天上班,晚上写作,每次看她公众号的更文时间都很心疼她。

她做任何工作,业绩都要拼到极致,任何吃穿用度都要用到最好,晚上一两点才睡是常态,生病了还边打吊瓶边写作。在她的一篇文章里,她悲情地说自己掉发严重、记性变差,怀疑自己会早早死掉。

我不看好这种“自毁式的勤奋”,这哪是鸡血,分明是狗血。我多希望她能把晚睡晚起置换成早睡早起,在工作时长差不多的情况下,把对身体的伤害降至最低。

以保重身体为前提的努力,是一种远见。

我热爱写作,也想长久写作,所以,榜样都是精力足以撑起终生写作的人。比如村上春树,对于小说家及其创作而言,他认为最重要的就是集中力和耐力。这就是他早睡早起、朝五晚九、规律生活、坚持跑步的原因,是为了身体好,有体力,以便能有写一本书的能量。

日本另一位高产优产的作家松浦弥太郎,也在书中写过早睡早起、作息规律的重要性。他说:每天晚上十点左右上床,早上五点起床,每隔一天要去锻炼,快走十公里,然后八点去上班。这种规律性的生活对于我来说,是非常重要的工作之一。

健康管理是最重要的工作。有规律地生活,是做好一切的最基本要求。时刻保持旺盛的精力,这也是获得信任的第一步。

如果你有心追求事业,热爱生活,深爱家人,那么一定要把健康当成顶层设计,挂起晚睡的战靴,远离手机这类晚睡培养皿,第二天早点起来提升或运动。

我始终相信,早睡早起身体好。而身体好的人,才有一切的选择权和被选择权。

转载

来源:哪梁爽哪喜庆(ID: zheliangshuang) | 作者: 梁爽 | 平台: 新浪微博

题目

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.

The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R(Right), L(Left), U(Up) and D(down). The output should be true or false representing whether the robot makes a circle.

Example 1:

1
2
Input: "UD"
Output: true

Example 2:

1
2
Input: "LL"
Output: false

难度

Easy

方法

对每个方向计数,如果L出现的次数==R出现的次数,并且U出现的次数==D出现的次数,则robot会回到初始位置,返回True;否则返回False

python代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution(object):
def judgeCircle(self, moves):
"""
:type moves: str
:rtype: bool
"""
counter = {}
for c in moves:
counter[c] = counter.get(c, 0) + 1

return counter.get("L") == counter.get("R") and counter.get("U") == counter.get("D")

assert Solution().judgeCircle("UD") == True
assert Solution().judgeCircle("LL") == False

题目

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node’s value is the smaller value among its two sub-nodes.

Given such a binary tree, you need to output the second minimum value in the set made of all the nodes’ value in the whole tree.

If no such second minimum value exists, output -1 instead.

Example 1:

1
2
3
4
5
6
7
8
9
Input: 
2
/ \
2 5
/ \
5 7

Output: 5
Explanation: The smallest value is 2, the second smallest value is 5.

Example 2:

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

Output: -1
Explanation: The smallest value is 2, but there isn't any second smallest value.

难度

Easy

方法

首先将最小值minimum和第二小值second_minimum置为maxint,递归遍历二叉树,如果节点值比最小值小,则将最小值赋给第二小值,然后将最小值更新为节点值;如果节点值比最小值大,并且节点值小于第二小值,则将第二小值更新为节点值。节点遍历后如果第二小值==maxint,即没有第二小值,则返回-1

python代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import sys

class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None

class Solution(object):
def findSecondMinimumValue(self, root):
self.min = sys.maxint
self.second_min = sys.maxint
def traverse(node):
if node:
if node.val < self.min:
self.second_min = self.min
self.min = node.val
elif node.val < self.second_min and node.val != self.min:
self.second_min = node.val

traverse(node.left)
traverse(node.right)

traverse(root)
if self.second_min != sys.maxint:
return self.second_min
return -1

root = TreeNode(2)
root.left = TreeNode(2)
root.right = TreeNode(5)
root.right.left = TreeNode(5)
root.right.right = TreeNode(7)

assert Solution().findSecondMinimumValue(root) == 5

题目

We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactly 1.
Now, given an integer array, you need to find the length of its longest harmonious subsequence among all its possible subsequences.
Example 1:

1
2
3
**Input:** [1,3,2,2,5,2,3,7]
**Output:** 5**
Explanation:** The longest harmonious subsequence is [3,2,2,2,3].

Note: The length of the input array will not exceed 20,000.

难度

Easy

方法

用一个map存放所有值出现的次数,对于map中的每个num,求出numnum+1出现的次数之和,找出最大值即可

python代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution(object):
def findLHS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums_count = {}
for num in nums:
nums_count[num] = nums_count.get(num, 0) + 1

result = 0
for num in nums_count:
count = nums_count[num]
if nums_count.get(num+1):
result = max(result, count + nums_count[num+1])
return result

assert Solution().findLHS([1,3,2,2,5,2,3,7]) == 5

题目

Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.
Note:

  1. All letters in hexadecimal (a-f) must be in lowercase.
  2. The hexadecimal string must not contain extra leading 0s. If the number is zero, it is represented by a single zero character ‘0’; otherwise, the first character in the hexadecimal string will not be the zero character.
  3. The given number is guaranteed to fit within the range of a 32-bit signed integer.
  4. You must not use any method provided by the library which converts/formats the number to hex directly.

Example 1:

1
2
3
4
Input:
26
Output:
"1a"

Example 2:

1
2
3
4
Input:
-1
Output:
"ffffffff"

难度

Easy

方法

对于num,每次num&15便可取到最右的4个bit位,然后num>>4循环处理即可将num转换为16进制。因为num范围为32bit位,因此最多右移8次,每次右移4个bit位。当num不为0时,转换为16进制后,前面有0需去除。

python代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution(object):
def toHex(self, num):
"""
:type num: int
:rtype: str
"""
if num == 0:
return "0"
map = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
hex = ""
for i in range(8):
hex = map[num & 0xf] + hex
num = num >> 4

return hex.lstrip("0")

assert Solution().toHex(-1) == "ffffffff"
assert Solution().toHex(26) == "1a"

题目

Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.

Note:

  1. The length of both num1 and num2 is < 5100.
  2. Both num1 and num2 contains only digits 0-9.
  3. Both num1 and num2 does not contain any leading zero.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

难度

Easy

方法

从低位到高位相加,每一位的结果为除10取余,如果有进位1,则在下一位求和的时候需要+1,循环处理即可。注意最高位相加后如果有进位,需要在结果的最前面补1

python代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution(object):
def addStrings(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
i = len(num1) - 1
j = len(num2) - 1
num = ""
carry = 0
while i >= 0 or j >= 0:
result = carry
if i >= 0:
result += int(num1[i])
if j >= 0:
result += int(num2[j])
if result >= 10:
carry = 1
else:
carry = 0
num = str(result % 10) + num
i -= 1
j -= 1
if carry == 1:
num = "1" + num
return num

assert Solution().addStrings("111", "234965") == "235076"
assert Solution().addStrings("999", "1") == "1000"

题目

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

Example:
Given a binary tree

1
2
3
4
5
    1
/ \
2 3
/ \
4 5

Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].

Note: The length of path between two nodes is represented by the number of edges between them.

难度

Easy

方法

求出每个节点左右子树的深度,相加即为该节点对应的diameter,最后取最大的diameter即可

python代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None

class Solution(object):
def diameterOfBinaryTree(self, root):
self.diameter = 0
def depth(node):
if node:
l_depth = depth(node.left)
r_depth = depth(node.right)
self.diameter = max(self.diameter, l_depth + r_depth)
return max(l_depth, r_depth) + 1
return 0

depth(root)
return self.diameter


root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)
assert Solution().diameterOfBinaryTree(root) == 3

题目

Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.
Example:

1
2
Input: s = "abcdefg", k = 2
Output: "bacdfeg"

Restrictions:

  1. The string consists of lower English letters only.
  2. Length of the given string and k will in the range [1, 10000]

难度

Easy

方法

每次处理s[0:2k],将s[0:k]逆序后拼接s[k:2k],然后将s[2k:]赋值给s,循环处理直至s为空即可

python代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution(object):
def reverseStr(self, s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
result = ""
while s:
temp = s[0:k]
result += temp[::-1] + s[k:2*k]
s = s[2*k:]
return result

assert Solution().reverseStr("abcdefg", 2) == "bacdfeg"