题库chevron_right分类chevron_right计数
pin

计数

149 道题目
简单: 56中等: 77困难: 16

计数 是技术面试里最常出现的能力维度之一。建议先掌握基础题型的边界处理,再逐步过渡到模式识别和复杂度 trade-off。

面试场景

高频考察问题建模、边界条件与口头表达的清晰度。

常见误区

只背模板不解释为什么,容易在追问里失分。

练习策略

每轮练 3-5 题,固定复盘复杂度和可替代解法。

推荐练习顺序

#题目难度
169

多数元素

Find the majority element in an array, where the element appears more than n/2 times, using efficient algorithms.

简单
229

多数元素 II

Identify all elements in an integer array appearing more than ⌊ n/3 ⌋ times using efficient array scanning and hash coun…

中等
299

猜数字游戏

Solve the Bulls and Cows problem using hash tables and string manipulation to track exact and misplaced digits.

中等
347

前 K 个高频元素

Find the k most frequent elements from an array using efficient algorithms like hashing and sorting.

中等
383

赎金信

Determine if a ransom note can be constructed from a magazine's letters using hash tables and string counting techniques…

简单
387

字符串中的第一个唯一字符

Find the index of the first non-repeating character in a string using efficient queue-driven state processing.

简单
451

根据字符出现频率排序

Sort Characters By Frequency requires counting characters efficiently and rearranging a string in descending frequency o…

中等
594

最长和谐子序列

Find the length of the longest harmonious subsequence in an integer array using array scanning and hash-based frequency …

简单
621

任务调度器

Task Scheduler is solved by counting task frequencies and computing how cooldown gaps force idle slots around the most f…

中等
692

前K个高频单词

Solve Top K Frequent Words by counting each word, then ordering ties alphabetically so frequency wins before lexicograph…

中等
767

重构字符串

Reorganize a string so that no two adjacent characters are the same, if possible, using a greedy approach.

中等
811

子域名访问计数

The Subdomain Visit Count problem requires counting the visits to all subdomains of a given domain from a list of count-…

中等
819

最常见的单词

Find the most frequent non-banned word in a paragraph, excluding punctuation, using an efficient array scan and hash tab…

简单
869

重新排序得到 2 的幂

Determine if a number's digits can be rearranged to form a power of two using counting and hash-based checks.

中等
884

两句话中的不常见单词

Find uncommon words from two sentences by counting word frequencies using hash tables.

简单
900

RLE 迭代器

Design an efficient iterator for a run-length encoded array, handling large counts and sequential access correctly every…

中等
914

卡牌分组

Solve the problem of determining if a deck can be partitioned into groups with equal occurrences of card values.

简单
923

三数之和的多种可能

Count all unique triplets in an integer array whose sum equals the target, handling multiplicity efficiently using hashi…

中等
945

使数组唯一的最小增量

This problem challenges you to find the minimum moves to make all elements in an array unique by incrementing elements.

中等
992

K 个不同整数的子数组

Find subarrays with exactly k distinct integers in an integer array using sliding window and hash lookup techniques.

困难
1010

总持续时间可被 60 整除的歌曲

Calculate the number of song pairs whose total durations sum to a multiple of 60 using array scanning and hash lookups.

中等
1054

距离相等的条形码

Rearrange barcodes in an array so that no two adjacent elements are equal, using a greedy approach and hash table for ef…

中等
1079

活字印刷

Compute all unique non-empty sequences from given letter tiles using backtracking search with pruning efficiently.

中等
1090

受标签影响的最大值

Maximize the sum of selected item values while respecting label use limits using array scanning and hash tracking.

中等
1128

等价多米诺骨牌对的数量

Count all pairs of dominoes that are equivalent by scanning the array and using a hash table for fast lookup.

简单
1160

拼写单词

Compute the total length of words that can be fully constructed from given characters using array scanning and hash mapp…

简单
1189

“气球” 的最大数量

Find the maximum number of times the word 'balloon' can be formed from characters of the given string.

简单
1221

分割平衡字符串

Split a string into the maximum number of balanced substrings where each substring has an equal number of 'L' and 'R'.

简单
1267

统计参与通信的服务器

Count Servers that Communicate involves identifying servers in a grid that can communicate based on row or column connec…

中等
1347

制造字母异位词的最小步骤数

Calculate the minimum steps to transform one string into an anagram of another using character replacements efficiently.

中等
1356

根据数字二进制下 1 的数目排序

Sort an array of integers by the number of 1's in their binary representation and then by their value in case of ties.

简单
1366

通过投票对团队排名

Rank Teams by Votes requires counting position-based votes and resolving ties with array scanning and hash lookup techni…

中等
1370

上升下降字符串

Reorder the string based on a specific algorithm using hash tables and string manipulation.

简单
1394

找出数组中的幸运数

Identify the largest lucky integer in an array by counting frequencies and comparing values with their occurrences effic…

简单
1400

构造 K 个回文字符串

Determine if a string's characters can be rearranged to form exactly k non-empty palindrome strings using greedy validat…

中等
1419

数青蛙

Determine the minimum number of frogs required to sequentially produce all croaks in a mixed frog croak string efficient…

中等
1481

不同整数的最少数目

Find the least number of unique integers after removing exactly k elements from an array using efficient frequency count…

中等
1497

检查数组对是否可以被 k 整除

Check if array pairs are divisible by k by pairing elements whose sums are divisible by k using array scanning and hash …

中等
1512

好数对的数目

Count all index pairs in an array where elements match and the first index is smaller, using hash-based scanning efficie…

简单
1519

子树中标签相同的节点数

Compute the number of nodes in each subtree sharing the same label using DFS with hash table aggregation efficiently.

中等
1603

设计停车系统

Implement a class to manage a parking lot with fixed slots for big, medium, and small cars, tracking occupancy efficient…

简单
1657

确定两个字符串是否接近

Check if two strings can transform into each other using letter swaps and frequency reorganizations, leveraging hash tab…

中等
1684

统计一致字符串的数目

Count the number of strings fully composed of allowed characters using array scanning and hash lookup for efficiency.

简单
1704

判断字符串的两半是否相似

Check if two halves of a string contain the same number of vowels using a character counting approach efficiently.

简单
1726

同积元组

Given an array of distinct integers, return the number of tuples where the product of two pairs is the same.

中等
1737

满足三条件之一需改变的最少字符数

This problem asks for the minimum operations to change two strings so that one of three conditions is met.

中等
1742

盒子中小球的最大数量

The problem asks you to find the box with the maximum number of balls based on the sum of digits of ball numbers.

简单
1748

唯一元素的和

Given an array, find the sum of all its unique elements by identifying those that appear only once.

简单
1775

通过最少操作次数使数组的和相等

Solve the problem of balancing the sums of two integer arrays with minimal operations, using array scanning and hash loo…

中等
1781

所有子字符串美丽值之和

Calculate the total beauty of all substrings by counting character frequencies and computing frequency differences effic…

中等
1782

统计点对的数目

Given a graph with nodes and edges, count pairs of nodes where the degree sum exceeds a given threshold for each query.

困难
1790

仅执行一次字符串交换能否使两个字符串相等

Check if one string swap can make two equal strings using hash table and string counting methods.

简单
1814

统计一个数组中好对子的数目

Count Nice Pairs in an Array requires efficiently pairing numbers where nums[i] + rev(nums[j]) equals nums[j] + rev(nums…

中等
1819

序列中不同最大公约数的数目

Given an array of positive integers, find the number of different subsequences' GCDs.

困难
1854

人口最多的年份

Find the earliest year with the highest population using an array plus counting approach.

简单
1857

有向图中最大颜色值

Compute the maximum color frequency along any valid path in a directed graph using topological ordering and dynamic prog…

困难
1876

长度为三且各字符不同的子字符串

Count all substrings of length three in a string where each character is unique using sliding window techniques efficien…

简单
1897

重新分配字符使所有字符串都相等

Determine if you can redistribute characters among strings so that all strings become identical using a counting approac…

简单
1941

检查是否所有字符出现次数相同

Check if a string has characters with equal occurrences using hash tables and string manipulation techniques.

简单
1994

好子集的数目

Find the number of good subsets in an integer array, where each subset's product is the product of distinct primes.

困难
2001

可互换矩形的组数

Count all pairs of rectangles that share the exact width-to-height ratio using efficient array scanning and hash lookup.

中等
2006

差的绝对值为 K 的数对数目

Find how many pairs (i, j) with i < j satisfy |nums[i] - nums[j]| == k using array scanning and hash lookup.

简单
2013

检测正方形

Detect Squares requires tracking points on a 2D plane to quickly count all possible axis-aligned squares using efficient…

中等
2014

重复 K 次的最长子序列

Find the longest subsequence repeated k times in a string using backtracking search with pruning.

困难
2023

连接后等于目标字符串的字符串对

Count all unique index pairs in a string array whose concatenation exactly matches the given target string using efficie…

中等
2025

分割数组的最多方案数

Determine the maximum number of ways to split an array into equal sums using at most one element change, leveraging pref…

困难
2029

石子游戏 IX

In the Stone Game IX problem, Alice and Bob take turns removing stones, and Alice wins if the sum of removed stones is d…

中等
2048

下一个更大的数值平衡数

Find the smallest numerically balanced number greater than a given integer using backtracking search and pruning.

中等
2053

数组中第 K 个独一无二的字符串

Find the kth distinct string in an array by identifying strings that appear only once, using efficient array scanning an…

简单
2068

检查两个字符串是否几乎相等

Solve Check Whether Two Strings are Almost Equivalent by counting letters and rejecting any character whose frequency ga…

简单
2085

统计出现过一次的公共字符串

Learn how to efficiently count strings appearing exactly once in both arrays using array scanning and hash lookup patter…

简单
2131

连接两字母单词得到的最长回文串

Find the maximum-length palindrome by combining two-letter words using array scanning and hash table lookups efficiently…

中等
2148

元素计数

Count elements in an array that have both strictly smaller and strictly greater numbers using array sorting efficiently.

简单
2150

找出数组中的所有孤独数字

Find lonely numbers in an array where each lonely number appears once and has no adjacent values.

中等
2170

使数组变成交替数组的最少操作数

Given an array, calculate the minimum number of operations needed to make it alternating.

中等
2182

构造限制重复的字符串

Construct a lexicographically largest string from a given string with no letter appearing more than a repeatLimit times …

中等
2186

制造字母异位词的最小步骤数 II

Compute the fewest character insertions needed to turn two strings into anagrams using hash table frequency counts effic…

中等
2190

数组中紧跟 key 之后出现最频繁的数字

Scan the array once, count which value appears right after the key, and return the uniquely most frequent follower.

简单
2206

将数组划分成相等数对

Determine if an array of 2n integers can be partitioned into n pairs where each pair contains identical elements using h…

简单
2225

找出输掉零场或一场比赛的玩家

Identify players with zero or one losses by efficiently scanning arrays and counting losses using hash lookups for accur…

中等
2244

完成所有任务需要的最少轮数

The problem requires finding the minimum rounds to complete tasks, focusing on greedy algorithms and hash table lookups.

中等
2248

多个数组求交集

Find integers that are common across all arrays in a given list of arrays.

简单
2275

按位与结果大于零的最长组合

Find the largest group of integers in an array whose bitwise AND is greater than zero using array scanning and hash look…

中等
2283

判断一个数的数字计数是否等于数位的值

Determine if a number's digits correspond to their counts in the string representation.

简单
2284

最多单词数的发件人

Find the sender with the largest total word count by scanning messages and tallying counts using a hash table efficientl…

中等
2287

重排字符形成目标字符串

Calculate the maximum copies of a target string from letters in s using frequency counting and hash tables efficiently.

简单
2341

数组能形成多少数对

Given an array, count how many pairs can be formed and how many leftovers remain after pairing.

简单
2347

最好的扑克手牌

Determine the strongest poker hand from five cards using array scanning and hash table counting techniques efficiently.

简单
2351

第一个出现两次的字母

Find the first letter that repeats in a string using hash table tracking and early detection for efficiency.

简单
2364

统计坏数对的数目

Count the number of bad pairs in an array where the difference between indices and values does not match.

中等
2384

最大回文数字

Form the largest palindromic number from a string of digits while maintaining a valid palindrome structure.

中等
2404

出现最频繁的偶数元素

Identify the most frequent even element in an array using counting and hash lookup, handling ties by choosing the smalle…

简单
2416

字符串的前缀分数和

The 'Sum of Prefix Scores of Strings' problem involves calculating prefix scores for strings based on their occurrences …

困难
2423

删除字符使频率相同

Determine if removing a single letter from a string can make all remaining letters have identical frequency efficiently.

简单
2442

反转之后不同整数的数目

This problem requires counting distinct integers after performing reverse operations on each integer in an array.

中等
2453

摧毁一系列目标

Find the minimum seed value to destroy the most targets on a number line, using array scanning and hash lookup.

中等
2499

让数组不相等的最小总代价

Calculate the minimum cost to rearrange nums1 so that no element matches nums2 using optimal swaps and hash counting.

困难
2506

统计相似字符串对的数目

Count similar string pairs by converting each word into a character-set signature and counting matching signatures effic…

简单
2514

统计同位异构字符串数目

Learn to count distinct anagrams for a multi-word string using hash tables, math, and combinatorics efficiently.

困难
2526

找到数据流中的连续整数

Design a data stream checker to identify consecutive integers from a stream, focusing on handling state transitions effi…

中等
2529

正整数和负整数的最大计数

Compute the maximum count between positive and negative integers in a sorted array using efficient binary search countin…

简单
2531

使字符串中不同字符的数目相等

Determine if a single swap can equalize distinct character counts between two strings using hash tables for tracking fre…

中等
2547

拆分数组的最小代价

Minimize the cost of splitting an array into k subarrays by calculating importance values and applying dynamic programmi…

困难
2586

统计范围内的元音字符串数

Count the Number of Vowel Strings in Range asks to count vowel strings in a subarray of a given word list.

简单
2748

美丽下标对的数目

Count all index pairs in an array where the first digit of one number and last digit of another are coprime efficiently.

简单
2833

距离原点最远的点

Determine the maximum distance from origin using string moves and counting underscores optimally for efficient calculati…

简单
2856

删除数对后的最小数组长度

This problem involves minimizing the length of a sorted array by repeatedly removing adjacent pairs of equal elements.

中等
2870

使数组为空的最少操作次数

Minimize the number of operations to make an array empty by leveraging array scanning and hash lookup.

中等
2960

统计已测试设备

Simulate testing devices based on battery percentages to determine how many pass the test operations in sequence.

简单
2981

找出出现至少三次的最长特殊子字符串 I

Find the longest special substring in a string that appears at least three times, or return -1 if no such substring exis…

中等
2982

找出出现至少三次的最长特殊子字符串 II

Find the longest special substring that occurs at least three times in a given string using binary search and hash table…

中等
3005

最大频率元素计数

Count Elements With Maximum Frequency is solved by counting each value, tracking the highest count, and summing matching…

简单
3016

输入单词需要的最少按键次数 II

Given a word, find the minimum number of pushes to type it on a remapped keypad using a greedy approach.

中等
3035

回文字符串的最大数量

The problem focuses on maximizing the number of palindromes that can be formed from a given list of words through specif…

中等
3039

进行操作使字符串为空

Learn how to systematically apply operations on a string using array scanning and hash lookups to reduce it efficiently.

中等
3044

出现频率最高的质数

Find the most frequent prime over 10 from numbers generated by scanning a 2D matrix in all straight directions efficient…

中等
3046

分割数组

Determine if an even-length array can be split into two parts with all distinct elements using hash lookup scanning.

简单
3071

在矩阵上写出字母 Y 所需的最少操作次数

Find the minimum number of operations to write the letter Y on a grid by altering cell values.

中等
3081

替换字符串中的问号使分数最小

Minimize the cost of a string with '?' characters by replacing them with letters in lexicographical order while minimizi…

中等
3084

统计以给定字符开头和结尾的子字符串总数

Given a string and a character, find the total number of substrings that start and end with that character.

中等
3085

成为 K 特殊字符串需要删除的最少字符数

Minimize deletions to make a string k-special by adjusting character frequencies.

中等
3128

直角三角形

Count all possible right triangles in a 2D boolean grid using array scanning and hash lookup for efficiency.

中等
3137

K 周期字符串需要的最少操作次数

The problem requires calculating the minimum number of operations to make a string k-periodic using specific substring r…

中等
3138

同位字符串连接的最小长度

The problem asks to find the minimum length of a string t such that s is a concatenation of anagrams of t.

中等
3144

分割字符频率相等的最少子字符串

Partition a string into substrings with equal character frequencies using dynamic programming and state transitions.

中等
3153

所有数对中数位差之和

Sum of Digit Differences of All Pairs is a problem that involves counting digit differences in pairs from an array of in…

中等
3184

构成整天的下标对数目 I

Count all pairs in an array where their sum forms a complete day using hash-based counting for efficiency.

简单
3185

构成整天的下标对数目 II

Count the number of valid pairs of hours that form a complete day by checking if their sum is a multiple of 24.

中等
3186

施咒的最大总伤害

Calculate the maximum total damage by selectively casting spells while avoiding adjacent power conflicts using array sca…

中等
3223

操作后字符串的最短长度

Find the minimum length of a string after performing operations based on character frequency.

中等
3228

将 1 移动到末尾的最大操作次数

This problem requires finding the maximum number of operations to move ones to the end of a binary string.

中等
3238

求出胜利玩家的数目

Find how many players in a game win by picking more balls of a single color than their index position.

简单
3265

统计近似相等数对 I

Count Almost Equal Pairs I involves finding pairs of elements that can be made equal by swapping at most one digit.

中等
3267

统计近似相等数对 II

Count the number of almost equal integer pairs in an array using array scanning and hash lookup efficiently.

困难
3312

查询排序后的最大公约数

Solve the Sorted GCD Pair Queries problem by efficiently counting and locating GCDs of all array pairs using hash mappin…

困难
3335

字符串转换后的长度 I

Calculate the total number of characters in a string after repeated transformations using dynamic programming and freque…

中等
3337

字符串转换后的长度 II

Calculate the length of a string after repeated transformations using state transition dynamic programming for large t v…

困难
3371

识别数组中的最大异常值

Identify the largest outlier in an integer array using scanning and hash lookup for efficient detection and validation.

中等
3389

使字符频率相等的最少操作次数

This Hard problem asks to transform a string so all character frequencies match using minimal deletions, leveraging dyna…

困难
3438

找到字符串中合法的相邻数字

Identify the first valid adjacent digit pair in a string where each digit appears exactly as many times as its numeric v…

简单
3442

奇偶频次间的最大差值 I

Find the maximum difference between the frequency of characters in a string with odd and even frequencies.

简单
3443

K 次修改后的最大曼哈顿距离

Solve Maximum Manhattan Distance After K Changes by scanning prefixes and testing the four diagonal target pairs with li…

中等
3467

将数组按照奇偶性转化

Transform an array by sorting even numbers first, followed by odd numbers.

简单
3518

最小回文排列 II

Find the k-th lexicographically smallest palindromic rearrangement of a given palindromic string s.

困难
3527

找到最常见的回答

Find the most common survey response after eliminating duplicates within each day, using array scanning and hash lookups…

中等
3541

找到频率最高的元音和辅音

Given a string, calculate the sum of the most frequent vowel and consonant frequencies.

简单
3545

不同字符数量最多为 K 时的最少删除数

You need to delete characters in a string to reduce its distinct characters to at most k.

简单
3583

统计特殊三元组

Count Special Triplets requires scanning an array while tracking counts of previous and next values efficiently with has…

中等
3591

检查元素频次是否为质数

Check if any element in the array has a prime frequency count, leveraging array scanning and hash table lookup.

简单

关联高频模式

LeetCode 计数题型题解:149题训练路线