AI刷题中等:SQL代码补全功能| 豆包MarsCode AI刷题

95 阅读3分钟

每日一题:虽说是中等题目,不过做起来相比其他的其实也更简单,只需要java的hash去重和字符串查询即可,也算是加深下对哈希表和字符串这一方面的印象了吧,好久没刷字符串的题目也容易忘了。

image.png

import java.util.*;
public class Main {
    public static String solution(int num, String[] data, String input) {
        List<String> matches = new ArrayList<>();
        //对String data去重
        Set<String> s=new HashSet<>(Arrays.asList(data));
        for(String w:data){
            s.add(w);
        }
        // 遍历数据数组,检查每个元素是否以input开头
        for (String word : s) {
            if (word.startsWith(input)) {
                matches.add(word);
            }
        }
        // 如果没有匹配的元素,返回"-1"
        if (matches.isEmpty()) {
            return "-1";
        }
        // 按字典序排序匹配的元素
        Collections.sort(matches);
        // 将匹配的元素格式化为一个字符串,用逗号分隔
        return String.join(",", matches);
    }

    public static void main(String[] args) {
        //  You can add more test cases here
        String[] testData1 = {"select", "from", "where", "limit", "origin_log_db", "event_log_table", "user_id", "from_mobile"};
        String[] testData2 = {"select", "from", "where", "limit", "group", "having", "in", "index", "inner", "insert", "like", "log_db", "log_table", "user_id", "group_name", "group_id"};
        System.out.println(solution(8, testData1, "f").equals("from,from_mobile"));
        System.out.println(solution(16, testData2, "g").equals("group,group_name,group_id"));
        System.out.println(solution(16, testData2, "m").equals("-1"));
    }
}

详细分析:

方法分析

  1. 代码逻辑
    • 去重
      • 使用 HashSetdata 数组进行去重。虽然 HashSet 本身已经去重,但你代码中的 for 循环再次添加元素到 HashSet 中是多余的,因为 HashSet 会自动忽略重复元素。
    • 匹配
      • 遍历去重后的 HashSet,检查每个元素是否以 input 开头,使用 startsWith 方法进行判断。
    • 排序
      • 将匹配的元素存储在 ArrayList 中,并使用 Collections.sort 方法按字典序排序。
    • 格式化输出
      • 使用 String.join 方法将排序后的匹配元素用逗号连接成一个字符串。
    • 返回结果
      • 如果没有匹配的元素,返回 "-1"

代码优化建议

  1. 去重部分
    • 可以直接使用 HashSetdata 数组进行去重,不需要额外的 for 循环。
  2. 性能优化
    • 如果 data 数组非常大,可以考虑在匹配之前对 data 数组进行排序,然后使用二分查找来提高匹配效率。

接下来补充些对于哈希表和字符串的知识,作为备忘:

哈希表(HashSet)

知识点

  1. 定义

    • HashSet 是 Java 集合框架中的一种数据结构,基于哈希表实现,用于存储不重复的元素。
  2. 特点

    • 无序性HashSet 中的元素没有特定的顺序。
    • 唯一性HashSet 中的元素是唯一的,不会存储重复的元素。
    • 快速查找:由于基于哈希表实现,查找、插入和删除操作的时间复杂度为 O(1)。
  3. 常用方法

    • add(E e):向集合中添加元素。
    • contains(Object o):判断集合中是否包含指定元素。
    • remove(Object o):从集合中移除指定元素。
    • size():返回集合中元素的数量。
    • isEmpty():判断集合是否为空。

字符串(String)

知识点

  1. 定义

    • String 是 Java 中用于表示字符串的类,字符串是不可变的(immutable),即一旦创建,其内容不能被修改。
  2. 常用方法

    • length():返回字符串的长度。
    • charAt(int index):返回指定索引处的字符。
    • substring(int beginIndex, int endIndex):返回从 beginIndexendIndex-1 的子字符串。
    • startsWith(String prefix):判断字符串是否以指定的前缀开头。
    • endsWith(String suffix):判断字符串是否以指定的后缀结尾。
    • equals(Object obj):判断字符串是否与指定对象相等。
    • compareTo(String anotherString):按字典序比较两个字符串。
    • split(String regex):根据正则表达式分割字符串,返回字符串数组。
    • join(CharSequence delimiter, Iterable<? extends CharSequence> elements):使用指定的分隔符连接字符串集合。