替换字符--《剑指offer》
2017-01-14 20:27
169 查看
题目描述:给定一个字符数组,将其中的某一字符替换为其他几个字符;如we are tf将空格替换为%20,结果为we%20are%20tf(字符数组后有足够位置)
分析:如果从前往后替换则一些字符会多次移动浪费时间,如果能一次将其放入位置一定会减少资源利用的
思路:遍历一遍算出每个字符要移动到的位置,以便一次移动到目标位置
实现:
public static void replaceBlack(char s[]){
int i=0,length=0,count=0;
while(s[i]!='\0'){
if(s[i]==' '){
count++;
}
length++;
i++;
}
if(s==null||length==0)
return;
int last=count*2+length-1,pre=length-1;
while(pre!=last){
if(s[pre]!=' '){
s[last]=s[pre];
last--;
pre--;
}
else{
s[last--]='0';
s[last--]='2';
s[last--]='%';
pre--;
}
}
}
主调函数:
public static void main(String[] args) {
// TODO Auto-generated method stub
char[] s=new char[100];
s[0]='w';
s[1]='e';
s[2]=' ';
s[3]='a';
s[4]='r';
s[5]='e';
s[6]=' ';
s[7]='w';
s[8]='t';
s[9]='\0';
replaceBlack(s);
System.out.println(s);
}
结果:we%20are%20wt
相似案例:合并有序数组AB,结果放在A后面
实现:
public static void merge(int a[],int b[]){
int al=0,bl=0;
int i=0,last,prea,preb;
if(a==null||b==null)
return;
while(a[i]!='\0'){
al++;
i++;
}
i=0;
while(b[i]!='\0'){
bl++;
i++;
}
last=al+bl-1;
prea=al-1;
preb=bl-1;
if(prea==0||preb==0)
return;
while(prea>=0&&preb>=0){
if(a[prea]>b[preb]){
a[last]=a[prea];
last--;
prea--;
}
else{
a[last]=b[preb];
last--;
preb--;
}
}
while(preb>=0){
a[last]=b[preb];
preb--;
}
}
主调函数:
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] a=new int[100];
int [] b=new int[100];
a[0]=1;
a[1]=3;
a[2]=5;
a[3]='\0';
b[0]=2;
b[1]=4;
b[2]=4;
b[3]=9;
b[4]='\0';
merge(a, b);
for(int k=0;k<10;k++){
System.out.print(a[k]);
}
}
结果:1234459000
总结:遇到案例后如果有多次执行一个操作后到目标状态,应该想想是否可以一次可以到达目标状态。
分析:如果从前往后替换则一些字符会多次移动浪费时间,如果能一次将其放入位置一定会减少资源利用的
思路:遍历一遍算出每个字符要移动到的位置,以便一次移动到目标位置
实现:
public static void replaceBlack(char s[]){
int i=0,length=0,count=0;
while(s[i]!='\0'){
if(s[i]==' '){
count++;
}
length++;
i++;
}
if(s==null||length==0)
return;
int last=count*2+length-1,pre=length-1;
while(pre!=last){
if(s[pre]!=' '){
s[last]=s[pre];
last--;
pre--;
}
else{
s[last--]='0';
s[last--]='2';
s[last--]='%';
pre--;
}
}
}
主调函数:
public static void main(String[] args) {
// TODO Auto-generated method stub
char[] s=new char[100];
s[0]='w';
s[1]='e';
s[2]=' ';
s[3]='a';
s[4]='r';
s[5]='e';
s[6]=' ';
s[7]='w';
s[8]='t';
s[9]='\0';
replaceBlack(s);
System.out.println(s);
}
结果:we%20are%20wt
相似案例:合并有序数组AB,结果放在A后面
实现:
public static void merge(int a[],int b[]){
int al=0,bl=0;
int i=0,last,prea,preb;
if(a==null||b==null)
return;
while(a[i]!='\0'){
al++;
i++;
}
i=0;
while(b[i]!='\0'){
bl++;
i++;
}
last=al+bl-1;
prea=al-1;
preb=bl-1;
if(prea==0||preb==0)
return;
while(prea>=0&&preb>=0){
if(a[prea]>b[preb]){
a[last]=a[prea];
last--;
prea--;
}
else{
a[last]=b[preb];
last--;
preb--;
}
}
while(preb>=0){
a[last]=b[preb];
preb--;
}
}
主调函数:
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] a=new int[100];
int [] b=new int[100];
a[0]=1;
a[1]=3;
a[2]=5;
a[3]='\0';
b[0]=2;
b[1]=4;
b[2]=4;
b[3]=9;
b[4]='\0';
merge(a, b);
for(int k=0;k<10;k++){
System.out.print(a[k]);
}
}
结果:1234459000
总结:遇到案例后如果有多次执行一个操作后到目标状态,应该想想是否可以一次可以到达目标状态。
相关文章推荐
- 剑指offer--替换字符
- 剑指offer——替换字符
- 《剑指offer》字符串中的字符替换
- poj3981(字符替换)
- shell 替换特殊字符‘\’ '_'等
- 反射基础---成员变量反射应用,字符替换
- 使用POI替换word中的特定字符/文字改进版
- 《剑指Offer》:删除在另一个字符串中出现的字符
- 常见正则表达式运算 Regex 替换字符
- PHP中利用substr_replace将指定两位置之间的字符替换为*号
- 剑指Offer(二)替换空格
- JS基于正则截取替换特定字符之间字符串操作示例
- JS替换字符串中字符即替换全部而不是第一个
- 将字符串中指定字符替换成其他不同字符
- Oracle将字符串转换为数字,并替换特殊字符
- 在EXCEL中字符替换
- 替换html特殊字符
- [C/C++]_[初级]_[替换过滤utf8字符串里无效字符]
- 替换字符 sql-笔记