博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LintCode] 空格替换
阅读量:4646 次
发布时间:2019-06-09

本文共 952 字,大约阅读时间需要 3 分钟。

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 here10         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 };

 

转载于:https://www.cnblogs.com/jcliBlogger/p/4609199.html

你可能感兴趣的文章
儿子和女儿——解释器和编译器的区别与联系
查看>>
第一阶段冲刺3
查看>>
父类引用指向子类对象
查看>>
网页如何实现下载功能
查看>>
IT男专用表白程序
查看>>
读《大道至简》第六章感想
查看>>
ef linq 中判断实体中是否包含某集合
查看>>
章三 链表
查看>>
Solution for Concurrent number of AOS' for this application exceeds the licensed number
查看>>
CSE 3100 Systems Programming
查看>>
IntelliJ IDEA 的Project structure说明
查看>>
Java Security(JCE基本概念)
查看>>
Linux Supervisor的安装与使用入门
查看>>
创建 PSO
查看>>
JasperReport报表设计4
查看>>
项目活动定义 概述
查看>>
团队冲刺04
查看>>
我的Python分析成长之路8
查看>>
泛型在三层中的应用
查看>>
SharePoint2010 -- 管理配置文件同步
查看>>