Description Using O(1) time to check whether an integer n is a power of 2. Example For n=4, return true; For n=5, return false; Challenge O(1) time 题解:题目要求判断一个数是不是2的幂次方,并且规定时间复杂度为线性级.那么,就不能用常规的循环求解思路去解题了,之前做过一个不用加号求两数之和的题目.这两个题目有点类似,可以通过位运算来快速求解.先考虑这…
142. O(1) Check Power of 2[easy] Using O(1) time to check whether an integer n is a power of 2. Have you met this question in a real interview? Yes Example For n=4, return true; For n=5, return false; Challenge O(1) time 解法一: class Solution { public:…
Description Given a collection of intervals, merge all overlapping intervals. Example Given intervals => merged intervals: [ [ (1, 3), (1, 6), (2, 6), => (8, 10), (8, 10), (15, 18) (15, 18) ] ] Challenge O(n log n) time and O(1) extra space. 题意:给定一个…
Description Write a method to replace all spaces in a string with %20. The string is given in a characters array, you can assume it has enough space for replacement and you are given the true length of the string. You code should also return the new…
Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. There may exist multiple valid solutions, return any of them. Example Given [1,2,3,4,5,6,7], return 4 / \ 2 6 / \ / \ 1 3 5 7 解题:题目要求根据一个有序的数…
Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, return 0->1->2->3->null. 解题:用插入法排序链表.很简单的一道题目,但还是出现了很多问题. 总结一下遇到的问题: (1)没有头结点的情况下,为了方便可以构造一个,返回头结点的next就行了. (2)没有必要一直在原来的链表上纠结,完全可以申请一个头结点,将原链表结点一…
Description Given an array and a value, remove all occurrences of that value in place and return the new length. The order of elements can be changed, and the elements after the new length don't matter. Example Given an array [0,4,4,0,0,2,4,4], value…
Description Given a non-overlapping interval list which is sorted by start point. Insert a new interval into it, make sure the list is still in order and non-overlapping (merge intervals if necessary). Example Insert (2, 5) into [(1,2), (5,9)], we ge…
Description Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Example Given a binary tree as follow: 1 / \ 2 3 / \ 4 5 The minimum depth is …
Description Given two strings, write a method to decide if one is a permutation of the other. Example abcd is a permutation of bcad, but abbe is not a permutation of abe 解题:遇到过类似的题目,比较简单的方法是,把字符串转化为字符数组,然后排序.比较每一位的数是否相等.这样做效率比较低,代码如下: public class So…