![]() ![]() Forming a string just to dump the entry to a stream involves unnecessary allocation and copying. Use an array of characters and write the characters to the stream one-by-one when it's time to emit a particular permutation. It's the rare case that you actually have to write the letters out to the destination stream, so bias your solution against that operation. String is a poor choice, given that it's immutable, when generating permutations is best done by swapping and rotating elements in a vector (really, a ring). Note too that most of the solutions posted here use type String and keep chopping it up and reassembling it. For example, to type A, you would press the keypad and type 1, and then press the space bar. To type a letter other than a number, you need to use the keypad to type the letter, and then press the space bar. To type a number, you need to use the numbers 1 through 9. ![]() Try writing it out on paper, recording the steps you take to shuffle the elements, and you'll find you've written the program you need. To type a letter in a phone number, you need to use the keypad on your phone. You can iterate down the sequence, swapping the chosen element with the last element, reversing the sequence, expanding the bottom 24 permutations each time.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |