【百度面经】HCG 秋招提前批一面
百度HCG 秋招提前批一面面经,难度一般~
13. 手撕快排
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine());
int[] arr = new int[n];
String[] strs = reader.readLine().split(" ");
for(int i = 0; i < n; i++){
arr[i] = Integer.parseInt(strs[i]);
}
quickSort(arr, 0, arr.length - 1);
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
reader.close();
}
public static void quickSort(int[] arr, int start, int end){
if(start < end){
int low = start;
int high = end;
int stard = arr[start];
while(low < high){
while(low < high && stard <= arr[high]){
high--;
}
arr[low] = arr[high];
while(low < high && arr[low] <= stard){
low++;
}
arr[high] = arr[low];
}
arr[low] = stard;
quickSort(arr, start, low);
quickSort(arr, low+1 ,end);
}
}
}
14. 手撕工厂模式
首先,定义一个形状接口(Shape.java):
public interface Shape {
void draw();
}
然后,实现该接口的两个具体类,圆形(Circle.java)和矩形(Rectangle.java):
// Circle.java
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
// Rectangle.java
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
接下来,创建一个形状工厂(ShapeFactory.java)来生成基于给定信息的形状对象:
public class ShapeFactory {
// 使用getShape方法获取形状类型的对象
public Shape getShape(String shapeType) {
if (shapeType == null) {
return null;
}
if (shapeType.equalsIgnoreCase("CIRCLE")) {
return new Circle();
} else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
return new Rectangle();
}
// 默认返回null,可以根据需要抛出异常或返回null
return null;
}
}
最后,可以使用这个工厂类来创建形状对象并调用它们的draw
方法:
public class FactoryPatternDemo {
public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();
// 获取Circle的对象,并调用它的draw方法
Shape shape1 = shapeFactory.getShape("CIRCLE");
shape1.draw();
// 获取Rectangle的对象,并调用它的draw方法
Shape shape2 = shapeFactory.getShape("RECTANGLE");
shape2.draw();
// 获取不存在的形状类型的对象,将返回null
Shape shape3 = shapeFactory.getShape("SQUARE");
if (shape3 != null) {
shape3.draw();
} else {
System.out.println("Invalid shape type");
}
}
}
#软件开发笔面经#校招面经大全 文章被收录于专栏
收录各个网友分享的各个公司的面经,并给出答案。