最右一面,凉透了的经

面试八股环节还好,但是我是力扣战士,面试官让在编译器上写代码
类都不会怎么写了,两个算法,字母异位词和策略模式,只写了方法,力扣战士凉透了,希望大家平时还是要在编译器上做题
策略模式等一些设计模式,希望大家也进行默写,别像我一样会背不会写
字母异位词
import java.util.*;

public class Main {
    public static List<List<String>> groupAnagrams(String[] strs) {
        // 用于存储分组结果的映射,键为排序后的字符串,值为对应的字符串列表
        Map<String, List<String>> anagramGroups = new HashMap<>();
        
        for (String s : strs) {
            // 将字符串转换为字符数组并排序
            char[] chars = s.toCharArray();
            Arrays.sort(chars);
            String sortedStr = new String(chars);
            
            // 如果映射中不存在该键,则创建一个新的列表
            anagramGroups.computeIfAbsent(sortedStr, k -> new ArrayList<>());
            // 将当前字符串添加到对应的列表中
            anagramGroups.get(sortedStr).add(s);
        }
        
        // 返回映射中的所有值(即所有分组)
        return new ArrayList<>(anagramGroups.values());
    }

    public static void main(String[] args) {
        // 示例输入
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        // 调用方法获取分组结果
        List<List<String>> result = groupAnagrams(strs);
        
        // 输出结果
        for (List<String> group : result) {
            System.out.println(group);
        }
    }
}
策略模式
// 支付策略接口
interface PaymentStrategy {
    void pay(double amount);
}

// 微信支付实现
class WechatPay implements PaymentStrategy {
    @Override
    public void pay(double amount) {
        System.out.println("使用微信支付:" + amount + "元");
        // 微信支付具体实现逻辑
    }
}

// 支付宝实现
class Alipay implements PaymentStrategy {
    @Override
    public void pay(double amount) {
        System.out.println("使用支付宝支付:" + amount + "元");
        // 支付宝支付具体实现逻辑
    }
}

// 银行卡支付实现
class BankCardPay implements PaymentStrategy {
    @Override
    public void pay(double amount) {
        System.out.println("使用银行卡支付:" + amount + "元");
        // 银行卡支付具体实现逻辑
    }
}

// 支付上下文(策略持有者)
class PaymentContext {
    private PaymentStrategy strategy;

    public PaymentContext(PaymentStrategy strategy) {
        this.strategy = strategy;
    }

    public void setStrategy(PaymentStrategy strategy) {
        this.strategy = strategy;
    }

    public void executePayment(double amount) {
        strategy.pay(amount);
    }
}

// 主类和测试代码
public class Main {
    public static void main(String[] args) {
        // 创建支付上下文
        PaymentContext context = new PaymentContext(new WechatPay());
        
        // 使用微信支付
        context.executePayment(100.0);
        
        // 切换为支付宝支付
        context.setStrategy(new Alipay());
        context.executePayment(200.0);
        
        // 切换为银行卡支付
        context.setStrategy(new BankCardPay());
        context.executePayment(300.0);
    }
}
全部评论
是秋招嘛
点赞 回复 分享
发布于 昨天 16:41 辽宁
最右的手撕是不是都很男啊我当时面暑期实习手撕是实现计算器
点赞 回复 分享
发布于 昨天 12:19 贵州
代码的尽头是英文吗
点赞 回复 分享
发布于 07-28 17:26 辽宁

相关推荐

评论
2
3
分享

创作者周榜

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