Description Given a permutation which contains no repeated number, find its index in all the permutations of these numbers, which are ordered in lexicographical order. The index begins at 1. Example Given [1,2,4], return 1. 解题:至今仍然不理解这个“字典中的顺序”是什么意思,…
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 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…
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 Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list should be made by splicing together the nodes of the two lists and sorted in ascending order. Example Given 1->3->8->11->15->null,…
Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification What is Anagram? Two strings are anagram if they can be the same after change the order of characters. Example Given s = "abcd", t = "dcab&q…
Description Implement an algorithm to determine if a string has all unique characters. Example Given "abc", return true. Given "aab", return false. Challenge What if you can not use additional data structures? 解题:题目要求判断字符串是否有重复的元素,并且要求…
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…