Google面经(持续更新)

66. Plus One

Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.

You may assume the integer do not contain any leading zero, except the number 0 itself.

The digits are stored such that the most significant digit is at the head of the list.

Note: if digits = {9,9}, new integer = {1,0,0}. we just need to set the most significant digit in answer.

66. Plus One

128. 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.

解法一: HashMap

The key thing is to update boundary points of the sequence. For example, for sequence {1, 2, 3, 4, 5}, map.get(1) and map.get(5) should both return 5 while we don't care what map.get(3) may return because it will never be used by new element.

128. Longest Consecutive Sequence

解法二: HashSet

add integer to a set and remove it, complexity O(2n)

set can store element that hasn't been visited

128. Longest Consecutive Sequence

361. Bomb Enemy

解法一:

需要一个rowCnt变量,用来记录到下一个墙之前的敌人个数。还需要一个数组colCnt,其中colCnt[j]表示第j列到下一个墙之前的敌人个数。算法思路是遍历整个数组grid,对于一个位置grid[i][j],对于水平方向,如果当前位置是开头一个或者前面一个是墙壁,我们开始从当前位置往后遍历,遍历到末尾或者墙的位置停止,计算敌人个数。对于竖直方向也是同样,如果当前位置是开头一个或者上面一个是墙壁,我们开始从当前位置向下遍历,遍历到末尾或者墙的位置停止,计算敌人个数。有了水平方向和竖直方向敌人的个数,那么如果当前位置是0,表示可以放炸弹,我们更新结果ans即可,复杂度O(mn)参见代码如下:


361. Bomb Enemy
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容