力扣:环于杆,简单题

#include <iostream>#include <vector> using namespace std; class Solution {public: static constexpr int POLE_NUM = 10; static constexpr int COLOR_NUM = 3; int getColorId(char color) { if (color == 'R')...

2103. 环和杆 --力扣 --JAVA

题目 解题思路 通过遍历收集每个杆上存在的颜色种类;选择存储数据的容器,Map加Set进行收集和去重; 代码展示 class Solution { public int countPoints(String rings) { int sum = 0; Map<Integer, Set<Character>> data = new HashMap<>(); for (int i = 1; i < rin...

274. H 指数 --力扣 --JAVA

题目 解题思路 对数组进行排序,从大到小开始进行统计;当引用次数大于统计的文章数时,就作为一个结果进行返回;返回最大的结果值。 代码展示 class Solution { public int hIndex(int[] citations) { Arrays.sort(citations); int count = 0; int min = Integer.MAX_VALUE; int ans = 0...

275. H 指数 II --力扣 --JAVA

题目 解题思路 数组是升序排列的,所以可以从后向前遍历;遍历过的元素肯定大于等于当前元素的值,遍历过的元素数量为n - i;只要当前元素的值大于等于遍历过的元素数量,既可作为结果之一;选择最后结果最大值输出。 代码展示 class Solution { public int hIndex(int[] citations) { int ans = 0; int n = citations.length;...

41. 缺失的第一个正数 --力扣 --JAVA

题目 解题思路 对数组进行排序,便于查看是否连续;因为是最小正整数,所以判断值应从1开始;只要当前元素值大于最小值,则直接返回最小值。 代码展示 class Solution { public int firstMissingPositive(int[] nums) { Arrays.sort(nums); int min = 1; for (int i = 0; i < nums.length; i...

73. 矩阵置零 --力扣 --JAVA

题目 解题思路 通过二层循环找出元素为0所在的行和列;设置标志位记录当前行是否存在元素为0的,设置列表存储列为0的列;在内循环结束后对存在元素为0的行,填充为0;遍历列表,将列置为0; 代码展示 class Solution { public void setZeroes(int[][] matrix) { Set<Integer> row = new HashSet<>(); for (int i ...

2520. 统计能整除数字的位数 --力扣 --JAVA

题目 解题思路 将整数num转换成字符串并读取每位字符进行运算 代码展示 class Solution { public int countDigits(int num) { int count = 0; String str = num + ""; for (int i = 0; i < str.length(); i++){ if(num % (str.charAt(i) - 48) == 0) ...

14.力扣c++刷题-->有效括号

题目:给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串 s ,判断字符串是否有效。 有效字符串需满足: 左括号必须用相同类型的右括号闭合。 左括号必须以正确的顺序闭合。 每个右括号都有一个对应的相同类型的左括号。 #include<iostream>#include<vector>#include<map>#include<unordered_map>#include...

力扣:有效的括号

自己编写的代码 。 自己的思路: class Solution {private: unordered_map<char,int>symbolValues={ {'(',1}, {')',2}, {'{',4}, {'}',5}, {'[',8}, {']',9}, }; public: bool isValid(string s) { bool flag=false; int lens=s.le...

56. 合并区间 --力扣 --JAVA

题目 解题思路 根据区间的起始点对区间进行排序排序后对交叉区间进行合并,添加新的间隔区间当前区间只需要考虑与上一个存储区间的关系 代码展示 class Solution { public int[][] merge(int[][] intervals) { if(intervals.length == 0) return intervals; Map<Integer,Integer> data = n...
© 2024 LMLPHP 关于我们 联系我们 友情链接 耗时0.002617(s)
2024-05-02 16:26:56 1714638416