如何洗牌string中的字符
如何在一个string中打乱字符(例如hello可以是ehlol或lleoh或…)。 我不想使用Collections.shuffle(...)
方法,有没有更简单的方法?
我不知道什么更简单。 但是你可以使用Math.rand()函数在字符长度范围内生成一个随机数而不需要replace,这会给你一个混洗的输出
public class Shuffle { public static void main(String[] args) { Shuffle s = new Shuffle(); s.shuffle("hello"); } public void shuffle(String input){ List<Character> characters = new ArrayList<Character>(); for(char c:input.toCharArray()){ characters.add(c); } StringBuilder output = new StringBuilder(input.length()); while(characters.size()!=0){ int randPicker = (int)(Math.random()*characters.size()); output.append(characters.remove(randPicker)); } System.out.println(output.toString()); } } /* Sample outputs hlleo llheo leohl lleho */
不是很好的performance,但在我看来很可读:
public static String shuffleString(String string) { List<String> letters = Arrays.asList(string.split("")); Collections.shuffle(letters); String shuffled = ""; for (String letter : letters) { shuffled += letter; } return shuffled; }
这个怎么样:
public static String shuffle(String text) { char[] characters = text.toCharArray(); for (int i = 0; i < characters.length; i++) { int randomIndex = (int)(Math.random() * characters.length); char temp = characters[i]; characters[i] = characters[randomIndex]; characters[randomIndex] = temp; } return new String(characters); }
class ShuffleString { public static String shuffle(String s) { String shuffledString = ""; while (s.length() != 0) { int index = (int) Math.floor(Math.random() * s.length()); char c = s.charAt(index); s = s.substring(0,index)+s.substring(index+1); shuffledString += c; } return shuffledString; } } public class foo{ static public void main(String[] args) { String test = "hallo"; test = ShuffleString.shuffle(test); System.out.println(test); } }
输出:ahlol
例如:
static String shuffle(String text){ if (text.length()<=1) return text; int split=text.length()/2; String temp1=shuffle(text.substring(0,split)); String temp2=shuffle(text.substring(split)); if (Math.random() > 0.5) return temp1 + temp2; else return temp2 + temp1; }
你可以遍历所有的字符,比较每一个与下一个。 然后,如果Math.rand()> 0.5交换这个字符与下一个,否则移动到下一个字符。
这里的代码既不需要recursion,也不需要转换为集合。
public static String shuffle(String string) { StringBuilder sb = new StringBuilder(string.length()); double rnd; for (char c: string.toCharArray()) { rnd = Math.random(); if (rnd < 0.34) sb.append(c); else if (rnd < 0.67) sb.insert(sb.length() / 2, c); else sb.insert(0, c); } return sb.toString(); }
不知道你为什么不想使用洗牌,除非是上学。 ;)
如果你关心性能,你绝对不能使用任何连接string与“+”的解决scheme。
这是我能想出的最紧凑的解决scheme:
public static String shuffle(String string) { if (StringUtils.isBlank(string) { return string; } final List<Character> randomChars = new ArrayList<>(); CollectionUtils.addAll(randomChars, ArrayUtils.toObject(string.toCharArray())); Collections.shuffle(randomChars); return StringUtils.join(randomChars, ""); }
String shuffled; do { shuffled = Stream.of(text.split("")).sorted((o1, o2) -> ThreadLocalRandom.current().nextInt(3) - 1).collect(Collectors.joining()); }while(shuffled.equals(text));
多烦人的问题。 我终于结束了这个:
import java.util.Collections; import com.google.common.primitives.Chars; import org.apache.commons.lang3.StringUtils; String shuffle(String s) { List<Character> chars = Chars.asList(s.toCharArray()); Collections.shuffle(chars); return StringUtils.join(chars.stream().toArray()); }
是的,两个图书馆:)
如果你以后还想恢复原始String
,请尝试如下所示:
public static class ShuffledString { private List<Integer> indices; private String string; public ShuffledString(List<Integer> indices, String string) { this.indices = indices; this.string = string; } public List<Integer> getIndices() { return indices; } public String getRegularString() { StringBuilder stringBuilder = new StringBuilder(); for (int stringIndex = 0; stringIndex < indices.size(); stringIndex++) { int characterIndex = indices.indexOf(stringIndex); stringBuilder.append(string.charAt(characterIndex)); } return stringBuilder.toString(); } } public static ShuffledString shuffle(String input) { List<Integer> indices = new ArrayList<>(); StringBuilder output = new StringBuilder(input.length()); while (indices.size() < input.length()) { int randomIndex; while (indices.contains(randomIndex = (int) (Math.random() * input.length()))) { } indices.add(randomIndex); output.append(input.charAt(randomIndex)); } return new ShuffledString(indices, output.toString()); }