2010-07-23 12 views
13

मैं स्ट्रिंग में वर्णों को कैसे घुमा सकता हूं (उदाहरण के लिए हैलो ehlol या lleoh या ... हो सकता है)। मैं Collections.shuffle(...) विधि का उपयोग नहीं करना चाहता, क्या कुछ आसान है?स्ट्रिंग में वर्णों को कैसे घुमाएं

+6

नहीं है चाहते हैं, या आपके व्याख्याता ने आपको नहीं बताया? –

+2

मुझे संदेह है कि आपके लिए उपयोग करने के लिए पहले से उपलब्ध कुछ भी आसान है ... (कम से कम इस मामले में) – npinti

+0

शफल – user339108

उत्तर

25

मुझे कुछ भी आसान नहीं पता है। लेकिन तुम Math.rand() कार्यक्षमता का उपयोग कर सकते हैं बिना चरित्र की लंबाई की सीमा के भीतर एक यादृच्छिक संख्या उत्पन्न करने के लिए बदल सकते हैं और है कि आप एक shuffled उत्पादन

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 
*/ 
0

आप से अधिक सभी पात्रों पुनरावृति सकता है देना होगा, प्रत्येक की तुलना अगले के साथ एक। फिर यदि Math.rand()> 0.5 इस चरित्र को अगले के साथ स्वैप करें, अन्यथा अगले चरित्र पर जाएं।

1

उदाहरण के लिए:

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; 
}  
3
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

10

नहीं शानदार प्रदर्शन है, लेकिन मेरी राय में काफी पठनीय:

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; 
} 
0

यहाँ कोड है कि न तो प्रत्यावर्तन की आवश्यकता है, और न ही एक संग्रह में कनवर्ट करना।

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(); 
} 
1

सुनिश्चित नहीं हैं कि तुम क्यों, फेरबदल का उपयोग करने के जब तक यह स्कूल के लिए है नहीं करना चाहते। ;)

और यदि आप प्रदर्शन से चिंतित हैं, तो आप निश्चित रूप से किसी भी समाधान का उपयोग नहीं कर सकते जो "+" के साथ तारों को जोड़ता है।

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, ""); 
} 
5

कैसे इस बारे में:

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); 
} 
0
 String shuffled; 
     do { 
      shuffled = Stream.of(text.split("")).sorted((o1, o2) -> ThreadLocalRandom.current().nextInt(3) - 1).collect(Collectors.joining()); 
     }while(shuffled.equals(text)); 
+0

का उपयोग नहीं करना चाहते हैं वास्तव में कूल करें, क्योंकि 'तुलनाकर्ता' को वापस जाना है तारों की एक दी गई जोड़ी के लिए एक ही परिणाम अन्यथा आपको 'सॉर्ट (...)' विधि के साथ कोई समस्या है और "तुलना विधि" अपने सामान्य अनुबंध का उल्लंघन करती है! " आंतरिक टिमॉर्ट से। –

0

क्या एक कष्टप्रद समस्या

यहां सबसे कॉम्पैक्ट समाधान मैं के साथ आ सकता है। मैं अंत में इस के साथ समाप्त हो गया:

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()); 
} 

हाँ, दो पुस्तकालयों :)

0

आप अभी भी मूल 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()); 
} 
संबंधित मुद्दे