zoukankan      html  css  js  c++  java
  • LintCode 8. 旋转字符串

    import org.junit.Test;
    
    import java.util.Arrays;
    
    public class RotateString {
        /**
         * @param str: An array of char
         * @param offset: An integer
         * @return: nothing
         */
    
        /**
         * 8. 旋转字符串
         * 给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转)
         * <p>
         * 样例
         * 对于字符串 "abcdefg". 6
         * <p>
         * offset=0 => "abcdefg"
         * offset=1 => "gabcdef"
         * offset=2 => "fgabcde"
         * offset=3 => "efgabcd"
         * <p>
         * 这个鸟题目测试用例中offset可能大于数组的长度,所以如果offset的长度过大则取余数
         */
        public void rotateString(char[] str, int offset) {
            // write your code here
            if (str.length == 0) {
                offset = 0;
            } else {
                offset = offset % str.length;
            }
            String s1 = "";
            String s2 = "";
            for (int i = 0; i < str.length - offset; i++) {
                s1 += str[i];
            }
            for (int i = str.length - offset; i < str.length; i++) {
                s2 += str[i];
            }
            char[] ch = (s2 + s1).toCharArray();
            for (int i = 0; i < str.length; i++) {
                str[i] = ch[i];
            }
        }
    
        @Test
        public void testDigitCounts() {
            char ch[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
            rotateString(ch, 2);
            System.out.println(Arrays.toString(ch));
        }
    }
    
  • 相关阅读:
    顶点与片段着色器的例子
    Unity cg vertex and fragment shaders(二)
    Unity cg vertex and fragment shaders(一)
    C#线程(一)
    shell脚本变量定义注意别跟系统变量重名了……
    VLC编译问题
    VIM技巧:翻页
    Linux命令:cd
    Linux设置:环境变量
    VIM技巧:显示行号
  • 原文地址:https://www.cnblogs.com/wei1/p/9582060.html
Copyright © 2011-2022 走看看