国庆中秋特辑系列文章:
国庆中秋特辑(三)使用生成对抗网络(GAN)生成具有节日氛围的画作,深度学习框架 TensorFlow 和 Keras 来实现
国庆中秋特辑(二)浪漫祝福方式 使用生成对抗网络(GAN)生成具有节日氛围的画作
国庆中秋特辑(一)浪漫祝福方式 用循环神经网络(RNN)或长短时记忆网络(LSTM)生成祝福诗词
以下是中高级Java软件工程师常见编程面试题,共有20道。
public boolean isSortedArray(int[] nums) {
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] > nums[i + 1]) {
return false;
}
}
return true;
}
public int[] removeDuplicates(int[] nums) {
if (nums == null || nums.length == 0) {
return null;
}
int i = 0;
for (int j = 0; j < nums.length; j++) {
if (i == 0 || nums[j]!= nums[i]) {
nums[i++] = nums[j];
}
}
int[] result = new int[i];
System.arraycopy(nums, 0, result, 0, i);
return result;
}
public int[] mergeTwoSortedArrays(int[] nums1, int[] nums2) {
int[] result = new int[nums1.length + nums2.length];
int i = 0, j = 0, k = 0;
while (i < nums1.length && j < nums2.length) {
if (nums1[i] < nums2[j]) {
result[k++] = nums1[i++];
} else {
result[k++] = nums2[j++];
}
}
while (i < nums1.length) {
result[k++] = nums1[i++];
}
while (j < nums2.length) {
result[k++] = nums2[j++];
}
return result;
}
public void reverseArray(int[] nums) {
for (int i = 0; i < nums.length; i++) {
int temp = nums[i];
nums[i] = nums[nums.length - 1 - i];
nums[nums.length - 1 - i] = temp;
}
}
public double averageArray(int[] nums) {
long sum = 0;
for (int num : nums) {
sum += num;
}
return (double) sum / nums.length;
}
public double medianArray(int[] nums) {
Arrays.sort(nums);
int length = nums.length;
if (length % 2 == 0) {
return (double) (nums[length / 2 - 1] + nums[length / 2]) / 2.0;
} else {
return (double) nums[length / 2];
}
}
public int mostCommon(int[] nums) {
int[] count = new int[101];
for (int num : nums) {
count[num]++;
}
int max = 0;
int res
for (int i = 0; i < 101; i++) {
if (count[i] > max) {
max = count[i];
res = i;
}
}
return res;
}
public double varianceArray(int[] nums) {
long sum = 0;
for (int num : nums) {
sum += num;
}
double mean = (double) sum / nums.length;
double sumSqr = 0;
for (int num : nums) {
double d = num - mean;
sumSqr += d * d;
}
return sumSqr / nums.length;
}
public double standardDeviationArray(int[] nums) {
double variance = varianceArray(nums);
return Math.sqrt(variance);
}
public boolean isPalindrome(String s) {
int i = 0;
int j = s.length() - 1;
while (i < j) {
if (s.charAt(i)!= s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
public String removeDuplicates(String s) {
if (s == null || s.length() == 0) {
return null;
}
char[] chars = new char[s.length()];
for (int i = 0; i < s.length(); i++) {
if (chars[i]!= s.charAt(i)) {
chars[chars.length - 1] = s.charAt(i);
}
}
return new String(chars);
}
public String mergeStrings(String s1, String s2) {
if (s1 == null) {
return s2;
}
if (s2 == null) {
return s1;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < Math.max(s1.length(), s2.length()); i++) {
char c1 = (i < s1.length())? s1.charAt(i) : '\0';
char c2 = (i < s2.length())? s2.charAt(i) : '\0';
sb.append(c1);
sb.append(c2);
}
return sb.toString();
}
public int editDistance(String s1, String s2) {
int m = s1.length();
int n = s2.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0) {
dp[i][j] = j;
} else if (j == 0) {
dp[i][j] = i;
} else if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1;
}
}
}
return dp[m][n];
}
public class Singleton {
private static Singleton instance;
private Singleton() {
}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
饿汉式:
public class Singleton {
private static final Singleton instance = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
return instance;
}
}
public class Factory {
public static void main(String[] args) {
Product productA = factory.createProductA();
Product productB = factory.createProductB();
productA.display();
productB.display();
}
public static Product createProductA() {
return new ProductA();
}
public static Product createProductB() {
return new ProductB();
}
}
abstract class Product {
public abstract void display();
}
class ProductA extends Product {
public void display() {
System.out.println("Product A");
}
}
class ProductB extends Product {
public void display() {
System.out.println("Product B");
}
}
public class ObserverPattern {
public static void main(String[] args) {
Subject subject = new Subject();
Observer observer1 = new ConcreteObserver(subject);
Observer observer2 = new ConcreteObserver(subject);
subject.addObserver(observer1);
subject.addObserver(observer2);
subject.notifyObservers();
}
public static interface Subject {
void addObserver(Observer observer);
void removeObserver(Observer observer);
void notifyObservers();
}
public static interface Observer {
void update(String message);
}
public static class ConcreteObserver implements Observer {
private Subject subject;
public ConcreteObserver(Subject subject) {
this.subject = subject;
}
@Override
public void update(String message) {
System.out.println("Received: " + message);
}
}
public static class ConcreteSubject implements Subject {
private List<Observer> observers;
public ConcreteSubject() {
observers = new ArrayList<>();
}
@Override
public void addObserver(Observer observer) {
observers.add(observer);
}
@Override
public void removeObserver(Observer observer) {
observers.remove(observer);
}
@Override
public void notifyObservers() {
for (Observer observer : observers) {
observer.update("Hello, World!");
}
}
}
}
public class StrategyPattern {
public static void main(String[] args) {
Strategy strategy = new DefaultStrategy();
strategy.execute();
strategy = new CustomStrategy();
strategy.execute();
}
public static interface Strategy {
void execute();
}
public static class DefaultStrategy implements Strategy {
@Override
public void execute() {
System.out.println("Default strategy");
}
}
public static class CustomStrategy implements Strategy {
@Override
public void execute() {
System.out.println("Custom strategy");
}
}
}
实现适配器模式需要以下几个步骤:
// 目标接口
public interface Target {
void request();
}
// 被适配的类
public class Adaptee {
public void specificRequest() {
System.out.println("被适配的类的方法被调用");
}
}
// 适配器类
public class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public void request() {
adaptee.specificRequest();
}
}
// 客户端代码
public class Client {
public static void main(String[] args) {
Adaptee adaptee = new Adaptee();
Target target = new Adapter(adaptee);
target.request();
}
}
在这个示例中,Target
是目标接口,Adaptee
是被适配的类,Adapter
是适配器类。适配器类 Adapter
实现了目标接口 Target
,并在其 request()
方法中调用了被适配的类 Adaptee
的 specificRequest()
方法。客户端代码通过目标接口 Target
使用适配器类 Adapter
,实现了对被适配的类 Adaptee
的调用。
public class HanoiTower {
public static void main(String[] args) {
int n = 3; // 设置盘子的数量
hanoi(n, 'A', 'B', 'C');
}
/**
* 汉诺塔递归方法
* @param n 盘子数量
* @param from 源柱子
* @param auxiliary 辅助柱子
* @param to 目标柱子
*/
public static void hanoi(int n, char from, char auxiliary, char to) {
if (n == 1) { // 当只有一个盘子时,直接从源柱子移动到目标柱子
System.out.println("Move disk 1 from " + from + " to " + to);
} else {
// 将 n-1 个盘子从源柱子借助目标柱子移动到辅助柱子
hanoi(n - 1, from, to, auxiliary);
// 将第 n 个盘子从源柱子移动到目标柱子
System.out.println("Move disk " + n + " from " + from + " to " + to);
// 将 n-1 个盘子从辅助柱子借助源柱子移动到目标柱子
hanoi(n - 1, auxiliary, from, to);
}
}
}
public class ShoppingCart {
private ArrayList<Item> items;
public ShoppingCart() {
items = new ArrayList<>();
}
/**
* 向购物车添加商品
* @param item 商品对象
*/
public void addItem(Item item) {
for (int i = 0; i < items.size(); i++) {
if (items.get(i) == item) {
items.set(i, item);
return;
}
}
items.add(item);
}
/**
* 从购物车中删除商品
* @param item 商品对象
* @return 是否成功删除
*/
public boolean removeItem(Item item) {
for (int i = 0; i < items.size(); i++) {
if (items.get(i) == item) {
items.remove(i);
return true;
}
}
return false;
}
/**
* 计算购物车中商品的总价
* @return 总价
*/
public double calculateTotal() {
double total = 0;
for (Item item : items) {
total += item.getPrice();
}
return total;
}
}
购物车类使用一个 ArrayList 来存储商品对象。添加商品、删除商品和计算总价的方法分别遍历 ArrayList 来完成相应操作。