由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请教 怎样存下这个string
相关主题
这两道leetcode题有更好的答案吗?谁能贴一下求nth permutation 和已知permutation 求rank的code
permuation sequence 超时大家能说说(leecode) Permutation Sequence这道题后的数学思路吗?
A Question from leetcode, 求标准解法,本人解的太笨袅今天才整明白Permutation的最优解!?
Exposed上一道string permutation的题谁能帮我写写这道题? print all permutations of a string
Given a string, find all its permutations without any repetition?T家电面面经并且不解为何被秒拒
Permutation leetcode-求问个G家面试题
请教leetcode上的count and sayString permunation question (CS)
问一个题目问一道题目
相关话题的讨论汇总
话题: result话题: sol话题: int话题: string
进入JobHunting版参与讨论
1 (共1页)
c********p
发帖数: 1969
1
leetcode permutation sequence
先谢谢各位大牛!
我知道这个问题有更好的解法,但我想知道:
我想把第k个permutation存在result中, 无论我result是否设为全局的,它都存不下。
当时读到的时候,有存进去,可是接着run之后就没有了。我要怎么保存它呢?
int count;
StringBuffer result = new StringBuffer();
public String getPermutation(int n, int k) {
// Start typing your Java solution below
// DO NOT write main() function
count = 0;
if(n == 0 || k == 0){
return "";
}

StringBuffer sol = new StringBuffer();
permute(n, k, 1, sol, result);
// System.out.println("pass is:" + result);
String ret = new String(result);
return ret;
}
public void permute(int n, int k, int level, StringBuffer sol,
StringBuffer result){
if(level == n + 1){
count++;
System.out.println(sol);
if(count == k){
result = new StringBuffer(sol);

}
System.out.println("safe result as: " + result);
}

for(int i = level; i <= n; i++){
sol.append(i);
permute(n, k, level + 1, sol, result);
sol.setLength(sol.length() - 1);
}

}
k********4
发帖数: 858
c********p
发帖数: 1969
3
谢谢,我总是在这里出错!

【在 k********4 的大作中提到】
: Java pass by value
: read this
: http://stackoverflow.com/questions/40480/is-java-pass-by-refere

k********4
发帖数: 858
4

Result设成返回值比较好。

【在 c********p 的大作中提到】
: 谢谢,我总是在这里出错!
c********p
发帖数: 1969
5
这样递归起来就不方便了:(

【在 k********4 的大作中提到】
:
: Result设成返回值比较好。

1 (共1页)
进入JobHunting版参与讨论
相关主题
问一道题目Given a string, find all its permutations without any repetition?
leetcode里, backtracking的time complexity怎么算,比如permutations这题目Permutation leetcode-
leetcode的count and say请教leetcode上的count and say
一道题:number of permutation是 for a total score问一个题目
这两道leetcode题有更好的答案吗?谁能贴一下求nth permutation 和已知permutation 求rank的code
permuation sequence 超时大家能说说(leecode) Permutation Sequence这道题后的数学思路吗?
A Question from leetcode, 求标准解法,本人解的太笨袅今天才整明白Permutation的最优解!?
Exposed上一道string permutation的题谁能帮我写写这道题? print all permutations of a string
相关话题的讨论汇总
话题: result话题: sol话题: int话题: string