流利说的机试是做过的最简单的了,是唯一一个半小时就都写完AC的。
题目一 判断是否是IPV4地址
题目分析
第一题是输入字符串判断是不是 IPV4 格式,首先按照 “.”分割,数组个数必须是四,且每个小的元素长度不超过三,且不含非数字之外的字符,同时数字的范围是0-255。都符合的话就是IPV4。输出true,否则输出false。
代码实现
//判断是否是IPV4地址
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String string = in.nextLine();
String[] temp = string.split("\\.");
boolean istrue = true;
if (temp.length != 4) {
System.out.println(false);
istrue = false;
} else {
for (int i = 0; i < 4; i++) {
if (temp[i].length() > 3) {
System.out.println("false");
istrue = false;
break;
} else {
int num = 0;
for (int j = 0; j < temp[i].length(); j++) {
if (temp[i].charAt(j) > '9' || temp[i].charAt(j) < '0') {
System.out.println("false");
istrue = false;
break;
} else {
num += num * 10 + temp[i].charAt(j) - '0';
}
}
if (num > 255 || num < 0) {
System.out.println("false");
istrue = false;
break;
}
}
}
if (istrue)
System.out.println("true");
}
}
}
题目二 链表翻转
题目分析
虽然是链表翻转,但是输入和输出形式都是数组,所以我大胆猜测这个存到数组里反着输出也是一样的,但是这肯定是不对的。
可以使用头插法或者是使用栈两种形式来进行链表的翻转。
代码实现
//链表倒序
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Node head = new Node(0, null);
Node h1 = head;
int count = 0;
while (count < 4) {
Node temp = new Node(in.nextInt(), null);
h1.next = temp;
h1 = h1.next;
count++;
}
Node out = new Node(0, null);
Node out1 = out;
Stack<Node> stack = new Stack<Node>();
while (count-- > 0) {
stack.push(head.next);
head = head.next;
}
int i = 0;
while (!stack.isEmpty()) {
if (i != 0)
System.out.println(" ");
System.out.println(stack.peek().val);
out.next = stack.pop();
i++;
}
out.next = null;
//out1指向翻转后的链表
}
}
class Node {
int val;
Node next;
public Node(int val, Node next) {
this.val = val;
this.next = next;
}
}