• [LintCode] 空格替换


     1 class Solution {
     2 public:
     3     /**
     4      * @param string: An array of Char
     5      * @param length: The true length of the string
     6      * @return: The true length of new string
     7      */
     8     int replaceBlank(char string[], int length) {
     9         // Write your code here
    10         if (!length) return 0;
    11         int spaces = 0;
    12         for (int i = 0; string[i]; i++)
    13             if (string[i] == ' ')
    14                 spaces++;
    15         if (!spaces) return length;
    16         int newlen = length + spaces * 2;
    17         int pn = newlen - 1, p = length - 1;
    18         while (p >= 0 && pn >= 0) {
    19             if (string[p] == ' ') {
    20                 string[pn--] = '0';
    21                 string[pn--] = '2';
    22                 string[pn--] = '%';
    23                 p--;
    24             }
    25             else string[pn--] = string[p--];
    26         }
    27         return newlen;
    28     }
    29 };
  • 相关阅读:
    websocket简易demo
    innerHTML误区
    nuget离线
    vs2017js 方法注释
    Neo私钥到地址
    vant-ui rem问题
    鼠标悬停出菜单
    wangeditor视频
    display:grid
    Python中的循环
  • 原文地址:https://www.cnblogs.com/jcliBlogger/p/4609199.html
Copyright © 2020-2023  润新知