代码改变世界

[LeetCode 75] Sort Colors

2017-11-20 00:16 by naturesound, 151 阅读, 0 推荐, 收藏, 编辑
摘要:Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, 阅读全文

[LeetCode 297] Serialize and deserialize binary tree

2017-11-18 13:19 by naturesound, 174 阅读, 0 推荐, 收藏, 编辑
摘要:Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or 阅读全文

[LeetCode 65] Valid Number (有效数字表达)

2017-11-17 02:35 by naturesound, 476 阅读, 0 推荐, 收藏, 编辑
摘要:Validate if a given string is numeric. Some examples: "0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true Note: It is intended for th 阅读全文

[LeetCode 621] Task Scheduler

2017-11-13 00:52 by naturesound, 403 阅读, 0 推荐, 收藏, 编辑
摘要:这题虽是M难度,却花了我比Hard难度还多的时间。 1.刚开始是想用一个堆排序来实现,排序的比较标准是两个:1.任务的最早启动时间;2.任务的重复次数。并且把1作为第一比较。 后来发现这个思路有误:只要A,B的最早启动时间都小于当前时间,不管A和B哪个最早启动时间更小,这时只需要考虑重复次数:哪个重 阅读全文

LeetCode 273. Integer to English Words

2017-11-12 00:26 by naturesound, 178 阅读, 0 推荐, 收藏, 编辑
摘要:此题是一个数字字符转化类问题。没有什么复杂的技巧,只要想清楚2个转化的规律: 1,此题给出的非负数字上限是2^31-1,大约是十亿的数量级(2^10 = 1024, (2^10)^3 ~1billion)。而我们已知每1000进位方式有三种后缀,thousand, million和billion,这 阅读全文

LeetCode 283 Move Zeroes

2017-11-11 01:55 by naturesound, 148 阅读, 0 推荐, 收藏, 编辑
摘要:是道容易题,可以想到很多解法,但有些解法容易出小错误: 1)交换法: 用一个指针p保存零元素起始位置。遍历数组,当发现是非零元素,则和前面的零元素交换,同时p自增一。 这个方法有几个地方容易想错,首先p的初始值怎么定义,如果你把p定义为第一个零元素的下标,则刚开始p你可能会定义为-1,因为你不知道第 阅读全文

[LeetCode] 129 Sum Root to Leaf Numbers 求根到叶节点数字之和

2017-04-26 06:30 by naturesound, 183 阅读, 0 推荐, 收藏, 编辑
摘要:此题题意是求所有从根结点到叶节点的路径转化为数字后之和。 因为是二叉树,容易想到是用递归求解。 整体思想是从根到叶子进行遍历,其中不断保存当前的中间结果(上一层的结果乘以10后加上当前层根节点的数值)并通过参数向下传递。。。 到达叶子节点时可以逐层返回最终结果。解法不难,但有几个细节需要考虑清楚。 阅读全文