牛客网--关于数组中重复的数字

本文详细解析了牛客网上一道关于寻找数组中重复数字的题目,通过使用Java的HashMap数据结构,实现了一个有效的方法来找出数组中任意一个重复的数字。以数组{2,3,1,0,2,5,3}

牛客网--关于数组中重复的数字

题目描述

在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。

代码

import java.util.*;
public class Solution {
    // Parameters:
    //    numbers:     an array of integers
    //    length:      the length of array numbers
    //    duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
    //                  Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
    //    这里要特别注意~返回任意重复的一个,赋值duplication[0]
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    public boolean duplicate(int numbers[],int length,int [] duplication) {
        if(numbers == null || numbers.length == 0)
        {
            duplication[0] = -1;
            return false;
        }
        
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < numbers.length; i++)
        {
            if(map.containsKey(numbers[i]))
            {
                duplication[0] = numbers[i];
                return true;
            }
            else
            {
                map.put(numbers[i], 1);
            }
        }
        return false;
    }
}
计算包含 `k` 种不同字母的子序列数量是一个典型的组合问题,通常出现在字符串处理与动态规划的场景中。该问题要求从一个字符串中统计出所有恰好包含 `k` 种不同字母的子序列的个数。子序列定义为从原字符串中删除若干字符(可以不连续)后形成的序列。 ### 解题思路 1. **滑动窗口 + 双指针** 滑动窗口法是解决此类问题的一种高效方法,尤其适用于统计满足特定条件的子串或子序列数量。我们可以维护一个窗口 `[left, right]`,并使用哈希表或数组统计窗口内不同字符的种类数。 2. **组合计数优化** 由于子序列可以是不连续的,因此在滑动窗口的基础上,可以使用组合数学的方法来统计每个满足条件的窗口对应的子序列数量。 3. **时间复杂度** 该方法的时间复杂度为 `O(n)`,适用于处理较长的字符串。 ### 示例代码(C++) 以下是一个基于滑动窗口和组合数学的实现,用于统计包含 `k` 种不同字母的子序列数量: ```cpp #include <iostream> #include <vector> #include <unordered_map> using namespace std; long long countSubsequencesWithKChars(const string& s, int k) { int n = s.size(); vector<long long> dp(n + 1, 0); dp[0] = 1; // 空子序列 unordered_map<char, int> last_pos; vector<long long> sum(n + 1, 0); sum[0] = 1; for (int i = 1; i <= n; ++i) { dp[i] = 2 * dp[i - 1]; if (last_pos.count(s[i - 1])) { int prev = last_pos[s[i - 1]]; dp[i] -= dp[prev - 1]; } last_pos[s[i - 1]] = i; sum[i] = dp[i]; } // 统计恰好包含 k 种字符的子序列数量 vector<int> freq(26, 0); int unique = 0; long long res = 0; int left = 0; for (int right = 0; right < n; ++right) { if (freq[s[right] - 'a'] == 0) { unique++; } freq[s[right] - 'a']++; while (unique > k) { freq[s[left] - 'a']--; if (freq[s[left] - 'a'] == 0) { unique--; } left++; } if (unique == k) { res += 1LL << (right - left + 1); res -= 1; // 减去空子序列 } } return res; } int main() { string s; int k; cin >> s >> k; cout << countSubsequencesWithKChars(s, k) << endl; return 0; } ``` ### 说明 - 上述代码分为两个部分: - 第一部分使用动态规划计算所有子集的子序列数量。 - 第二部分使用滑动窗口统计恰好包含 `k` 种字符的子序列数量。 - 时间复杂度为 `O(n)`,空间复杂度为 `O(n)`,适用于大多数牛客网题目要求。 ### 相关问题 1. 如何计算包含 `k` 种不同数字的子数组数量? 2. 如何优化动态规划中的空间复杂度? 3. 滑动窗口法在字符串处理中的典型应用场景有哪些? 4. 如何处理包含重复字符的子序列统计问题? 5. 如何扩展该算法以支持包含 `k` 到 `m` 种字符的子序列统计?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值