有道编程题来问问答案,有指导一下吗?

袅浅 2019-11-24 12:34:35
输入:一个带转义字符‘\n’的字符串 要求删除转义字符‘\n’和它前面的字符,如果遇到多个多个连续‘\n’,则删除相同数量的转义字符和前面的字符,输出最终的字符。
例如:“abc\n\nd\n\nnghi” 输出:“nghi”
...全文
183 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_40599844 2019-11-25
  • 打赏
  • 举报
回复
new一个list,遍历输入的字符串,遇到非\n就执行add,遇到\n就执行remove,遍历完成后打印list。能不能行
  • 打赏
  • 举报
回复
引用 3 楼 落幕安魂 的回复:
    public static void main(String[] args) {

String str = "abc\n\nd\n\nnghi";

List<Character> list = new ArrayList<>();
int index = 0;
for (char c : str.toCharArray()) {
if (c == '\n') {
index -= 1;
continue;
}
list.add(index++, c);
}
list.subList(0, index).forEach(System.out::print);
}


二版
    public static void main(String[] args) {

String str = "abc\n\nd\n\nnghi";

char[] chars = str.toCharArray();
int index = 0;
for (char c : chars) {
if (c == '\n') {
index -= 1;
continue;
}
chars[index++] = c;
}
System.out.println(new String(chars, 0, index));
}
何处是天涯 2019-11-25
  • 打赏
  • 举报
回复
楼上的做法没错,就是在遍历字符串的时候遇到\ 还要判断后面跟的是不是n。
weixin_40599844 2019-11-25
  • 打赏
  • 举报
回复
new一个list,遍历输入的字符串,遇到非\n就执行add,遇到\n就执行remove,遍历完成后打印list。能不能行
  • 打赏
  • 举报
回复
    public static void main(String[] args) {

String str = "abc\n\nd\n\nnghi";

List<Character> list = new ArrayList<>();
int index = 0;
for (char c : str.toCharArray()) {
if (c == '\n') {
index -= 1;
continue;
}
list.add(index++, c);
}
list.subList(0, index).forEach(System.out::print);
}
  • 打赏
  • 举报
回复
new一个list,遍历输入的字符串,遇到非\n就执行add,遇到\n就执行remove,遍历完成后打印list。可行?
jiawenhe123 2019-11-24
  • 打赏
  • 举报
回复
使用String.split方法来处理: 使用\n转义符分割,这样除了最后一个字符串所有有内容的字符串都要回退一个字符, 如果出现空字符串说明这个就是一个\n,那么递归回退。 note:如果最后出现一个\n(split没有把最后这个单独分组),那么需要补充处理。
public class EscapeApplication {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("Please input the orginal string:");
		String input = in.nextLine();
		StringBuilder builder = reConstruct(handle(input));

		System.out.println(builder.toString());
	}

	//合并分割处理后的数据
	private static StringBuilder reConstruct(String[] datas) {
		StringBuilder builder = new StringBuilder();
		for (String data : datas) {
			builder.append(data);
		}
		return builder;
	}

	//处理转义字符串
	private static String[] handle(String input) {
		boolean lasted = false;
		if (input.endsWith("\\n")) {
			lasted = true;
		}
		String[] datas = input.split("\\\\n");

		for (int i = 0; i < datas.length; i++) {
			if (datas[i].length() == 0) {
				int backIndex = i - 1;
				while (backIndex >= 0) {
					String backStr = datas[backIndex];
					if (backStr.length() > 0) {
						datas[backIndex] = backStr.substring(0, backStr.length() - 1);
						break;
					}
					backIndex--;
				}
			} else {
				if (i != datas.length - 1)
					datas[i] = datas[i].substring(0, datas[i].length() - 1);
				else {
					if (lasted)
						datas[i] = datas[i].substring(0, datas[i].length() - 1);
				}
			}
		}
		return datas;
	}
}

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧