美文网首页常用算法
2019-08-04-字符串操作

2019-08-04-字符串操作

作者: 王元 | 来源:发表于2019-08-06 12:26 被阅读0次

一,题目:替换字符串中的空格

  • 替换空格为20%
  • 1,先根据字符串的长度获取空格的个数,
  • 2,根据空格个数确定替换之后字符串的长度

实现方式1:

public static String replaceBlack(String s) {
    final int len = s == null ? 0 : s.length();
    if(len <= 0) {
        return s;
    }
    int blackNum = 0;
    char[] arr = s.toCharArray();
    for(int  i = 0; i < len; i++) {
        if(arr[i] == ' ') {
            blackNum++;
        }
    }
    int replaceNum = len + blackNum * 2;
    int originLen = len;
    char[] replaceArr = new char[replaceNum];
    while(originLen > 0) {
        --originLen;
        if(arr[originLen] == ' ') {
            replaceArr[--replaceNum] = '0';
            replaceArr[--replaceNum] = '2';
            replaceArr[--replaceNum] = '%';
            
        } else {
            
            replaceArr[--replaceNum] = arr[originLen];
        }
        
    }
        
    return new String(replaceArr);
}

代码实现方式2

public static String replaceSpace(String str) {
    final int len = str == null ? 0 : str.length();
    int spaceNum = 0;
    for (int i = 0; i < len; i++) {
        char c = str.charAt(i);
        if(c == ' ') {
            spaceNum++;
        }
    }
    if(spaceNum <= 0) {
        return str;
    }

    int newLen = len + spaceNum * 2;
    char[] chars = new char[newLen];

    int newIndex = newLen - 1;
    int oldIndex = len - 1;

    while (oldIndex >= 0) {
        char c = str.charAt(oldIndex);
        if(c == ' ') {
            chars[newIndex--] = '0';
            chars[newIndex--] = '2';
            chars[newIndex--] = '%';
        } else {
            chars[newIndex--] = c;
        }
        oldIndex--;

    }
    return new String(chars);
}

二,反转字符串

1,代码实现,按照空格调整顺序

private static void reverse() {
    String[] arr = VALUE.split(" ");
    final int len = arr == null ? 0 : arr.length;
    StringBuilder sb = new StringBuilder();
    for(int i = len -1; i >=0; i--) {
        if(i < len -1) {
            sb.append(" ");
        }
        sb.append(arr[i]);
    }
    System.out.println(sb.toString());
}
    
public static void reverse1() {
    String[] arr = VALUE.split(" ");
    final int len = arr == null ? 0 : arr.length;
    int start = 0;
    int end = len - 1;
    
    while(start < end) {
        String temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
    for(String s : arr) {
        System.out.println(s + " ");
    }
}

2,转为char,然后使用异或来实现,待验证,

public static void reverse2() {
    char[] arr = VALUE.toCharArray();
    final int len = arr == null ? 0 : arr.length;
    int start = 0;
    int end = len - 1;
    
    while(start < end) {
        arr[start] = (char)(arr[start] ^ arr[end]);
        arr[end] = (char)(arr[start] ^ arr[end]);
        arr[start] = (char)(arr[end] ^ arr[start]);
        start++;
        end--;
    }
    for(char s : arr) {
        System.out.print(s);
    }
}

相关文章

  • 2019-08-04-字符串操作

    一,题目:替换字符串中的空格 替换空格为20% 1,先根据字符串的长度获取空格的个数, 2,根据空格个数确定替换之...

  • Python字符串高端操作

    字符串骚操作 字符串优雅操作

  • Python初学(十)

    这章学习下字符串的操作。 字符串的操作 字符串操作符: 针对字符串,Python语言提供了几个基本操作符 字符串处...

  • python 字符串

    字符串操作 + 字符串连接操作 * 字符串复制操作 [] 字符串索引 通过索引访问指定位置的字符,索引从头(0)...

  • Python基础-day06

    list ​ 字符串操作 ​ 字典操作 ​ list操作 字符串操作 编码解码 计算机存储数据使用的是...

  • python函数知识归纳笔记(2)

    字符串相关操作 字符串连接 字符串赋值 [索引值] 字符串通过索引访问位置 从0开始 [::] 字符串取片操作,实...

  • C++之string

    字符串构造和赋值操作 实例 存取字符 实例 字符串拼接操作 实例 字符串查找和替换 实例 字符串比较 实例 字符串...

  • 05-字符串的操作

    字符串的操作方法 [] 字符串索引操作,通过索引访问指定位置的字符,索引从0开始 [::] 字符串取片操作完整格式...

  • Python常用语法二

    Python 字符串操作和文件操作以及其它Python能力补充 Python字符串操作 in和not in: 'x...

  • 【前端】常用的JS知识点整理

    操作数组 操作字符串

网友评论

    本文标题:2019-08-04-字符串操作

    本文链接:https://www.haomeiwen.com/subject/yipudctx.html