day06

本文介绍了四个Java编程题目,涉及字符串异位词检查、数组交集、快乐数判断和两数之和,展示了使用哈希表、迭代和条件判断解决这些问题的方法。

242. 有效的字母异位词

字符串s和t都只包含小写字母,新建一个数组来存储s中每个字符出现的次数,然后遍历t,对应位置上的字符自减。最后遍历新数组,若数组中的每一位都是0,则符合条件。

class Solution {
    public boolean isAnagram(String s, String t) {
        int[] result = new int[26];
        for (int i = 0; i < t.length(); i++) {
            result[t.charAt(i) - 'a']++;
        }
        for (int i = 0; i < s.length(); i++) {
            result[s.charAt(i) - 'a']--;
        }

        for (int i = 0; i < result.length; i++) {
            if (result[i] != 0) {
                return false;
            }
        }
        return true;
    }
}

349. 两个数组的交集

使用两个hashset,第一个hashset用来存储nums1的所有元素,然后遍历nums2,同时判断nums2中的元素是否出现在第一个hashset,若出现,则说明当前元素时两个数组的交集,将其放入另一个hashset

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        if (nums1.length == 0 || nums1 == null || nums2.length == 0 || nums2 == null) {
            return new int[0];
        }
        HashSet<Integer> temp = new HashSet<>();
        HashSet<Integer> result = new HashSet<>();
        for (int i = 0; i < nums2.length; i++) {
            temp.add(nums2[i]);
        }
        for (int i = 0; i < nums1.length; i++) {
            if (temp.contains(nums1[i])) {
                result.add(nums1[i]);
            }
        }
        return result.stream().mapToInt(x -> x).toArray();
    }
}

202. 快乐数

注意条件无限循环,若无限循环返回false,直到找到值1。判断元素是否在集合中出现,使用hash法。

class Solution {
    public boolean isHappy(int n) {
        HashSet<Integer> result = new HashSet<>();
        while (n != 1 && !result.contains(n)) {
            result.add(n);
            n = getNextInt(n);
        }
        return n == 1;
    }
    private int getNextInt(int n) {
        int res = 0;
        while (n > 0) {
            int temp = n % 10;
            res += temp * temp;
            n = n / 10;
        }
        return res;
    }
}

1. 两数之和

新建一个hashmap,遍历数组,若map中不存在键为target-nums[i]的元素则将当前数组的值和下标放入map(数组的值做key的原因是简化操作)。若出现符合条件的元素,跳出循环并返回。

class Solution {
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int[] result = new int[2];

        for (int i = 0; i < nums.length; i++) {
            int temp = target - nums[i];
            if (map.containsKey(temp)) {
                result[0] = i;
                result[1] = map.get(temp);
                break;
            }
            map.put(nums[i], i);
        }
        return result;
    }
}

### 关于苍穹外卖 Day06 的教程或资料 目前,关于苍穹外卖项目的官方文档和公开资源主要集中在前五天的内容上[^2]。然而,在实际开发过程中,后续的课程通常会涉及更复杂的业务逻辑和技术细节,例如分布式事务处理、微服务架构优化以及性能调优等内容。 如果需要了解 **苍穹外卖 Day06** 的相关内容,可以尝试以下几个方向: #### 1. 微服务框架扩展 在第六天的学习中,可能会深入讲解如何通过 Spring Cloud 或 Dubbo 实现微服务之间的通信机制。这部分内容可能包括但不限于: - 使用 Feign 进行声明式 REST 调用。 - 配置负载均衡策略以提高系统的可用性和稳定性。 ```java @FeignClient(name = "order-service") public interface OrderServiceClient { @GetMapping("/orders/{id}") public ResponseEntity<Order> getOrderById(@PathVariable Long id); } ``` 上述代码片段展示了如何定义一个简单的 Feign 客户端来调用远程订单服务。 --- #### 2. 数据库分片与读写分离 随着项目规模的增长,数据库的压力也会逐渐增大。因此,Day06 可能会引入 ShardingSphere 或 MyCat 来实现数据分片和读写分离的功能。以下是配置文件的一个简单示例: ```yaml spring: shardingsphere: datasource: names: ds_0,ds_1 ds_0: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/db_0?serverTimezone=UTC&useSSL=false username: root password: 123456 ds_1: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/db_1?serverTimezone=UTC&useSSL=false username: root password: 123456 ``` 此部分重点在于提升数据库访问效率并降低单点故障风险。 --- #### 3. 缓存设计优化 缓存作为高性能系统的重要组成部分,其合理应用能够显著减少数据库查询次数。在 Day06 中,预计会对 Redis 缓存的设计进一步深化,比如利用 Redis Stream 处理实时消息队列或者采用布隆过滤器防止缓存穿透等问题。 以下是一个基于 RedisConfiguration 类的基础设置实例: ```java @Configuration public class RedisConfiguration { @Bean public LettuceConnectionFactory redisConnectionFactory() { return new LettuceConnectionFactory(); } @Bean public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); return template; } } ``` 这段代码用于初始化 Redis 连接池及相关模板工具类。 --- #### 4. 日志监控体系搭建 为了更好地追踪线上问题,构建完善的日志收集与分析平台显得尤为重要。ELK (Elasticsearch, Logstash, Kibana) 堆栈可能是该阶段的重点之一,帮助开发者快速定位异常情况并提供可视化报表支持。 --- 尽管当前未找到明确标注为“苍穹外卖 Day06”的具体材料[^1],但从整体技术路线推测以上几个方面均有可能成为教学的核心主题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值