【leetcode】Contest98

简介: 一开始本来打算省时间用特么的有道翻译, 结果直接懵逼了。其实还是自己看题目, 结合例子理解一下很快的。   1. 题1(3分) 题目 888. Fair Candy Swap Difficulty: Easy Alice and Bob have candy bars of d...

 

一开始本来打算省时间用特么的有道翻译, 结果直接懵逼了。其实还是自己看题目, 结合例子理解一下很快的。

 

1. 题1(3分)

题目

888. Fair Candy Swap
Difficulty: Easy
Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Alice has, and B[j] is the size of the j-th bar of candy that Bob has.

Since they are friends, they would like to exchange one candy bar each so that after the exchange, they both have the same total amount of candy.  (The total amount of candy a person has is the sum of the sizes of candy bars they have.)

Return an integer array ans where ans[0] is the size of the candy bar that Alice must exchange, and ans[1] is the size of the candy bar that Bob must exchange.

If there are multiple answers, you may return any one of them.  It is guaranteed an answer exists.
Example 1:

Input: A = [1,1], B = [2,2]
Output: [1,2]
Example 2:

Input: A = [1,2], B = [2,3]
Output: [1,2]
Example 3:

Input: A = [2], B = [1,3]
Output: [2,3]
Example 4:

Input: A = [1,2,5], B = [2,4]
Output: [5,4]
 

Note:

1 <= A.length <= 10000
1 <= B.length <= 10000
1 <= A[i] <= 100000
1 <= B[i] <= 100000
It is guaranteed that Alice and Bob have different total amounts of candy.
It is guaranteed there exists an answer.

 

 

我的答案

这个就是一个简单的算术题。。没什么技术难度

class Solution {
    public int[] fairCandySwap(int[] A, int[] B) {
        int totalA = 0;
        int totalB = 0;
        for(int a : A) {
            totalA += a;
        }
        for(int b : B) {
            totalB += b;
        }
        int[] results = new int[2];
        for(int a : A)
            for(int b : B) {
                if(totalA - a + b == totalB + a - b){
                    results[0] = a;
                    results[1] = b;
                }     
            }
        return results;
    }
}

 

2. 题2(5分)

890. Find and Replace Pattern

Difficulty: Medium
You have a list of words and a pattern, and you want to know which words in words matches the pattern.

A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.

(Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.)

Return a list of the words in words that match the given pattern. 

You may return the answer in any order.

 

 

 

Example 1:

Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"]
Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}. 
"ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation,
since a and b map to the same letter.

 

我的答案

我的思路比较粗暴,一开始我是想把word都用数字 , 比如把 “abb”写成“011”,就是定义第一个字母为0,第二个字母如果和第一个字母一样就append 一个 0,出现了第二个不同的字母就append 1, 第三个不同的就append 2.类推, 但是这个有个问题就是如果超过10个字母就会 1010会混淆。因为有26个字母,所以我就用了char来做这个事情。

class Solution {
    public List<String> findAndReplacePattern(String[] words, String pattern) {
        
        String numPattern = parseAspatternString(pattern);
        List<String> results = new ArrayList<>();
        for(String word : words) {
            if(numPattern.equals(parseAspatternString(word))) {
                results.add(word);
            }
        }
        return results;
    }
    
    private String parseAspatternString(String str) {
        char[] chars = str.toCharArray();
        StringBuilder sb = new StringBuilder();
        Map<Character, Character> map = new HashMap<>();
        for(char c : chars) {
            if(map.get(c) == null){
                char ch = (char)(map.size() + 97);
                sb.append(ch);
                map.put(c, ch);
            } else {
                sb.append(map.get(c));
            }
        }
        return sb.toString();
    }
}

 

第三题是个二叉树没看懂题目

 

第四题是个List的子序列问题,题目很粗暴就是遍历排列组合。我第一时间想到了Python。但是我只会切片操作。。不会用迭代器

                                  

 

相关文章
|
17天前
|
存储 弹性计算 人工智能
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
2025年9月24日,阿里云弹性计算团队多位产品、技术专家及服务器团队技术专家共同在【2025云栖大会】现场带来了《通用计算产品发布与行业实践》的专场论坛,本论坛聚焦弹性计算多款通用算力产品发布。同时,ECS云服务器安全能力、资源售卖模式、计算AI助手等用户体验关键环节也宣布升级,让用云更简单、更智能。海尔三翼鸟云服务负责人刘建锋先生作为特邀嘉宾,莅临现场分享了关于阿里云ECS g9i推动AIoT平台的场景落地实践。
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
|
8天前
|
云安全 人工智能 安全
Dify平台集成阿里云AI安全护栏,构建AI Runtime安全防线
阿里云 AI 安全护栏加入Dify平台,打造可信赖的 AI
|
11天前
|
人工智能 运维 Java
Spring AI Alibaba Admin 开源!以数据为中心的 Agent 开发平台
Spring AI Alibaba Admin 正式发布!一站式实现 Prompt 管理、动态热更新、评测集构建、自动化评估与全链路可观测,助力企业高效构建可信赖的 AI Agent 应用。开源共建,现已上线!
1044 34
|
11天前
|
机器学习/深度学习 人工智能 搜索推荐
万字长文深度解析最新Deep Research技术:前沿架构、核心技术与未来展望
近期发生了什么自 2025 年 2 月 OpenAI 正式发布Deep Research以来,深度研究/深度搜索(Deep Research / Deep Search)正在成为信息检索与知识工作的全新范式:系统以多步推理驱动大规模联网检索、跨源证据。
805 55
|
9天前
|
文字识别 测试技术 开发者
Qwen3-VL新成员 2B、32B来啦!更适合开发者体质
Qwen3-VL家族重磅推出2B与32B双版本,轻量高效与超强推理兼备,一模型通吃多模态与纯文本任务!
697 11
下一篇
开通oss服务