Floyd 判圈算法
2017-12-01
判断一个单链表中是否存在环,并找出环的入口: https://leetcode.com/problems/linked-list-cycle-ii/ Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up: Can you solve it without using extra space? 不知道 LeetCode 难度评级的标准是什么,有的 hard 题看完题目就有思路,像这种 medium 题我琢磨半天也想不出空间复杂度为 $O(1)$ 的解法。后来了解到这一题已经有著名的解法了,即 Floyd 判圈算法,没错,就是求最短路径的弗洛伊德算法的那个 Floyd。 …
Single Number II
2017-11-27
LeetCode 的一道题,在一个 int 数组中,其余数值均出现了 3 次,只有一个数值出现了 1 次,求这个数: https://leetcode.com/problems/single-number-ii/description/ Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? …
Gas Station
2017-10-14
加油站问题,LeetCode 链接: https://leetcode.com/problems/gas-station/description/ There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations. …
分析和解决手机重启若干次后,虚拟按键、状态栏等失效的问题
2017-09-19
Android 有种特殊的 App 叫老化测试工具,一般包括若干次重启、存储读写、音频播放、视频播放等流程,是一个预装在手机的 APK,出厂时会去掉。 测试做老化测试挂机 24 小时后,手机概率性出现问题,现象主要包括** Home 键和 Recent 键不能用、无法下拉快捷设置和通知栏、锁屏失效、开发者模式无法打开**等。复现概率很低,每台重启 50 次,10 台中出现 0 ~ 3 台。诡异的是,我这边也会每天挂机,但一台都没有复现。 …
工厂模式 Factory Pattern
2017-09-09
工厂模式可分为简单工厂模式 Simple Factory Pattern、工厂方法模式 Factory Method Pattern 和 抽象工厂模式 Abstract Factory Pattern。 简单工厂模式 简单工厂模式实际上是对产品创建逻辑做了统一的封装,包含如下角色: …
在 Android Java 核心库 libcore 中打印 Log
2017-08-30
Android Java 核心库中是无法直接使用 android.util.Log 的,添加后编译不通过,因为 framework 中的 Java API 依赖于 Java 核心库。 在 Android 7.0 之前,Java 核心库源码在libcore/luni/下,luni 代表 lang、util、net、io,是 Java 中最常见的包;Android 7.0 中,核心库在libcore/ojluni/下,oj 代表 OpenJDK。 …
求两个有序数组的中位数 Median of Two Sorted Arrays
2017-07-24
这是 LeetCode 上的一道题,求两个有序数组的中位数: https://leetcode.com/problems/median-of-two-sorted-arrays/description/ Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. Follow up: The overall run time complexity should be O(log (m+n)). Example 1: Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2. Example 2: …
装饰器模式 Decorator Pattern
2017-06-22
在面向对象编程中,要扩展一个类或对象的功能,可以使用继承机制。 举例来说,咖啡店可能会提供拿铁、卡布奇诺、美式咖啡、意式浓缩等供客人选择,我们可以先定义一个 Coffee 类,并在其中声明价格、成分等方法,然后为每种咖啡继承它:Latte、Cappuccino、Americano、Espresso 等,每个类中都实现自己的一套价格、成分信息,这些类都是预先定义好的。 …