zoukankan      html  css  js  c++  java
  • 将字符串中的*移到串的前部分

    题目:编码完成处理函数:函数将字符串中的字符'*'移到串的前面,前面的非'*'字符后移,但不能改变非'*'字符的先后顺序,函数返回串中字符'*'的数量。

    举例:原始串:ab**cd**e*12,处理后为*****abcde12,函数返回值为5。

    要求:使用尽量少的时间和辅助空间。

    答:

    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    //将字符串中的*移到串的前部分
    int RemoveStarToFront(char *str)
    {
        if (NULL == str)
        {
            return 0 ;
        }
        int len = 0;
        int count = 0;
        char *p = str;
        while (*p != '\0')
        {
            if (*p != '*')
            {
                len++;
            }
            else if (*p == '*')
            {
                count++;
                for (int j = 0; len > j; j++)
                {
                    *(p - j) = *(p - j -1);
                }
                *(p - len) = '*';
            }
            p++;
        }
        return count;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        char str[] = "ab**cd**e*12";
        cout<<RemoveStarToFront(str)<<"  "<<str;
        cout<<endl;
        return 0;
    }

    运行界面如下:

  • 相关阅读:
    python读写hdf5及cdf格式文件
    常用python shell
    opencv的使用——经典大坑
    opencv python实用操作
    opencv python基本操作
    opencv c++实用操作
    opencv c++基本操作
    opencv安装
    [HNOI2005]星际贸易
    [2017SEERC]Divide and Conquer
  • 原文地址:https://www.cnblogs.com/venow/p/2660186.html
Copyright © 2011-2022 走看看