刷题总结
第一题
最近题目刷的还是少,笔试做得惨不忍睹。
首先分享一道百度的笔试题(2023-09-27)。java编程题第三题
题目忘完了快,不过百度让用本地idea。所以记录下来了代码。
就是有关注释的最后一道编程题。当然昨天没写对。差一个条件没搞出来很伤心。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<String> list=new ArrayList<>();
while(sc.hasNextLine()){
String line = sc.nextLine();
list.add(line);
if("end".equals(line)) break;
}
for (String s:list) {
System.out.println(s);
}
int count =sc.nextInt();
for (int i = 0; i < count; i++) {
int command= sc.nextInt();
int num=sc.nextInt()-1;
if(command==1){
//命令一追加注释
String s = list.get(num);
s.concat(sc.next());
list.set(num,s);
}else if(command==2){
//
String s = list.get(num);
String[] split = s.split("//");
list.set(num,split[0]);
}
else if(command==3){
//把对应行变成注释
String s = list.get(num);
if("//".equals(s.substring(0,2))) continue;
else {
String concat = "//".concat(s);
list.set(num,concat);
}
}
else if(command==4){
//取消对应行注释
String s = list.get(num);
if("//".equals(s.substring(0,2))){
s=s.substring(2);
list.set(num,s);
}else{
continue;
}
}
else if(command==5){
//显示注释
String s = list.get(num);
String[] split = s.split("//");
//System.out.println("5:+s->"+s+","+(split.length==1));
if(split.length==1) {
System.out.println("null");
continue;
}
for (int j = 1; j < split.length; j++) {
if(j==split.length-2)
System.out.print(split[j]+"//");
else System.out.print(split[j]);
}
System.out.println();
}
}
}
}
输入的是:
import java.util.*;
class Main{
public static void main(String[] args){
Node x,y=new Node(5); //‘new’ can lots of things.
//Node b=new Node();
System.out.print(x.val);
//return; //it can be ignored.
}
}
end
10
5 4
3 4
5 4
5 5
4 5
5 5
1 7 Nice!
5 7
4 7
5 7
结果
剑指offer
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param numbers int整型一维数组
* @return int整型
*/
public int duplicate (int[] numbers) {
// write code here
int length = numbers.length;
boolean[] flag = new boolean[length];
for (int i = 0; i < length; i++) {
flag[i] = false;
}
for (int i = 0; i < length; i++) {
if (flag[numbers[i]] == false) flag[numbers[i]] = true;
else return numbers[i];
}
return -1;
}
}
一个标记数组搞定。
JZ4 二维数组中的查找
public class Solution {
public boolean Find(int target, int [][] array) {
//优先判断特殊
if(array.length == 0)
return false;
int n = array.length;
if(array[0].length == 0)
return false;
int m = array[0].length;
//从最左下角的元素开始往左或往上
for(int i = n - 1, j = 0; i >= 0 && j < m; ){
//元素较大,往上走
if(array[i][j] > target)
i--;
//元素较小,往右走
else if(array[i][j] < target)
j++;
else
return true;
}
return false;
}
}
用到了递归。
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.*;
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre, int [] vin) {
int n = pre.length;
int m = vin.length;
if (n == 0 || m == 0) return null;
TreeNode root = new TreeNode(pre[0]);
for (int i = 0; i < vin.length; i++) {
if (pre[0] == vin[i]) {
root.left = reConstructBinaryTree(Arrays.copyOfRange(pre, 1, i + 1),
Arrays.copyOfRange(vin, 0, i));
root.right = reConstructBinaryTree(Arrays.copyOfRange(pre, i + 1, pre.length),
Arrays.copyOfRange(vin, i + 1, vin.length));
break;
}
}
return root;
}
}
JZ8 二叉树的下一个结点
中序遍历 左根右
/*
public class TreeLinkNode {
int val;
TreeLinkNode left = null;
TreeLinkNode right = null;
TreeLinkNode next = null;
TreeLinkNode(int val) {
this.val = val;
}
}
*/
import java.util.*;
public class Solution {
ArrayList<TreeLinkNode> nodes = new ArrayList<>();
public TreeLinkNode GetNext(TreeLinkNode pNode) {
// 获取根节点
TreeLinkNode root = pNode;
while (root.next != null) root = root.next;
// 中序遍历打造nodes
InOrder(root);
// 进行匹配
int n = nodes.size();
for (int i = 0; i < n - 1; i++) {
TreeLinkNode cur = nodes.get(i);
if (pNode == cur) {
return nodes.get(i + 1);
}
}
return null;
}
// 中序遍历
void InOrder(TreeLinkNode root) {
if (root != null) {
InOrder(root.left);
nodes.add(root);
InOrder(root.right);
}
}
}
JZ10 斐波那契数列
斐波那契数列
1 1 2 3 5 8
当前位置数等于前两个数之和
public class Solution {
public int Fibonacci(int n) {
if (n <= 2) return 1;
int num1 = 1;
int num2 = 1;
int num3 = 0;
for (int i = 3; i <= n; i++) {
num3 = num1 + num2;
num1 = num2;
num2 = num3;
}
return num3;
}
}