source of the question: http://seanzhou.iteye.com/blog/2032981
Question 1 Character-wise Shift For String
Implement an algorithm that can do character-wise shift for strings in either direction. For example,
To shift string "abcdef" to the left by 2 characters, you get "cdefab".
To shift string "abcdef" to the right by 3 characters, you get "defabc"
Performance requirement: assume the length of the string is n, the algorithm should finish in O(n) time and within O(1) space.
My Thoughts
1. Start from the first character, find its target position, put it there. For the character originally in target position of the first character, iteratively find its target positon and put it there again ... and so on.
2. We only need to record how many characters have been put in its correct position.
3. It's possible that n shares some common factor with m, so that after some iteration, you may go back to the first character. So you need to record where did you start. After you go back to starting point, you should go right one step if not all of the characters have found its correct position.
Implementation in Java
package q1; /** * Question 1 Character-wise Shift For String * * Implement an algorithm that can do character-wise shift for strings in either direction. For example, * * To shift string "abcdef" to the left by 2 characters, you get "cdefab". * To shift string "abcdef" to the right by 3 characters, you get "defabc" * * Performance requirement: assume the length of the string is n, the algorithm should finish in O(n) time and within O(1) space. * @author Sean * */ public class StringShift { public static void main(String[] args) { //m & n has no common factors assert "DEFGABC".equals(shift("ABCDEFG", 3, true)); assert "EFGABCD".equals(shift("ABCDEFG", 3, false)); //n is multiple of m assert "EFGHABCD".equals(shift("ABCDEFGH", 4, true)); assert "EFGHABCD".equals(shift("ABCDEFGH", 4, false)); //n and m shares a common factor assert "GHIABCDEF".equals(shift("ABCDEFGHI", 6, true)); assert "DEFGHIABC".equals(shift("ABCDEFGHI", 6, false)); } private static String shift(String origin, int m, boolean left) { // input check if ( origin == null ) return null; int len = origin.length(); if (len == 0 ) return origin; m %= len; if ( m == 0 ) return origin; //get the char array from the String char[] arr = new char[len]; origin.getChars(0, len, arr, 0); //shift left or right? int direction = left ? -1 : 1; //record how many characters have been put in its correct position int found = 0; //record the starting position of the character which we are looing for its target position int start = 0; //record the current position of the character which we are looking for its target position int current = 0; char c = arr[current]; char temp; // do the shift from the beginning character while (found < len) { // find the target position , plus n is to avoid negative current = (direction * m + current + len) % len; // swap the source and target temp = c; c = arr[current]; arr[current] = temp; ++found; // check whether the iteration go back to the starting point if ( current == start) { current = start = ( start + 1) % len; c = arr[current]; } } return new String(arr); } }
相关推荐
Rec. ITU-R BT.709-3 1 RECOMMENDATION ITU-R BT.709-3 ...PARAMETER VALUES FOR THE HDTV STANDARDS FOR PRODUCTION AND INTERNATIONAL PROGRAMME EXCHANGE (Question ITU-R 27/11) (1990-1994-1995-1998)
Rec. ITU-R BT.709-1 1 RECOMMENDATION ITU-R BT.709-1 BASIC PARAMETER VALUES FOR THE HDTV STANDARD FOR THE STUDIO AND FOR INTERNATIONAL PROGRAMME EXCHANGE (Question ITU-R 27/11) (1990-1994)
17. question - 提问 18. meaning - 意义 19. discuss - 讨论 20. promise - 承诺 21. beginning - 开始 22. improve - 改进 23. physical - 身体的 24. themselves - 他们自己 25. hobby - 业余爱好 26. weekly - ...
- Asking about the long jumper and including Julia in the question. 4. “加油,彼得!”莉莉在喊。- Encouragement from Lily to Peter. 5. 高老师为彼得感到非常骄傲。- Miss Gao's pride in Peter's ...
Comma , --..-- Period .-.-.- Question Mark ? ..--.. Dash --..-- Slash -..-. Underscore ..--.. Space (一个长信号) 在"translate"这个压缩包文件中,可能包含了一个程序或者工具,用于将输入的英文字符或符号...
20. question - 问题;难题;询问;疑问(noun) 21. answer - 问答;答复;答案(noun) 22. look - 看;望;看起来(verb) 23. first - 第一(number) 24. first name - 名字 25. last - 最后的;上一个的...
29. question - 问题 30. spread - 传播 31. among - 在...之间 32. examine - 检查 33. patient - 病人 34. herself - 她自己 35. themselves - 他们自己 36. answer - 回答 37. duty - 责任 38. save - 拯救 二、...
Java练习题Question1.txtJava练习题Question1.txtJava练习题Question1.txtJava练习题Question1.txtJava练习题Question1.txtJava练习题Question1.txtJava练习题Question1.txtJava练习题Question1.txtJava练习题...
python练习题Question1.txtpython练习题Question1.txtpython练习题Question1.txtpython练习题Question1.txtpython练习题Question1.txtpython练习题Question1.txtpython练习题Question1.txtpython练习题Question1.txt...
#### QUESTION 1 - **问题描述:** Daniel是组织内部电子商务网站开发项目的项目经理。他拥有强制权,并指定团队成员Julie负责主持团队会议。在会议中,Julie应该做什么? - **选项:** A. 影响团队成员支持项目...
1. **看**: - `look`:强调看的动作,如"Look! There's a bird in the sky." - `see`:强调看到的结果,如"I saw him leave the building." - `watch`:通常用于观察持续的或有变化的事物,如"watch a movie"或...
d-freebase-mids/ has Freebase mids for each concept in each question, based on YodaQA entity linking results from d-dump. d-freebase-rp/ has extra custom-computed Freebase relation paths. d-freebase-...
Trust the best selling Official Cert Guide series from Cisco Press to help you learn, prepare, and practice for exam success. They are built with the objective of providing assessment, review, and ...
1. 主语从句:在句子中,主语从句是作为一个句子来用,充当主句的主语。例如:"Whether she will come or not" 在 "It doesn't matter whether she will come or not." 这句话中,"Whether she will come or not" ...
Rice / Coursera算法思维课程的文件第1-2周project1.py-项目1代码application1.py-应用程序1项目的代码dpa_algorithm.py-问题4的DPA算法的实现dpa_trial.py-为dpa算法提供的类application1_question1.png-应用程序1...
The benefits from tumbling two-legged mechatronic creatures and multiprocessor-based question-and-answer games seem hard to discover for noninvolved persons. In general the benefits from fundamental ...
1. accept - 接受:用于表示同意接受某物或某人。 例句:She accepted his invitation to the party. 2. achieve - 实现:达成目标或目的。 例句:With hard work, he achieved his dream of becoming a doctor. 3...
11. 问题 - question 12. 电影 - movie 13. 比赛 - match 14. 中心 - center 15. 超市 - supermarket 16. 天啊! - Oh my God! 17. 你愿意……吗? - Would you like to... 二、重要短语: 1. plan to do - 计划去...
1. "Its up to you." - 这句话意味着决定权在于对方,让对方决定接下来的行动。 2. "I envy you." - 表达对别人某些优点或经历的羡慕之情。 3. "How can I get in touch with you?" - 询问对方的联系方式,以便以后...