leetcode--字符串

题目一:替换所有的问号(2020/12/30)

(题目链接:https://leetcode-cn.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/

给你一个仅包含小写英文字母和 '?' 字符的字符串 s,请你将所有的 '?' 转换为若干小写字母,使最终的字符串不包含任何 连续重复 的字符。

注意:你 不能 修改非 '?' 字符。

题目测试用例保证 除 '?' 字符 之外,不存在连续重复的字符。

在完成所有转换(可能无需转换)后返回最终的字符串。如果有多个解决方案,请返回其中任何一个。

可以证明,在给定的约束条件下,答案总是存在的。

方法一:自己的解题思路:

首先对输入的字符串的长度进行判断,分为三类:一是字符串长度为1,二是字符串长度为2,三是字符串长度为3

另外进行替换是,需要判断?被替换后与它前后要保持不同

Java源代码

package com.atguigu.interview;

public class modifystring {

	public static void main(String[] args) {
		String s = "i??";
		String str2="";
		int len1 = s.length();
		if(len1 == 1) {
			if(s.charAt(0) =='?') {
				str2 += 'a';
			}else {
				str2 += s.charAt(0);
			}
		}
		else if(len1 ==2) {
			if(s.charAt(0) =='?') {
				for(char ch ='a';ch<='z';ch++) {
					if(ch !=s.charAt(1)) {
						str2+=ch;
						break;
					}
				}
			}else {
				str2 += s.charAt(0);
			}
			
			if(s.charAt(1) =='?') {
				for(char ch ='a';ch<='z';ch++) {
					if(ch !=s.charAt(1) && ch!=str2.charAt(0)) {
						str2+=ch;
						break;
					}
				}
			}else {
				str2 += s.charAt(1);
			}
		}else if(len1>2) {
			if(s.charAt(0) =='?') {
				for(char ch ='a';ch<='z';ch++) {
					if(ch !=s.charAt(1)) {
						str2+=ch;
						break;
					}
				}
			}else {
				str2 += s.charAt(0);
			}
			for(int i =1;i<len1-1;i++) {
				if(s.charAt(i)=='?') {
					for(char ch ='a';ch<='z';ch++) {
						if(ch !=s.charAt(i-1)&&ch!=s.charAt(i+1)&&ch!=str2.charAt(i-1)) {
							str2+=ch;
							break;
						}
					}
				}else {
					str2 += s.charAt(i);
				}
			}
			if(s.charAt(len1-1) =='?') {
				for(char ch ='a';ch<='z';ch++) {
					if(ch !=s.charAt(len1-2)&&ch!=(str2.charAt(str2.length()-1))) {
						str2+=ch;
						break;
					}
				}
			}else {
				str2 += s.charAt(len1-1);
			}
		}
		System.out.println(str2);
	}
}

[注意1]:要分多种情况进行讨论

【注意2】:旧字符串被替换后元素发生对应变化,再进行前后是否相等判断的时候要注意使用新字符串中的元素进行比较

方法二:官方思路:

加两个哨兵,即在字符串首尾加上两个特殊字符,这样可以避免进行特殊判断

import string

s ="qe?r?"
s1 ='abcdefghijklmnopqrstuvwxyz'
alphabet = string.ascii_lowercase
newlists =list('0'+s+'0')
i = 1
print("yes1")
for i in range(1, len(newlists)):
    print("yes5")
    if newlists[i] =='?':
        for char in alphabet:
            if char != newlists[i - 1] and char != newlists[i + 1]:
                newlists[i] = char
                break
print(''.join(newlists[1:-1]))

【注意:】对于用字符进行替换,我们使用alphabet = string.ascii_lowercase

第二次写的代码:

class Solution {
    public String modifyString(String s) {
        char[] repl = {'a','b','c','d','e','f','g','h'};
        //String repl = "abcdefghijklmnopqrstuvwxyz";
        String temps = '0'+s+'0';
        char[] ts = temps.toCharArray();
        for(int i=1;i<ts.length-1;i++){
            if(ts[i]=='?'){
                int j=0;
                while(ts[i-1]==repl[j] || ts[i+1] == repl[j]){
                    j++;
                }
                ts[i]=repl[j];
            }
        }
        String res = new String(ts);
        return res.substring(1,res.length()-1);
    }
}

注意:1)实现把要替换的字符存放在一个字符串数组里面;

           2)原始字符串首尾各加一个字符'0',便于处理边界问题.

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值