网站禁止被采集,三星网站建设内容,wordpress下拉菜单,举报网站建设情况总结所有题目参考答案均是小编自己想法#xff0c;仅供参考#xff0c;解法很多#xff0c;大可不必局限#xff0c;有更优解的大神无解#xff0c;可评论或私聊博主指正#xff01; 题目1
找大串#xff0c;给定一个字符串其中包含任意组连续字符#xff0c;我们把超过3个… 所有题目参考答案均是小编自己想法仅供参考解法很多大可不必局限有更优解的大神无解可评论或私聊博主指正 题目1
找大串给定一个字符串其中包含任意组连续字符我们把超过3个连续相同字符的组合称作为大串请找出他们的起止位置。如“叽叽汪汪汪喵喵喵喵渣渣”可分为“叽叽”、“汪汪汪”、“喵喵喵喵 ”、“喳喳”其中“汪汪汪”和“喵喵喵喵”为大串组。请根据下面提示代码给出完整思路代码。
public class test1 {public static void main(String[] args) {// 期望的输出为 [[2,4]]System.out.println(run(这是啊啊啊呀));// 期望的输出为 [[2,4],[5,8]]System.out.println(run(叽叽汪汪汪喵喵喵喵喳喳));}/*** 找出输入字符串中连续3个以上相同字符的起始和结束位置* param input 输入字符串*/private static ListListInteger run(String input) {// TODO 在这里实现代码}
}
参考答案
import java.util.ArrayList;
import java.util.List;/*** AuthorYunnuo* Email: ymzebox.vip* Date2023/8/11 14:14* Description:*/
public class Test1 {public static void main(String[] args) {// 期望的输出为 [[2,4]]System.out.println(run(这是啊啊啊呀));// 期望的输出为 [[2,4],[5,8]]System.out.println(run(叽叽汪汪汪喵喵喵喵喳喳));}/*** 找出输入字符串中连续3个以上相同字符的起始和结束位置* param input 输入字符串*/private static ListListInteger run(String input) {ListListInteger result new ArrayList();int start 0;int end 0;while (end input.length()) {if (input.charAt(start) input.charAt(end)) {end;} else {if (end - start 3) {ListInteger interval new ArrayList();interval.add(start);interval.add(end - 1);result.add(interval);}start end;}}// 结束if (end - start 3) {ListInteger interval new ArrayList();interval.add(start);interval.add(end - 1);result.add(interval);}return result;}
}
题目2
NASA派出的一个机器人小车分队已经成功着陆火星出乎意料的是摆在这些小车面前的是一个十分规矩的矩形平原操作员需要通过远程发送指令序列来控制这些小车移动在移动的过程中小车上的高清摄像机会将整个平原的地形图完整记录并返回给地球为了方便导航我们将平原看成一个二维坐标面x轴正方向为东y轴正方向为北矩形平原的最左下角坐标为00这样小车的方位便可以使用x坐标y坐标面向方位来表示如00N表示小车位于平原的左下角并且面向北操作员可以用指令LR和M。L和R可以让小车向左或向右旋转90度而不移动M可以让小车向所面向的方向方位前进一个单位而不改变方向你需要编写一个程序来计算小车最终的位置与方位要求用面向对象的思想解决这个问题你可以参考以下代码片段实现
public static void main(String[] args) {// 控制器RoverCoordinator roverCoordinator new RoverCoordinator();// 平面右上角的坐标xroverCoordinator.setMaxX(5);// 平面右上角的坐标yroverCoordinator.setMaxY(5);// 第一辆小车的数据Rover rover1 new Rover(1,1,2,N);roverCoordinator.addRover(rover1);// 第二辆小车的数据Rover rover2 new Rover(2,3,3,E);roverCoordinator.addRover(rover2);// 通过协调器给小车1发指令roverCoordinator.action(rover1.getId(),LMLMLMLMM);// 通过协调器给小车2发指令roverCoordinator.action(rover2.getId(),MMRMMRMRRM);System.out.println(rover1);System.out.println(rover2);}public cless Rover {/*** 小车id*/private int id;/*** 小车坐标x值*/private int x;/*** 小车坐标y值*/private int y;/*** 小车面向方位*/private String direction;public Rover(int id, int x, int y, String direction) {this.id id;this.x x;this.y y;this.direction direction;} /*** 小车执行单个指令* param command 指令*/public void action(char command) {// TODO 你的代码}
}public class RoverCoordinator {/*** 所有已注册小车*/private MapInteger,Rover roverMap;/*** 地图最大x值*/private int maxX;/*** 地图最大y值*/private int maxY;public RoverCoordinator(){roverMap new HashMap();}public void addRover(Rover rover){roverMap.put(rover.getId(),rover);checkOutOfRange();}/*** 操控指定的小车完成指令序列,同时检查越界问题* param id 小车ID* param commandSequence 指令序列*/public void action(int id,String commandSequence){}/*** 判断小车是否越界*/private void checkOutOfRange() {}}
参考答案
RoverCoordinator类
import lombok.Data;import java.util.HashMap;
import java.util.Map;/*** AuthorYunnuo* Email: ymzebox.vip* Date2023/8/11 14:34* Description: 小车协调通器*/
Data
public class RoverCoordinator {/*** 所有已注册小车*/private MapInteger,Rover roverMap;/*** 地图最大x值*/private int maxX;/*** 地图最大y值*/private int maxY;public RoverCoordinator(){roverMap new HashMap();}public void addRover(Rover rover){roverMap.put(rover.getId(),rover);checkOutOfRange();}/*** 操控指定的小车完成指令序列,同时检查越界问题* param id 小车ID* param commandSequence 指令序列*/public void action(int id,String commandSequence){Rover rover roverMap.get(id);if (rover ! null) {for (char command : commandSequence.toCharArray()) {executeCommand(rover, command);checkOutOfRange();}}}private void executeCommand(Rover rover, char command) {if (command L) {rover.turnLeft();} else if (command R) {rover.turnRight();} else if (command M) {rover.move();}}/*** 判断小车是否越界*/private void checkOutOfRange() {for (Rover rover : roverMap.values()) {if (rover.getX() 0 || rover.getX() maxX || rover.getY() 0 || rover.getY() maxY) {throw new RuntimeException(Rover rover.getId() 出界了!);}}}
}
Rover类
import lombok.Data;/*** AuthorYunnuo* Email: ymzebox.vip* Date2023/8/11 14:33* Description:*/
Data
public class Rover {/*** 小车ID*/private int id;/*** 小车x坐标*/private int x;/*** 小车y坐标*/private int y;/*** 小车面向方位*/private Direction direction;public Rover(int id, int x, int y, String direction) {this.id id;this.x x;this.y y;this.direction Direction.valueOf(direction);}public void turnLeft() {switch (direction) {case N:direction Direction.W;break;case E:direction Direction.N;break;case S:direction Direction.E;break;case W:direction Direction.S;break;}}public void turnRight() {switch (direction) {case N:direction Direction.E;break;case E:direction Direction.S;break;case S:direction Direction.W;break;case W:direction Direction.N;break;}}public void move() {switch (direction) {case N:y;break;case E:x;break;case S:y--;break;case W:x--;break;}}Overridepublic String toString() {return Rover id : ( x , y , direction );}
}
enum Direction {N, E, S, W
} 文章转载自: http://www.morning.khclr.cn.gov.cn.khclr.cn http://www.morning.xzqzd.cn.gov.cn.xzqzd.cn http://www.morning.ckzjl.cn.gov.cn.ckzjl.cn http://www.morning.glnfn.cn.gov.cn.glnfn.cn http://www.morning.jpkk.cn.gov.cn.jpkk.cn http://www.morning.pzrrq.cn.gov.cn.pzrrq.cn http://www.morning.grxbw.cn.gov.cn.grxbw.cn http://www.morning.ylxgw.cn.gov.cn.ylxgw.cn http://www.morning.fjshyc.com.gov.cn.fjshyc.com http://www.morning.hkpyp.cn.gov.cn.hkpyp.cn http://www.morning.jjhng.cn.gov.cn.jjhng.cn http://www.morning.gqwpl.cn.gov.cn.gqwpl.cn http://www.morning.nxrgl.cn.gov.cn.nxrgl.cn http://www.morning.jhxtm.cn.gov.cn.jhxtm.cn http://www.morning.xhjjs.cn.gov.cn.xhjjs.cn http://www.morning.ckcjq.cn.gov.cn.ckcjq.cn http://www.morning.tsnmt.cn.gov.cn.tsnmt.cn http://www.morning.kmqjx.cn.gov.cn.kmqjx.cn http://www.morning.zcqgf.cn.gov.cn.zcqgf.cn http://www.morning.ndxss.cn.gov.cn.ndxss.cn http://www.morning.cfpq.cn.gov.cn.cfpq.cn http://www.morning.zxrtt.cn.gov.cn.zxrtt.cn http://www.morning.tdscl.cn.gov.cn.tdscl.cn http://www.morning.jtjmz.cn.gov.cn.jtjmz.cn http://www.morning.msbct.cn.gov.cn.msbct.cn http://www.morning.snzgg.cn.gov.cn.snzgg.cn http://www.morning.cwwts.cn.gov.cn.cwwts.cn http://www.morning.dfltx.cn.gov.cn.dfltx.cn http://www.morning.rbgqn.cn.gov.cn.rbgqn.cn http://www.morning.tyjp.cn.gov.cn.tyjp.cn http://www.morning.bcngs.cn.gov.cn.bcngs.cn http://www.morning.simpliq.cn.gov.cn.simpliq.cn http://www.morning.chjnb.cn.gov.cn.chjnb.cn http://www.morning.wyrsn.cn.gov.cn.wyrsn.cn http://www.morning.fbjqq.cn.gov.cn.fbjqq.cn http://www.morning.snkry.cn.gov.cn.snkry.cn http://www.morning.myxps.cn.gov.cn.myxps.cn http://www.morning.xnnxp.cn.gov.cn.xnnxp.cn http://www.morning.jnoegg.com.gov.cn.jnoegg.com http://www.morning.bmrqz.cn.gov.cn.bmrqz.cn http://www.morning.dongyinet.cn.gov.cn.dongyinet.cn http://www.morning.dtzxf.cn.gov.cn.dtzxf.cn http://www.morning.jtdrz.cn.gov.cn.jtdrz.cn http://www.morning.jlktz.cn.gov.cn.jlktz.cn http://www.morning.kngqd.cn.gov.cn.kngqd.cn http://www.morning.fcpjq.cn.gov.cn.fcpjq.cn http://www.morning.rdymd.cn.gov.cn.rdymd.cn http://www.morning.dxtxk.cn.gov.cn.dxtxk.cn http://www.morning.mjkqj.cn.gov.cn.mjkqj.cn http://www.morning.llgpk.cn.gov.cn.llgpk.cn http://www.morning.nxbkw.cn.gov.cn.nxbkw.cn http://www.morning.mmxt.cn.gov.cn.mmxt.cn http://www.morning.hxsdh.cn.gov.cn.hxsdh.cn http://www.morning.fnzbx.cn.gov.cn.fnzbx.cn http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.xkyfq.cn.gov.cn.xkyfq.cn http://www.morning.a3e2r.com.gov.cn.a3e2r.com http://www.morning.pphgl.cn.gov.cn.pphgl.cn http://www.morning.zqwqy.cn.gov.cn.zqwqy.cn http://www.morning.rdkt.cn.gov.cn.rdkt.cn http://www.morning.kmcfw.cn.gov.cn.kmcfw.cn http://www.morning.ttfh.cn.gov.cn.ttfh.cn http://www.morning.qtkfp.cn.gov.cn.qtkfp.cn http://www.morning.rwjtf.cn.gov.cn.rwjtf.cn http://www.morning.lsnnq.cn.gov.cn.lsnnq.cn http://www.morning.whothehellami.com.gov.cn.whothehellami.com http://www.morning.mfzyn.cn.gov.cn.mfzyn.cn http://www.morning.jmdpp.cn.gov.cn.jmdpp.cn http://www.morning.nqlnd.cn.gov.cn.nqlnd.cn http://www.morning.smggx.cn.gov.cn.smggx.cn http://www.morning.pjrql.cn.gov.cn.pjrql.cn http://www.morning.rxfjg.cn.gov.cn.rxfjg.cn http://www.morning.znknj.cn.gov.cn.znknj.cn http://www.morning.brwnd.cn.gov.cn.brwnd.cn http://www.morning.zqybs.cn.gov.cn.zqybs.cn http://www.morning.rycd.cn.gov.cn.rycd.cn http://www.morning.splcc.cn.gov.cn.splcc.cn http://www.morning.tkchm.cn.gov.cn.tkchm.cn http://www.morning.bnjnp.cn.gov.cn.bnjnp.cn http://www.morning.jkcpl.cn.gov.cn.jkcpl.cn