Redian新闻
>
谁能帮我写写这道题? print all permutations of a string
avatar
谁能帮我写写这道题? print all permutations of a string# JobHunting - 待字闺中
l*n
1
网上买了个东西,现在不想要了,明天UPS
schedule了delivery,我明天不在家,担心
UPS直接仍在门口了事,能现在打电话refuse吗?
avatar
n*r
2
实在太落后了。看了解法还是不会写。
Design an algorithm to print all permutations of a string. For simplicity,
assume all characters are unique.
Test String: abcdefg
Case “a” --> {a}
Case “ab” --> {ab, ba}
Case “abc” --> ?
This is the first “interesting” case. If we had the answer to P(“ab”),
how could we generate P(“abc”). Well, the additional letter is “c”, so
we can just stick c in at every possible point. That is:
merge(c, ab) --> cab, acb, abc
merge(c, ba) --> cba, bca, bac
Algorithm: Use a recursive algorithm. Generate all permutations of a string
by “chopping off” the last character and generating all permutations of s[
1… n-1]. Then, insert s[n] into every location of the string.
avatar
z*q
3
能更猥琐一点吗
avatar
h*i
4
The simplest method is by recursion: if you have k letters, then print the
first letter, call the function on the remaining (k-1) letters;
then second letter, and so on.
avatar
f*0
5
在门上留条说refuse那个包裹 签名
我上次是这样做的

【在 l***n 的大作中提到】
: 网上买了个东西,现在不想要了,明天UPS
: schedule了delivery,我明天不在家,担心
: UPS直接仍在门口了事,能现在打电话refuse吗?

avatar
x*6
6
经典算法,直接背下来吧,10行左右。
avatar
L*3
7
好像ups driver要当面问一下的
我上次就是门上留条:拒收dell的,收newegg的
结果driver门都没敲,直接把newegg的drop 到office了。第二天他带着dell的包裹又来了,问我LD说
no want?得到yes后才送回dell
avatar
t*i
8
public static void permutation(String prefix, String str) {
int n = str.length();
if (n == 0)
System.out.println(prefix);
else {
for (int i = 0; i < n; i++) {
permutation(prefix + str.charAt(i), str.substring(0, i)
+ str.substring(i + 1, n));
}
}
}
avatar
f*0
9
你LD真说的yes?

又来了,问
我LD说

【在 L*3 的大作中提到】
: 好像ups driver要当面问一下的
: 我上次就是门上留条:拒收dell的,收newegg的
: 结果driver门都没敲,直接把newegg的drop 到office了。第二天他带着dell的包裹又来了,问我LD说
: no want?得到yes后才送回dell

avatar
x*a
10
is it ok to use the stl?
avatar
p*2
11
我写了一个练练
val str="abc"
str.permutations.foreach(println)
output:
abc
acb
bac
bca
cab
cba
avatar
l*a
12
whether null is supported in this language?
whether null.permutation is legal?

【在 p*****2 的大作中提到】
: 我写了一个练练
: val str="abc"
: str.permutations.foreach(println)
: output:
: abc
: acb
: bac
: bca
: cab
: cba

avatar
p*2
13

obviously. you have no idea about scala. null is java thing

【在 l*****a 的大作中提到】
: whether null is supported in this language?
: whether null.permutation is legal?

avatar
l*a
14
anything can match null in scala?

【在 p*****2 的大作中提到】
:
: obviously. you have no idea about scala. null is java thing

avatar
n*r
15
有落后了
what is scala:
Scala is a general purpose programming language designed to express common
programming patterns in a concise, elegant, and type-safe way. It smoothly
integrates features of object-oriented and functional languages, enabling
Java and other programmers to be more productive. Code sizes are typically
reduced by a factor of two to three when compared to an equivalent Java
application.
avatar
d*x
16
四月一起学这个吧!
https://www.coursera.org/course/progfun

【在 n******r 的大作中提到】
: 有落后了
: what is scala:
: Scala is a general purpose programming language designed to express common
: programming patterns in a concise, elegant, and type-safe way. It smoothly
: integrates features of object-oriented and functional languages, enabling
: Java and other programmers to be more productive. Code sizes are typically
: reduced by a factor of two to three when compared to an equivalent Java
: application.

avatar
n*r
17

思路好像跟开始不大一样,不过试了abc例子好像work的
str.length最小为1吧?
该了2行
if (n == 0) 改成 if (n == 1)
permutation(prefix + str.charAt(i), str.substring(0, i)
+ str.substring(i + 1, n)); 中的str.substring(0, i)改成str.substring(0, i
-1)
不知对不对
以abc为例
permutation(null, abc)
--> P(a,bc),P(b,ac),P(c,ab)
-->P(a,P(b,c)),P(a,P(c,b)),P(b,P(a,c)),P(a,P(c,a)),P(c,P(a,b)),P(c,P(b,a))

【在 t******i 的大作中提到】
: public static void permutation(String prefix, String str) {
: int n = str.length();
: if (n == 0)
: System.out.println(prefix);
: else {
: for (int i = 0; i < n; i++) {
: permutation(prefix + str.charAt(i), str.substring(0, i)
: + str.substring(i + 1, n));
: }
: }

avatar
n*r
18
就是觉得还绕啊,写写就左右为难。

【在 x*******6 的大作中提到】
: 经典算法,直接背下来吧,10行左右。
avatar
d*x
20
Orz
我不是大牛啊,最近在反思,感觉弱爆了。。
我打算学,不过不急,到时候跟课程了

【在 p*****2 的大作中提到】
:
: 大牛准备要学吗?

avatar
p*2
21

我也准备跟课程了。对FP的性能心里没底。现在还是不敢搞纯的。也许是水平太差了。
不过听说scala的library里边都是imperative的。我最近也觉得报弱。可是比你差远的
,还没有好好反思。

【在 d**********x 的大作中提到】
: Orz
: 我不是大牛啊,最近在反思,感觉弱爆了。。
: 我打算学,不过不急,到时候跟课程了

avatar
h*a
22
Knuth有个很牛的算法,还可以handle重复字符。大概30行。

【在 n******r 的大作中提到】
: 实在太落后了。看了解法还是不会写。
: Design an algorithm to print all permutations of a string. For simplicity,
: assume all characters are unique.
: Test String: abcdefg
: Case “a” --> {a}
: Case “ab” --> {ab, ba}
: Case “abc” --> ?
: This is the first “interesting” case. If we had the answer to P(“ab”),
: how could we generate P(“abc”). Well, the additional letter is “c”, so
: we can just stick c in at every possible point. That is:

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。