京东笔试编程题,重叠子串和括弧


京东笔试,第一条关于重叠子串的题目 


public static void main(String[] args) {

Scanner in = new Scanner(System.in);

String s = in.next();

char[] str = s.toCharArray();

int index = -1;

for (int i = 1; i < str.length; i++) {

boolean overlap = true;

for (int j = 0; i+j < str.length; j++) {

if (str[i+j] != str[j]) {

overlap = false;

break;

}

}

if (overlap) {

index = i;

break;

}

}

String res = null;

if (index != -1) {

res = s + s.substring(str.length - index);

}

else {

res = s + s;

}

System.out.println(res);

}



第二道 关于括弧的题目



public class Second {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

String line = in.nextLine();

HashMap<String, Integer> map = new HashMap<>();

int time = findTime(line, map);

System.out.println(time);

}

public static int findTime(String line, HashMap<String, Integer> map) {

if (line.isEmpty()) {

return 1;

}

if (map.containsKey(line)) {

return map.get(line);

}

int res = 0;

for (int i = 1; i < line.length(); i++) {

if (line.charAt(i) == ')') {

String left = null;

if (i == 1) {

left = line.substring(2);

}

else {

left = line.substring(1, i) + line.substring(i+1);

}

if (isLegal(left)) {

res += findTime(left, map);

}

}

}

map.put(line, res);

return res;

}

public static boolean isLegal(String str) {

int l = 0, r = 0;

for (int i = 0; i < str.length(); i++) {

if (str.charAt(i) == '(') {

l++;

}

else {

r++;

}

if (r > l) {

return false;

}

}

return true;

}

}



#京东#
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 17:10
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 16:15
我应届生,去年10月份开始在这家公司实习,到今年10月份正好一年想(实习+试用期),在想要不要提前9月份就离职,这样好找工作些,但又差一个月满一年,又怕10月份国庆回来离职,容易错过了下半年的金九银十,到年底容易gap到年后
小破站_程序员YT:说这家公司不好吧,你干了快一年 说这家公司好吧,你刚毕业就想跑路说你不懂行情吧,你怕错过金九银十说 你懂行情吧,校招阶段在实习,毕业社招想换工作 哥们,我该怎么劝你留下来呢
应届生,你找到工作了吗
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务