LeetCode第215场周赛

第 215 场周赛

目录:

题目:设计有序流

有 n 个 (id, value) 对,其中 id 是 1 到 n 之间的一个整数,value 是一个字符串。不存在 id 相同的两个 (id, value) 对。

设计一个流,以 任意 顺序获取 n 个 (id, value) 对,并在多次调用时 按 id 递增的顺序 返回一些值。

实现 OrderedStream 类:

  • OrderedStream(int n) 构造一个能接收 n 个值的流,并将当前指针 ptr 设为 1 。
  • String[] insert(int id, String value) 向流中存储新的 (id, value) 对。存储后:
    • 如果流存储有 id = ptr 的 (id, value) 对,则找出从 id = ptr 开始的 最长 id 连续递增序列 ,并 按顺序 返回与这些 id 关联的值的列表。然后,将 ptr 更新为最后那个 id + 1 。
      否则,返回一个空列表。

示例:

输入
[“OrderedStream”, “insert”, “insert”, “insert”, “insert”, “insert”]
[[5], [3, “ccccc”], [1, “aaaaa”], [2, “bbbbb”], [5, “eeeee”], [4, “ddddd”]]
输出
[null, [], [“aaaaa”], [“bbbbb”, “ccccc”], [], [“ddddd”, “eeeee”]]

解释
OrderedStream os= new OrderedStream(5);
os.insert(3, “ccccc”); // 插入 (3, “ccccc”),返回 []
os.insert(1, “aaaaa”); // 插入 (1, “aaaaa”),返回 [“aaaaa”]
os.insert(2, “bbbbb”); // 插入 (2, “bbbbb”),返回 [“bbbbb”, “ccccc”]
os.insert(5, “eeeee”); // 插入 (5, “eeeee”),返回 []
os.insert(4, “ddddd”); // 插入 (4, “ddddd”),返回 [“ddddd”, “eeeee”]

提示:

1 <= n <= 1000
1 <= id <= n
value.length == 5
value 仅由小写字母组成
每次调用 insert 都会使用一个唯一的 id
恰好调用 n 次 insert

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/design-an-ordered-stream
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路:

用一个长度为n的数组将输入的(id, value)对保存起来,每次插入的时候检查id是否等于ptr,如果相等就遍历数组找到一段需要的结果,因为数组所以从前往后遍历id是递增的.

class OrderedStream {
    int ptr;
    String[] node;
    int len;

    public OrderedStream(int n) {
        ptr = 0;
        len = n;
        node = new String[n];
    }

    public List<String> insert(int id, String value) {
        node[id - 1] = value;
        List<String> res = new LinkedList<>();
        while(ptr < len && node[ptr] != null) res.add(node[ptr++]);
        return res;
    }
}

题目:确定两个字符串是否接近

如果可以使用以下操作从一个字符串得到另一个字符串,则认为两个字符串 接近 :

  • 操作 1:交换任意两个 现有 字符。

    • 例如,abcde -> aecdb
  • 操作 2:将一个 现有 字符的每次出现转换为另一个 现有 字符,并对另一个字符执行相同的操作。

    • 例如,aacabb -> bbcbaa(所有 a 转化为 b ,而所有的 b 转换为 a )

你可以根据需要对任意一个字符串多次使用这两种操作。

给你两个字符串,word1 和 word2 。如果 word1 和 word2 接近 ,就返回 true ;否则,返回 false 。

示例 1:

输入:word1 = "abc", word2 = "bca"
输出:true
解释:2 次操作从 word1 获得 word2 。
执行操作 1:"abc" -> "acb"
执行操作 1:"acb" -> "bca"

示例 2:

输入:word1 = "a", word2 = "aa"
输出:false
解释:不管执行多少次操作,都无法从 word1 得到 word2 ,反之亦然。

示例 3:

输入:word1 = "cabbba", word2 = "abbccc"
输出:true
解释:3 次操作从 word1 获得 word2 。
执行操作 1:"cabbba" -> "caabbb"
执行操作 2:"caabbb" -> "baaccc"
执行操作 2:"baaccc" -> "abbccc"

示例 4:

输入:word1 = "cabbba", word2 = "aabbss"
输出:false
解释:不管执行多少次操作,都无法从 word1 得到 word2 ,反之亦然。

提示:

  • 1 <= word1.length, word2.length <= 105
  • word1 和 word2 仅包含小写英文字母

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/determine-if-two-strings-are-close
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路:

  • 操作 1:交换任意两个现有字符。

    可以任意交换两个现有字符,表示对于任意两个字符只要他们排序后相等,我们就可通过一个字符串得到另一个字符串.

  • 操作 2:将一个 现有 字符的每次出现转换为另一个 现有 字符,并对另一个字符执行相同的操作。

    对于操作2,可以理解为,我们可以交换任意两个字符出现的次数.

题意问我们,是否可以通过以上两个操作,从一个字符串得到(接近)(可以直接理解为等于)另一个字符串.

因为操作2,对两个字符串,我们需统计每个字符出现的次数是否一样,不需要字符与字符之间对应 列如:s1(aab) s2(bba) 我们可以交换a b 出现的次数.但s1 与 s2 中出现的字符集必须相同,也就s1中所有的字符s2中必须有,s2中的所有字符s1中也必须有.因为操作1我们不用考虑两个字符串的顺序.

class Solution {
    public boolean closeStrings(String word1, String word2) {
        int n1 = word1.length();
        int n2 = word2.length();
        if(n1 != n2) return false;
        int[] h1 = new int[26];
        int[] h2 = new int[26];
        boolean[] h3 = new boolean[26];
        boolean[] h4 = new boolean[26];
        for(int i = 0; i < n1; i++) {
            h1[word1.charAt(i) - 'a']++;
            h3[word1.charAt(i) - 'a'] = true;
        }
        for(int i = 0; i < n2; i++) {
            h2[word2.charAt(i) - 'a']++;
            h4[word2.charAt(i) - 'a'] = true;
        }

        for(int i = 0; i < 26; i++) {
            if(h3[i] != h4[i]) return false;
        }
        Arrays.sort(h1);
        Arrays.sort(h2);
        for(int i = 0; i < 26; i++) {
            if(h1[i] != h2[i]) return false;
        }
        return true;
    }
}

题目:将 x 减到 0 的最小操作数

给你一个整数数组 nums 和一个整数 x 。每一次操作时,你应当移除数组 nums 最左边或最右边的元素,然后从 x 中减去该元素的值。请注意,需要 修改 数组以供接下来的操作使用。

如果可以将 x 恰好 减到 0 ,返回 最小操作数 ;否则,返回 -1 。

示例 1:

输入:nums = [1,1,4,2,3], x = 5
输出:2
解释:最佳解决方案是移除后两个元素,将 x 减到 0 。
示例 2:

输入:nums = [5,6,7,8,9], x = 4
输出:-1
示例 3:

输入:nums = [3,2,20,1,1,3], x = 10
输出:5
解释:最佳解决方案是移除后三个元素和前两个元素(总共 5 次操作),将 x 减到 0 。

提示:

1 <= nums.length <= 105
1 <= nums[i] <= 104
1 <= x <= 109

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-operations-to-reduce-x-to-zero
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路:

哈希+前缀后缀和

先求的数组的前缀和,用个哈希表存起来,然后求数组的后缀和,然后在哈希中查找是否有前缀+后缀等于需要求的值.需注意操作数不可超过数组的长度.


class Solution {
    public int minOperations(int[] nums, int x) {
        int ans = Integer.MAX_VALUE;
        int n = nums.length;

        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(0, 0);
        int sum = 0;
        for(int i = 0; i < n; i++){
            sum += nums[i];
            map.put(sum, i + 1);
        }

        if(map.containsKey(x)) ans = Math.min(ans, map.get(x));
        sum = 0;
        for(int i = n - 1; i >= 0; i--){
            sum += nums[i];
            if(map.containsKey(x - sum)) {
                int cur = n - i + map.get(x - sum);
                if(cur > n) continue;
                ans = Math.min(ans, cur);
            }
        }
        return ans == Integer.MAX_VALUE ? -1 : ans;
    }
}

双指针

我们可以先求的得整个数组的和,然后在数组中找出一段等于数组和减去目标值的最长的一段就得到结果.

在这里插入图片描述

class Solution {
    public int minOperations(int[] nums, int x) {
        int sum = 0;
        int n = nums.length;
        for (int i = 0; i < n; i++) sum += nums[i];
        if (sum < x) return -1;

        int start = 0;
        int target = sum - x;
        int curSum = 0;
        int ans = -1;
        for (int i = 0; i < n; i++) {
            curSum += nums[i];
            while (start <= i && curSum > target) {
                curSum -= nums[start];
                start++;
            }
            if(curSum == target) ans = Math.max(ans, i - start + 1);
        }

        return ans == -1 ? -1 : n - ans;
    }
}
um += nums[i];
            while (start <= i && curSum > target) {
                curSum -= nums[start];
                start++;
            }
            if(curSum == target) ans = Math.max(ans, i - start + 1);
        }

        return ans == -1 ? -1 : n - ans;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值