H指数 10.7
给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数。计算并返回该研究者的 h 指数。
根据维基百科上 h 指数的定义:h 代表“高引用次数” ,一名科研人员的 h 指数 是指他(她)至少发表了 h 篇论文,并且每篇论文 至少 被引用 h 次。如果 h 有多种可能的值,h 指数 是其中最大的那个。
示例 1:输入:citations = [3,0,6,1,5] 输出:3
解释:给定数组表示研究者总共有 5篇论文,每篇论文相应的被引用了 3, 0, 6, 1, 5次。由于研究者有 3 篇论文每篇 至少 被引用了 3
次,其余两篇论文每篇被引用 不多于 3 次,所以她的 h 指数是 3。
示例 2:输入:citations = [1,3,1] 输出:1
- class Solution {
- public int hIndex(int[] citations) {
- Arrays.sort(citations);
- for(int i=0;i
- {
- if(citations[i]>=citations.length-i)
- {
- return Math.min(citations[i],citations.length-i);
- }
- }
- return 0;
- }
- }
该解决方案首先对传入的引用次数组进行排序。然后,通过遍历排序后的数组,从最低引用次开始,检查是否存在至少有citations.size() - i次引用的论文数量。如果存在,则返回当前引用次和剩余未遍历的论文数量中较小的那个值作为H指数。如果不满足条件,则返回0。
实现RandomizedSet 类:
RandomizedSet() 初始化 RandomizedSet 对象bool insert(int val) 当元素 val 不存在时,向集合中插入该项,并返回 true ;否则,返回 false 。bool remove(int val) 当元素 val 存在时,从集合中移除该项,并返回 true ;否则,返回 false 。int getRandom() 随机返回现有集合中的一项(测试用例保证调用此方法时集合中至少存在一个元素)。每个元素应该有 相同的概率 被返回。
你必须实现类的所有函数,并满足每个函数的 平均 时间复杂度为 O(1) 。
- class RandomizedSet {
- ArrayList
list; - public RandomizedSet() {
- this.list=new ArrayList<>();
- }
-
- public boolean insert(int val) {
- if(list.contains(val))return false;
- list.add(val);
- return true;
- }
-
- public boolean remove(int val) {
- if(list.contains(val))
- {
- list.remove((Integer) val);
- return true;
- }
- return false;
-
- }
-
- public int getRandom() {
- Random random=new Random();
- int x=random.nextInt(list.size());
- return list.get(x);
- }
- }
此题实现很简单,有个雷,就是remove方法,要类型转换,不然删除的就是下标为val的值而不是元素val,淦!ex!又是想念c++的一天~
对列表排序(暴力做法)(想念c++的第10086天~呜呜呜)
- class Solution {
- public List
> kSmallestPairs(int[] nums1, int[] nums2, int k) {
- List
> arr=new ArrayList<>();
- if(nums1.length==0||nums2.length==0||k==0)return arr;
- PriorityQueue
minHeap = new PriorityQueue<>(); - for(int i=0;i
- {
- for(int j=0;j
- {
- minHeap.offer(new Nums(nums1[i],nums2[j]));
- }
- }
- while(k-->0&&!minHeap.isEmpty())
- {
- Nums c=minHeap.poll();
- List
list=new ArrayList<>(); - list.add(c.a);
- list.add(c.b);
- arr.add(list);
- }
- return arr;
- }
- }
- class Nums implements Comparable
{ - int a,b,sum=0;
- public Nums(int a,int b)
- {
- this.a=a;
- this.b=b;
- this.sum=a+b;
- }
-
- @Override
- public int compareTo(Nums o) {
- return this.sum-o.sum;
- }
- }
堆优化+lamdba表达式
- class Solution {
- public List
> kSmallestPairs(int[] nums1, int[] nums2, int k) {
- List
> arr=new ArrayList<>();
- if(nums1.length==0||nums2.length==0||k==0)return arr;
- PriorityQueue<int[]> minHeap = new PriorityQueue<int[]>((a, b) -> a[0] - b[0]);
- for (int i = 0; i < Math.min(nums1.length, k); i++) {
- minHeap.offer(new int[]{nums1[i]+nums2[0],i,0});
- }
- while(k>0&&!minHeap.isEmpty())
- {
- int c[]=minHeap.poll();
- List
list=new ArrayList<>(); - if (c[2]+1
- minHeap.offer(new int[]{nums1[c[1]]+nums2[c[2]+1],c[1],c[2]+1});
- list.add(nums1[c[1]]);
- list.add(nums2[c[2]]);
- arr.add(list);
- k--;
- }
- return arr;
- }
- }
- import java.util.Arrays;
-
- class Solution {
- public int sumDistance(int[] nums, String s, int d) {
- long sum[]=new long[nums.length];
- final int MOD=1000000007;
- for(int i=0;i
- {
- if(s.charAt(i)=='R')sum[i]=nums[i]+d;
- else sum[i]=nums[i]-d;
- }
- Arrays.sort(sum);
- long res = 0;
- for (int i = 1; i < nums.length; i++) {
- res += 1L * (sum[i] - sum[i - 1]) * i % MOD * (nums.length - i) % MOD;
- res %= MOD;
- }
- return (int)res;
- }
- }
-
- //R: +1
- //L: -1
除自身以外数组的乘积 10/11
- import java.util.Arrays;
-
- class Solution {
- public int[] productExceptSelf(int[] nums) {
- int ansPre[]=new int[nums.length];
- int ansLas[]=new int[nums.length];
- int ans[]=new int[nums.length];
- ansPre[0]=1;
- ansLas[0]=1;
- int res=1;
- for(int i=1;i< nums.length;i++)
- {
- ansPre[i]=nums[i-1]*ansPre[i-1];
- ansLas[i]=nums[nums.length-i]*ansLas[i-1];
- }
- for(int i=0;i< nums.length;i++)
- {
- ans[i]=ansPre[i]*ansLas[nums.length-i-1];
- }
- return ans;
- }
- }
奖励最顶尖的K名学生 (废物方法)
- import java.util.*;
- class Solution {
- public List
topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) { - List
id = new ArrayList<>(); - PriorityQueue
> stu = new PriorityQueue<>((o1, o2) -> { - if (o1.getValue() > o2.getValue()){
- return -1;
- }
- else if (o1.getValue() < o2.getValue()) {
- return 1;
- } else {
- if (o1.getKey() > o2.getKey()) {
- return 1;
- } else return -1;
- }
- });
- Set
pf=new HashSet<>(Arrays.asList(positive_feedback)); - Set
nf=new HashSet<>(Arrays.asList(negative_feedback)); - int score[] = new int[student_id.length];
- for (int i = 0; i < report.length; i++) {
- String s = report[i];
- String ns[]=s.split(" ");
- for (int j=0;j< ns.length;j++) {
- if (pf.contains(ns[j])) score[i] += 3;
- if (nf.contains(ns[j])) score[i] -= 1;
- }
- }
- for (int i = 0; i < report.length; i++) {
- AbstractMap.SimpleEntry pair = new AbstractMap.SimpleEntry(student_id[i], score[i]);
- stu.offer(pair);
- }
- System.out.println(Arrays.toString(score));
- for (int i = 0; i < k; i++) {
- int pid = stu.poll().getKey();
- id.add(pid);
- }
- return id;
- }
-
- }
找出数组串联值 10/12(简单题)
- import java.util.ArrayList;
-
- import java.util.Arrays;
-
- import java.util.LinkedList;
-
-
-
- class Solution {
-
- public long findTheArrayConcVal(int[] nums) {
-
- long ans=0;
-
- if(nums.length==0)return 0;
-
- if(nums.length==1)return nums[0];
-
- LinkedList
linkedList=new LinkedList<>(); -
- for(int i=0;i
-
- while(!linkedList.isEmpty()&&linkedList.size()>=2)
-
- {
-
- int pre=linkedList.pollFirst();
-
- int last=linkedList.pollLast();
-
- ans+=last;
-
- int cnt=0;
-
- while(last>0)
-
- {
-
- last/=10;
-
- cnt++;
-
- }
-
- ans+=pre*Math.pow(10,cnt);
-
- }
-
- if(!linkedList.isEmpty())ans+=linkedList.poll();
-
- return ans;
-
- }
-
- }
10/13避免洪水泛滥
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.TreeSet;
-
- class Solution {
- public int[] avoidFlood(int[] rains) {
- int ans[]=new int[rains.length];
- Arrays.fill(ans,1);
- TreeSet
treeSet=new TreeSet<>(); - Map
map=new HashMap<>(); - for(int i=0;i
- {
- if(rains[i]==0)treeSet.add(i);//记录所有晴天
- else {//如果是雨天,寻找最近的晴天抽水
- ans[i] = -1;
- if (map.containsKey(rains[i])) {
- Integer it = treeSet.ceiling(map.get(rains[i]));//寻找后面几天最近的晴天
- if (it == null) {
- return new int[0];
- } else {
- ans[it] = rains[i];
- treeSet.remove(it);
- }
- }
- map.put(rains[i], i);
- }
- }
- return ans;
- }
- }
TreeSet中ceiling用法:
public E ceiling(E e)
参数
e - 这是要匹配的值。
返回值
方法调用返回大于或等于e的最小元素,如果没有这样的元素则返回null。

模拟题(今天的代码越写越长就离谱,纯纯模拟了)
第一次用异常,好用+1+1+1螺旋矩阵
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
-
- class Solution {
- List
list; - int m,n;
- public List
spiralOrder(int[][] matrix) { - list=new ArrayList<>();
- m= matrix.length;
- n=matrix[0].length;
- if(m==1)
- {
- for(int i=0;i
0][i]); - return list;
- }
- if(n==1)
- {
- for(int i=0;i
0]); - return list;
- }
- for(int i=0,j=0;i<(m+1)/2;i++,j++)
- {
- try{
- add(i,j,matrix);
- }catch (Exception E){
- break;
- }
-
-
- }
- return list;
- }
- public void add(int i,int j,int[][] matrix)
- {
- for(int k=j;k
- if(matrix[i][k]!=-101)
- {
- list.add(matrix[i][k]);
- matrix[i][k]=-101;
- }
-
- }
- for(int k=i+1;k
- if(matrix[k][n-1-j]!=-101){
- list.add(matrix[k][n-1-j]);
- matrix[k][n-1-j]=-101;
- }
-
- }
- for(int k=n-2-j;k>j;k--){
- if(matrix[m-1-i][k]!=-101){
- list.add(matrix[m-1-i][k]);
- matrix[m-1-i][k]=-101;
- }
- }
- for(int k=m-1-i;k>i;k--){
- if(matrix[k][j]!=-101){
- list.add(matrix[k][j]);
- matrix[k][j]=-101;
- }
- }
- }
- }
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
-
- class Solution {
- int dx[]=new int[]{0,1,-1,0};
- int dy[]=new int[]{-1,0,0,1};
- public void setZeroes(int[][] matrix) {
- List
>list=new ArrayList<>();
- for(int i=0;i
- {
- for(int j=0;j
- {
- if(matrix[i][j]==0)
- list.add(Arrays.asList(i,j));
- }
- }
- for(int i=0;i
- {
- int x=list.get(i).get(0);
- int y=list.get(i).get(1);
- for(int j=0;j< matrix[0].length;j++)matrix[x][j]=0;
- for(int k=0;k< matrix.length;k++)matrix[k][y]=0;
- }
- }
- }
11/7打卡,好久没打卡啦,但每天都坚持在刷题哦~我觉得最大的进步不是只做自己会的,而是去接受自己不会的,做自己从未做过的~打破舒适圈~!!!
- import java.util.*;
-
- class Solution {
- public static void main(String[] args) {
- System.out.println(vowelStrings(new String[]{"are", "amy", "u"}, 0, 2));
- }
-
- public static int vowelStrings(String[] words, int left, int right) {
- int ans = 0;
- List
list = new ArrayList<>(); - list.add('a');
- list.add('e');
- list.add('i');
- list.add('o');
- list.add('u');
- for (int i = left; i <= right; i++) {
- if (list.contains(words[i].charAt(0)) && list.contains(words[i].charAt(words[i].length() - 1))) ans++;
- }
- return ans;
- }
- }
12.25
利用归并排序的思想,返回最中间的中位数
- class Solution {
- public double findMedianSortedArrays(int[] nums1, int[] nums2) {
- //一边归并,一边找中位数
- int len = nums1.length + nums2.length;
- int cnt = 0;
- int arr[] = new int[len];
- double ans;
- int i = 0, j = 0;
- while (i < nums1.length && j < nums2.length) {
- if (nums1[i] <= nums2[j]) {
- arr[cnt++] = nums1[i++];
- continue;
- }
- if (nums1[i] >= nums2[j]) {
- arr[cnt++] = nums2[j++];
- continue;
- }
- }
- while (i < nums1.length) arr[cnt++] = nums1[i++];
- while (j < nums2.length) arr[cnt++] = nums2[j++];
- return len % 2 == 0 ? (float) (arr[len / 2 - 1] + arr[len / 2]) / 2 : (float) arr[len / 2];
- }
- }
-
相关阅读:
sqli-labs/Less-60
二维偏序问题
【Java】之Java8新特性
带你深入理解面向对象三大特性 - 继承,多态
SpringBoot 自动配置预加载类-01
静态文件鉴权
[二进制学习笔记]LibcSearcher报错Requirements for download
给定一个字符串str,求最长回文子序列长度。
gpt-4-vision-preview 识图
Service详解
-
原文地址:https://blog.csdn.net/m0_71385141/article/details/133633491