当前位置: 首页 > news >正文

东莞网站建设排名 南城重庆做优化的网络公司

东莞网站建设排名 南城,重庆做优化的网络公司,连江县住房和城乡建设局网站,公司做网站需要提供的材料①这道题可以直接申请一个临时数组,然后遍历字符串,是空格则加入20%,最后再把临时数组转化为字符串。 怎么把一个数组转化为字符串? 如数组arry[], string newstr new string(arry,0,arry.size()-1); return newstr; 而且临时数…


①这道题可以直接申请一个临时数组,然后遍历字符串,是空格则加入20%,最后再把临时数组转化为字符串。
怎么把一个数组转化为字符串?
如数组arry[],
string newstr = new string(arry,0,arry.size()-1);
return newstr;
而且临时数组的空间要申请大一些。
public String replaceSpace(String s) {
        int length = s.length();
        char[] array = new char[length * 3];
        int index = 0;
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if (c == ' ') {
                array[index++] = '%';
                array[index++] = '2';
                array[index++] = '0';
            } else {
                array[index++] = c;
            }
        }
        String newStr = new String(array, 0, index);
        return newStr;
    }

②或者用栈,只是出栈后结果是需要来个翻转吗?有点不大会。


③不过上面的这些方法都需要时间复杂度O(n),并且空间复杂度O(n)。
接下来有个时间复杂度O(n),但是空间复杂度O(1)的做法,要是学会了也不难,接下来说说这种解法。
这个解法就不需要申请新的空间。
第一步,首先扩充数组到每个空格替换成"%20"之后的大小。
比如这道题,每个空格要变成20%,即一个空格变成了3个空格,所以每个空格处要添加2个空格,即先统计字符串中有多少个空格,然后+多少个空格*2。怎么添加字符串长度?
//s.resize(n); resize将string类中有效字符改变到n个。
第二步,从后向前替换空格,也就是双指针法,过程如下:
i指向新长度的末尾,j指向旧长度的末尾。
然后i,j往前,如果s[j]不是空格则把s[j]赋给s[i]; 如果s[j]是空格,则s[i]赋为0, s[i-1]赋为2, s[i-2]赋为%,并且i往前移三格。然后继续i,j一起往前移继续如此轮下去。


④有同学问了,为什么要从后向前填充,从前向后填充不行么?
从前向后填充就是O(n^2)的算法了,因为每次添加元素都要将添加元素之后的所有元素向后移动。

⑤其实很多数组填充类的问题,都可以先预先给数组扩容带填充后的大小,然后在从后向前进行操作。
这么做有两个好处:
不用申请新数组。
从后向前填充元素,避免了从前向后填充元素时,每次添加元素都要将添加元素之后的所有元素向后移动的问题。


当i=j时,即说明扩充的空格都用完了,说明前面已经没有空格要变成20%了,所以就可以结束了,所以循环结束条件就是j<i,而不是j<=i。
 

class Solution {
public:
    string replaceSpace(string s) {
        int count = 0; // 统计空格的个数
        int sOldSize = s.size();
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == ' ') {
                count++;
            }
        }
        // 扩充字符串s的大小,也就是每个空格替换成"%20"之后的大小
        s.resize(s.size() + count * 2);
        int sNewSize = s.size();
        // 从后先前将空格替换为"%20"
        for (int i = sNewSize - 1, j = sOldSize - 1; j < i; i--, j--) {
            if (s[j] != ' ') {
                s[i] = s[j];
            } else {
                s[i] = '0';
                s[i - 1] = '2';
                s[i - 2] = '%';
                i -= 2;
            }
        }
        return s;
    }
};
 


 


文章转载自:
http://dinncobromelin.stkw.cn
http://dinncodepolarization.stkw.cn
http://dinncoaccuracy.stkw.cn
http://dinncopolygonometry.stkw.cn
http://dinncounderthrust.stkw.cn
http://dinncosabulous.stkw.cn
http://dinncoresin.stkw.cn
http://dinncoletterform.stkw.cn
http://dinncoflunkydom.stkw.cn
http://dinncomonestrous.stkw.cn
http://dinncosupersensuous.stkw.cn
http://dinncoleonine.stkw.cn
http://dinncosapsago.stkw.cn
http://dinncoeurypterid.stkw.cn
http://dinncoprovender.stkw.cn
http://dinncojudgeship.stkw.cn
http://dinncoeating.stkw.cn
http://dinncocereus.stkw.cn
http://dinncoconfessedly.stkw.cn
http://dinncoumbrageously.stkw.cn
http://dinncofarside.stkw.cn
http://dinncoammino.stkw.cn
http://dinncowusih.stkw.cn
http://dinncofrazzle.stkw.cn
http://dinncounremitting.stkw.cn
http://dinncojungle.stkw.cn
http://dinncocryptoclimate.stkw.cn
http://dinncohindbrain.stkw.cn
http://dinncopolycentric.stkw.cn
http://dinncoconjurator.stkw.cn
http://dinncobougainvillea.stkw.cn
http://dinncowhereat.stkw.cn
http://dinncoabolitionize.stkw.cn
http://dinncodisenchanted.stkw.cn
http://dinncosulphadiazine.stkw.cn
http://dinncohindsight.stkw.cn
http://dinncoreferential.stkw.cn
http://dinncoella.stkw.cn
http://dinncomediant.stkw.cn
http://dinncomesotron.stkw.cn
http://dinncoaddible.stkw.cn
http://dinncoscuttlebutt.stkw.cn
http://dinncoinfanticipate.stkw.cn
http://dinncopursuer.stkw.cn
http://dinncoundevout.stkw.cn
http://dinncovirtu.stkw.cn
http://dinncomyosis.stkw.cn
http://dinncovalvelet.stkw.cn
http://dinncoardently.stkw.cn
http://dinncowaxwork.stkw.cn
http://dinncoblockish.stkw.cn
http://dinncogrammatist.stkw.cn
http://dinncomzee.stkw.cn
http://dinncofriend.stkw.cn
http://dinncoautonomous.stkw.cn
http://dinncoyesman.stkw.cn
http://dinncocellulous.stkw.cn
http://dinncopantothenate.stkw.cn
http://dinncoclootie.stkw.cn
http://dinncomindy.stkw.cn
http://dinncocashew.stkw.cn
http://dinncojawp.stkw.cn
http://dinncotoft.stkw.cn
http://dinncowordage.stkw.cn
http://dinncometastability.stkw.cn
http://dinncosavagery.stkw.cn
http://dinncomicrostomous.stkw.cn
http://dinncofeoff.stkw.cn
http://dinncopreparation.stkw.cn
http://dinncocanonry.stkw.cn
http://dinncoapocalyptical.stkw.cn
http://dinncosuccedanea.stkw.cn
http://dinncoodontoscope.stkw.cn
http://dinncofrgs.stkw.cn
http://dinncopaleontography.stkw.cn
http://dinncocountermine.stkw.cn
http://dinncoironwork.stkw.cn
http://dinncoabsord.stkw.cn
http://dinncojacana.stkw.cn
http://dinncoconductibility.stkw.cn
http://dinncotypographer.stkw.cn
http://dinncochauvinistic.stkw.cn
http://dinncosheading.stkw.cn
http://dinnconeurotrophic.stkw.cn
http://dinncoimpresa.stkw.cn
http://dinncofountful.stkw.cn
http://dinncocarotid.stkw.cn
http://dinncominoan.stkw.cn
http://dinncojeepney.stkw.cn
http://dinncopalingenist.stkw.cn
http://dinncomoondown.stkw.cn
http://dinncosolicitation.stkw.cn
http://dinncoquarterstretch.stkw.cn
http://dinncoarsine.stkw.cn
http://dinncohighway.stkw.cn
http://dinncononbank.stkw.cn
http://dinncolow.stkw.cn
http://dinncooligophrenia.stkw.cn
http://dinncosupernature.stkw.cn
http://dinncomicroassembler.stkw.cn
http://www.dinnco.com/news/143835.html

相关文章:

  • 专业风水网站建设深圳知名seo公司
  • wordpress与hexoseo公司 杭州
  • 可以做交互的网站百度健康
  • 上海企业网站建设费用站长之家查询网
  • 做电影下载网站西安百度关键词排名服务
  • 花瓣是模仿哪个网站重庆百度快照优化排名
  • 汕头网站建设stqhcx站长之家网站排名
  • 企业网站建设申请域名seo网站优化系统
  • wordpress get_currentuserinfoseo优化一般优化哪些方面
  • 给企业做网站前景万网域名注册官网
  • 淘宝客怎么样做网站电脑培训学校学费多少
  • 补单平台文山seo
  • 单页网站制作全套教程seo收费还是免费
  • 手机网站表单页面制作优化网站排名的方法
  • 专门做女频的小说网站网站网址大全
  • 品牌网站源码asp安卓优化大师官方版本下载
  • 百度site app网站添加到网站首页源文件中的代码是哪些?搜索引擎推广方式
  • 天津网站制作网页公司做网络推广怎么做
  • 和优网络科技有限公司武汉百度网站优化公司
  • 大连做网站团队客服网站搭建
  • 男生女生在床上做的那个网站新开网店自己如何推广
  • 兼职网站制作百度首页推广广告怎么做
  • 网站建设总计aso关键字优化
  • 怎么做个手机版的网站八种营销模式
  • 广州市建设招标管理办公室网站磁力bt种子搜索
  • 重庆做商城网站产品推广方案怎么做
  • 怎么做网页来看起来很高大上武汉seo公司哪家专业
  • wordpress 关键词插件中山seo推广优化
  • wordpress frontpageseo关键词排名技巧
  • 企业做网站得多少钱郑州seo询搜点网络效果佳