笔试题--Java实现命令编辑器

好久不见。

最近忙于校园招聘会,有招聘会自然就有笔试了。因此我在这里就分享某企业笔试中一道编程题。描述如下:

假定有一命令编辑框,可以接受两种命令:【type x】和【undo n】。x代表输入文本,n代表秒数。type命令用于当输入文本,而undo命令用于撤销操作。输入undo 1,表示向前撤销1秒。撤销操作可以指定撤销到当前时间之前操作的时间,并且撤销操作对自身同样有效。

举几个例子,

命令输入的时间命令显示结果
1type aa
2type bab
3undo 1a

 

 

 

 

 

命令输入的时间命令显示结果
1type aa
2type bab
3type cabc
4undo 2a
5undo 1abc

 

 

 

 

 

 

 

命令输入的时间命令显示结果
1type aa
2type bab
3type cabc
5undo 1abc

 

 

 

 

 

 

要求设计一个函数,可以接收命令和输入时间,返回最终结果。

当我看到这道题时,首先想到的是正则表达式匹配命令格式,使用map管理命令和输入时间,但是对操作与结果的缓存存在疑虑,最终还是没有完成。

今天又想起这题目,于是决定结束这未完成的命运:

View Code
 1 package com.lly.receivecomd;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 public class CommandEditor {
7
8 public String cmdEdit(String[] cmds, int[] times) {
9 //Regex to match two commands
10 String type = "^type\\s\\w+$";
11 String undo = "^undo\\s\\d$";
12 //Store the currently result
13 Map<Integer, String> map = new HashMap<Integer, String>();
14 String str = "";
15 for (int i = 0; i < cmds.length; i++) {
16 String[] text = cmds[i].split(" ");
17 if (cmds[i].matches(type)) {
18 //Different string object
19 str += text[text.length - 1];
20 map.put(times[i], str);
21 } else if (cmds[i].matches(undo)) {
22 int toUndoTime = Integer.parseInt(text[text.length - 1]);
23 if (toUndoTime > times[i]) {
24 return "Error Command!";
25 }
26 int rollbackTime = times[i] - toUndoTime - 1;
27 //Find the nearly rollback time
28 for (;!map.containsKey(rollbackTime); ) {
29 rollbackTime--;
30 }
31 map.put(times[i], map.get(rollbackTime));
32 } else {
33 return "Error Command!";
34 }
35
36 }
37 return map.get(times[times.length - 1]).toString();
38 }
39 }

完成后经一番调试得到以上代码。

思路与之前大体相同,关键点在于:1. 使用map缓存了每次操作后的时间和结果;2. 同一字符串引用改变后是不同的字符串对象。
测试代码如下: 

View Code
 1 package com.lly.test;
2
3 import static org.junit.Assert.*;
4
5 import org.junit.Test;
6 import com.lly.receivecomd.CommandEditor;
7
8 public class CmdEditorTest {
9
10 @Test
11 public void testCmdEdit() {
12 CommandEditor cmdEditor = new CommandEditor();
13 String[] cmds = { "type a", "type b", "type c", "undo 1", "undo 1" };
14 int[] times = { 1, 2, 3, 4, 5 };
15 String result = cmdEditor.cmdEdit(cmds, times);
16 assertEquals("abc", result);
17 }
18 }

 

转载于:https://www.cnblogs.com/dummyE/archive/2012/03/01/2376240.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值