Saturday, June 22, 2013

Permutation Sequence@leetcode

刷题必备书籍Cracking the Coding Interview: 150 Programming Questions and Solutions 

简历:The Google Resume: How to Prepare for a Career and Land a Job at Apple, Microsoft, Google, or any Top Tech Company
算法学习书籍:Introduction to Algorithms
编程珠玑:Programming Pearls (2nd Edition)
C++ 学习:The C++ Programming Language, 4th Edition
经典操作系统书籍,龙书:Operating System Concepts
创业:The Start-up of You: Adapt to the Future, Invest in Yourself, and Transform Your Career
The set [1,2,3,…,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"
  7. Given n and k, return the kth permutation sequence.
    Note: Given n will be between 1 and 9 inclusive.
    » Solve this problem
    小小感叹一下,最近老是犯困,刷题太慢,智商又捉急,那群一周刷完leetcode的娃们佩服毅力,总是刷一会儿玩儿一会儿,真想踹自己。
   嗯好吧,第一个按照之前permutation的思路,老解法递归去做,时间超了,显然就不行了。和之前permutation不同的是,它不需要把所有的permutation都输出,所以只需要找到这个东东排序的规律就好啦。所以直接loop搞起。

后一种做法,数学找规律,就是按照k和n来计算每一位是哪个数字,思路不难,就是中间细节处理麻烦。比如第一个数字是 k/(n-1)!, 然后再同样去考虑后面n-1个数。


No comments:

Post a Comment

Leetcode 316. Remove Duplicate Letters

 这道题表面问的是如何删除重复,实际在问如何从多个字符选取一个保留,从而让整个字符串按升序排列。那么策略就是对于高顺位的字符比如‘a',就要选靠前位置的保留,而低顺位字符如’z'则应该尽量选取靠后位置保留。 算法大概思路:每看到一个字符,我们要决定是否保留 1. ...