不申请额外空间调整字符串大小写顺序,大写字母依次放到后面

一道很经典的字符串算法题:
把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,且不能申请额外的空间。
例如
AkleBiCeilD, 调整后应该是 kleieilABCD

第一次做的时候写的是类似冒泡的两两交换,时间复杂度是O(n*n),明显当n达到万级的时候就会TLE,是很笨的方法。而且声明了 char t;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
const int maxn = 1005;

int main()
{  
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s;
    while(cin>>s)
    {
        int len = s.length();
        int pos = len-1;
        for(int i=len-1;i>=0;--i){
            if(s[i]>='a' && s[i]<='z'){//找最后一个小写字母的位置
                pos = i;
                break;
            }
        }

        for(int i = len-1;i>0;--i){
            if(s[i]>='a' && s[i]<='z' && s[i-1]>='A' && s[i-1]<='Z')
            {    //出现了类似 Aa 这种大写字母在前面的情况
                char t;
                for(int j=i-1;j<pos;++j)
                {     //i-1到pos 位置循环两两交换,把大写字母放到后面 
                    t = s[j];
                    s[j]=s[j+1];
                    s[j+1]=t;
                }  
                pos--; //pos位置前移,后面是移好的大写字母
            }
        }
        cout<<s<<endl;             
    }
    return 0;
}

(事实上,如果你要交换两个值,而不用到第三个值的时候,你可以这样写

a^=b;
b^=a;
a^=b;

利用的原理是
是一个整数与另外一个数进行两次异或运算仍然是其本身,而且与0异或也是其本身
基本原理用式子表达如下:右边数字表示状态
(1) A ^ A = 0;
(2) A1 = A0 ^B0;
(3) B1 = A1 ^B0 = (A0 ^ B0) ^ B0 = A0 ^ (B0 ^ B0) = A0 ^ 0 = A0;
(4) A2 = A1 ^ B1 = B1 ^ A1 = A0 ^ (A0 ^B0) = 0^B0=B0;

实际上,若果只是oj测评,不想Leetcode那样需要返回数组检查,直接输出2次就可以了,很强的方法。。。

#include <iostream>
#include <string.h>
using namespace std;
int main(){
    string s;
    while(cin >> s){
            for(int i = 0; i < s.length(); i++)
                if(s[i] >= 'a' && s[i] <= 'z')
                    cout << s[i];
            for(int i = 0; i < s.length(); i++)
                if(s[i] <= 'Z' && s[i] >= 'A')
                    cout << s[i];
            cout << endl;
    }
    return 0;
}

再耍流氓一点,调用STL 的 stable_partition 也是可以的。但是笔试就不行


#include <iostream>

#include <algorithm>

using namespace std;


bool isLower(char c) {

    return c>='a';

}



int main() {

    string s;

    while(cin >> s) {

        stable_partition(s.begin(),s.end(),isLower);

        cout << s << endl;

    }

}

既然说到了stable_partition,顺便说一下和 partition 的区别

bool Fun(char c)  
{  
    return c=='*';  
}  
int main()  
{  
    string str = "***b**a**c**d**";  
    string str1(str);  
    string str2(str);  

    std::partition(std::begin(str1), std::end(str1),Fun);  

    std::stable_partition(std::begin(str2), std::end(str2),Fun);  

    cout<<"str1="<<str1.c_str()<<endl;  
    cout<<"str2="<<str2.c_str()<<endl;  
    return 0;  
}  

运行结构是
str1=***********cdab
str2=***********bacd

从运行结果上看,partition函数把所有的都提前了,但是其他字母的顺序是打乱的,而stable_partition函数除了把号提前,其他字母的顺序和原来是一样的

Fun函数判断传入的char是不是*,如果是,就返回true

stable_partition 函数,前两个参数规定了排序的范围,最后一个参数要传入一个函数名,程序会遍历规定范围内的元素并传入Fun函数,根据返回值决定这个元素是往前放还是往后放

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值