15118954158-II翻来翻去

Posted 保护眼睛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了15118954158-II翻来翻去相关的知识,希望对你有一定的参考价值。

151. 翻转字符串里的单词

class Solution151 {
    public static String reverseWords(String s) {
        String[] strings = s.split(" ");

        StringBuilder stringBuilder = new StringBuilder();
        int len = strings.length;
        for (int i = len - 1; i >= 0; i--) {
            if (!strings[i].trim().equals("")) {
                if (stringBuilder.length() != 0) {
                    stringBuilder.append(" ");
                }
                stringBuilder.append(strings[i]);
            }
        }

        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        System.out.println(reverseWords("a good   example"));
    }
}

189. 旋转数组

class Solution189 {
    public static void rotate(int[] nums, int k) {
        int len = nums.length;
        k = k % len;
        int[] tmpNums = new int[len * 2];
        for (int i = 0; i < len; i++) {
            tmpNums[i] = nums[i];
        }
        int j = 0;
        for (int i = len; i < len * 2; i++) {
            tmpNums[i] = nums[j++];
        }
        j = 0;
        for (int i = len - k; i < 2 * len - k; i++) {
            nums[j++] = tmpNums[i];
        }
    }

    public static void rotate2(int[] nums, int k) {
        int len = nums.length;
        k = k % len;

        reverse(nums, 0, len - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, len - 1);
    }

    public static void reverse(int[] nums, int start, int end) {
        while (start < end) {
            int tmp = nums[start];
            nums[start] = nums[end];
            nums[end] = tmp;
            start += 1;
            end -= 1;
        }
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5, 6, 7};

        rotate2(nums, 4);
        System.out.println(Arrays.toString(nums));
    }
}

541. 反转字符串 II

class Solution541 {
    public static void main(String[] args) {
        System.out.println(reverseStr("abcd", 2));
    }

    public static String reverseStr(String s, int k) {
        char[] chars = s.toCharArray();
        int n = s.length();
        for (int i = 0; i < n; i += 2 * k) {
            reverse(chars, i, Math.min(i + k, n) - 1);
        }
        return new String(chars);
    }

    public static void reverse(char[] chars, int start, int end) {
        while (start < end) {
            char tmp = chars[start];
            chars[start] = chars[end];
            chars[end] = tmp;
            start += 1;
            end -= 1;
        }
    }
}

剑指 Offer 58 - II. 左旋转字符串

class Solution58_II {
    public String reverseLeftWords(String s, int n) {
        char[] a = s.toCharArray();
        char[] b = new char[n];

        for (int i = 0; i < n; i++) {
            b[i] = a[i];
        }
        for (int i = 0; i < a.length - n; i++) {
            a[i] = a[n + i];
        }
        for (int i = 0; i < n; i++) {
            a[a.length + i - n] = b[i];
        }

        return new String(a);
    }

    public static String reverseLeftWords2(String s, int n) {
        int len = s.length();
        char[] chars = s.toCharArray();

        reverse(chars, 0, len - 1);
        reverse(chars, 0, len - n - 1);
        reverse(chars, len - n, len - 1);

        return new String(chars);
    }

    public static void reverse(char[] chars, int start, int end) {
        while (start < end) {
            char tmp = chars[start];
            chars[start] = chars[end];
            chars[end] = tmp;
            start += 1;
            end -= 1;
        }
    }

    public static void main(String[] args) {
        System.out.println(reverseLeftWords2("lrloseumgh", 6));
    }
}

以上是关于15118954158-II翻来翻去的主要内容,如果未能解决你的问题,请参考以下文章

echarts的使用过程

设置textField的placegolder的字体大小和字体颜色

表(Table)

ios 实现局域网远程开机的代码

栗染-喜欢安静的样子

android 引入其他类库导致的各种坑