当前位置:   article > 正文

Leetcode - 周赛394_list[] g = new arraylist[n];遍历输出

list[] g = new arraylist[n];遍历输出

目录

一,3120. 统计特殊字母的数量 I

二,3121. 统计特殊字母的数量 II

三,3122. 使矩阵满足条件的最少操作次数

四,3123. 最短路径中的边


一,3120. 统计特殊字母的数量 I

本题就是统计有多少个字母的大小写同时出现在字符串word中,分别使用一个数组来统计大写字母和小写字母的出现次数。 

代码如下: 

  1. class Solution {
  2. public int numberOfSpecialChars(String word) {
  3. int[] cnt1 = new int[26];//大写
  4. int[] cnt2 = new int[26];//小写
  5. for(char ch : word.toCharArray()){
  6. if(Character.isUpperCase(ch)){
  7. cnt1[ch-'A']++;
  8. }else{
  9. cnt2[ch-'a']++;
  10. }
  11. }
  12. int ans = 0;
  13. for(int i=0; i<26; i++){
  14. if(cnt1[i]>0 && cnt2[i]>0)
  15. ans++;
  16. }
  17. return ans;
  18. }
  19. }

二,3121. 统计特殊字母的数量 II

本题比上一题多了一些条件,不仅要大小写都出现,并且所有的小写字母都要出现在其对应大写字母的前面。所以可以使用两个数组分别存储,一个存储大写字母的第一次出现下标,另一个存储小写字母最后一次出现下标,如果大小写都出现&小写字母的下标<大写字母的下标,那么就+1。

代码如下:

  1. class Solution {
  2. public int numberOfSpecialChars(String word) {
  3. int[] idx1 = new int[26];//大写字母第一次出现的下标
  4. int[] idx2 = new int[26];//小写字母最后一次出现的下标
  5. Arrays.fill(idx1, -1);
  6. Arrays.fill(idx2, -1);
  7. char[] ch = word.toCharArray();
  8. for(int i=0; i<ch.length; i++){
  9. if(Character.isLowerCase(ch[i])){
  10. idx2[ch[i]-'a'] = i;
  11. }else{
  12. idx1[ch[i]-'A'] = idx1[ch[i]-'A']==-1?i:idx1[ch[i]-'A'];
  13. }
  14. }
  15. int ans = 0;
  16. for(int i=0; i<26; i++){
  17. if(idx1[i]!=-1&&idx2[i]!=-1&&idx1[i]>idx2[i])
  18. ans++;
  19. }
  20. return ans;
  21. }
  22. }

上述做法更加不容易出错,但是还有更加省时间的做法,可以一次遍历实现,使用一个hash表来统计出现的大小写字母,再使用一个数组 cnt 来统计 26 个字母的状态:

  • 0:该字母没有计入答案
  • 1:该字母已计入答案
  • 2:该字母不可能是答案

遍历该字符串:

  • 如果是大写字母,并且之前出现过小写字母,将cnt[i] = 0 (表示该字母没有被计入ans中),将cnt[i] = 1,ans++
  • 如果是小写字母,并且之前出现过大写字母&& cnt[i] == 1 (表示该字母已经被计入ans中),将cnt[i] = 2 (表示该字母不可能是答案),ans--
  • 如果是大写字母,并且之前没有出现过小写字母,将cnt[i]=2 (表示该字母不可能是答案),防止出现BbB这种情况
  1. class Solution {
  2. public int numberOfSpecialChars(String word) {
  3. Set<Character> set = new HashSet<>();//统计大小写
  4. int[] cnt = new int[26];
  5. //0:该字母没有计入答案
  6. //1:该字母已计入答案
  7. //2:该字母被计入答案后又出现小写字母,又被删除
  8. int ans = 0;
  9. for(char ch : word.toCharArray()){
  10. if(Character.isUpperCase(ch)){
  11. //小写字母出现后大写字母出现的情况
  12. if(set.contains(Character.toLowerCase(ch)) && cnt[ch-'A']==0){
  13. cnt[ch-'A'] = 1;
  14. ans++;
  15. }
  16. //出现小写字母未出现,先出现大写字母的情况!!!(容易忽略)
  17. if(!set.contains(Character.toLowerCase(ch))){
  18. cnt[ch-'A'] = 2;
  19. }
  20. }else{
  21. //出现大写字母后,又出现小写字母的情况
  22. if(set.contains(Character.toUpperCase(ch)) && cnt[ch-'a']==1){
  23. cnt[ch-'a'] = 2;
  24. ans--;
  25. }
  26. }
  27. set.add(ch);
  28. }
  29. return ans;
  30. }
  31. }

三,3122. 使矩阵满足条件的最少操作次数

没有思路,先考虑暴力求解,也就是枚举每一列选择保留的数字,保证相邻两列保留的数不一样,求保留的最大值,所以 dfs 就需要两个变量,一个存储当前的列数,一个存储上一个保留的数字。

dfs(i,pre) 的定义:前 i 列数可以保留的最大数目。

  • 结束条件:i < 0,返回 0
  • 每次递归要做:使用 j 遍历0~9,res = Math.max( res,dfs(i-1,j)+cnt[ i ][ j ]),cnt[ i ][ j ]表示当前列中有多少个 j 
  • 结果: res
  1. class Solution {
  2. Map<String, Integer> map = new HashMap<>();
  3. public int minimumOperations(int[][] grid) {
  4. int n = grid.length;
  5. int m = grid[0].length;
  6. int[][] cnt = new int[m][10];
  7. for(int i=0; i<n; i++){
  8. for(int j=0; j<m; j++){
  9. cnt[j][grid[i][j]]++;
  10. }
  11. }
  12. return n*m - dfs(m-1, cnt, -1);
  13. }
  14. int dfs(int i, int[][] cnt, int pre){
  15. String key = i + "-" + pre;
  16. if(!map.isEmpty() && map.containsKey(key))
  17. return map.get(key);
  18. if(i < 0) return 0;
  19. int res = 0;
  20. for(int j=0; j<=9; j++){
  21. if(j != pre)
  22. res = Math.max(res, dfs(i-1, cnt, j)+cnt[i][j]);
  23. }
  24. map.put(key, res);
  25. return res;
  26. }
  27. }

四,3123. 最短路径中的边

本题和上周双周赛一样,考的还是djstra算法,只不过它问的是edges[i]是否在0->n-1节点的最短路径上,这里有两种做法,先讲简单一点的:

使用两次djstra算法,分别算出从0到所有位置的最短路径dis1,以及从n-1到所有位置的最短路径dis2,再遍历edges数组(x=e[0],y=e[1],w=e[2]),判断 dis1[x] + dis2[y] + w == dis1[n-1] 或者 dis1[y] + dis2[x] + w == dis1[n-1],如果相等说明ans[i] = true,否则 ans[i] = false。就是下图两种情况:

代码如下:

  1. class Solution {
  2. int n;
  3. public boolean[] findAnswer(int n, int[][] edges) {
  4. this.n = n;
  5. List<int[]>[] g = new ArrayList[n];
  6. Arrays.setAll(g, e->new ArrayList<>());
  7. for(int i=0; i<edges.length; i++){
  8. int[] e = edges[i];
  9. int x = e[0], y = e[1], w = e[2];
  10. g[x].add(new int[]{y, w});
  11. g[y].add(new int[]{x, w});
  12. }
  13. int[] dis1 = dis(g, 0);//0->?的最短路径
  14. int[] dis2 = dis(g, n-1);//n-1 -> ?的最短路径
  15. int mn = dis1[n-1];
  16. boolean[] ans = new boolean[edges.length];
  17. for(int i=0; i<edges.length; i++){
  18. int[] e = edges[i];
  19. int x = e[0], y = e[1], w = e[2];
  20. if(dis1[x] + dis2[y] + w == mn || dis1[y]+dis2[x]+w==mn){
  21. ans[i] = true;
  22. }else{
  23. ans[i] = false;
  24. }
  25. }
  26. return ans;
  27. }
  28. int[] dis(List<int[]>[] g, int s){
  29. int[] dis = new int[n];
  30. Arrays.fill(dis, Integer.MAX_VALUE/2);
  31. dis[s] = 0;
  32. PriorityQueue<int[]> que = new PriorityQueue<>((x,y)->x[0]-y[0]);
  33. que.offer(new int[]{0, s});
  34. while(!que.isEmpty()){
  35. int[] t = que.poll();
  36. int dx = t[0];
  37. int x = t[1];
  38. if(dx > dis[x]){
  39. continue;
  40. }
  41. for(int[] y : g[x]){
  42. int idx = y[0];
  43. int newDis = dx + y[1];
  44. if(dis[idx]==Integer.MAX_VALUE/2 || newDis < dis[idx]){
  45. dis[idx] = newDis;
  46. que.offer(new int[]{newDis,idx});
  47. }
  48. }
  49. }
  50. return dis;
  51. }
  52. }

另一种解法和上述解法的思路是相同的,但是时间复杂度更低,只使用一次djstra算法,算出从0到所有位置的最短路径dis,然后从n-1出发,倒着dfs,如果dis[y] + w == dis[x],就说明当前x-y这段路径在最短路径上。

代码如下:

  1. class Solution {
  2. public boolean[] findAnswer(int n, int[][] edges) {
  3. List<int[]>[] g = new ArrayList[n];
  4. Arrays.setAll(g, e->new ArrayList<>());
  5. for(int i=0; i<edges.length; i++){
  6. int[] e = edges[i];
  7. int x = e[0], y = e[1], w = e[2];
  8. g[x].add(new int[]{y, w, i});
  9. g[y].add(new int[]{x, w, i});
  10. }
  11. PriorityQueue<int[]> que = new PriorityQueue<>((x,y)->x[0]-y[0]);
  12. que.offer(new int[]{0, 0});
  13. int[] dis = new int[n];
  14. Arrays.fill(dis, Integer.MAX_VALUE/2);
  15. dis[0] = 0;
  16. while(!que.isEmpty()){
  17. int[] t = que.poll();
  18. int dx = t[0];
  19. int x = t[1];
  20. if(dx > dis[x]) continue;
  21. for(int[] y : g[x]){
  22. int idx = y[0];
  23. int newDis = dx + y[1];
  24. if(dis[idx]==Integer.MAX_VALUE/2 || newDis < dis[idx]){
  25. dis[idx] = newDis;
  26. que.offer(new int[]{newDis, idx});
  27. }
  28. }
  29. }
  30. boolean[] ans = new boolean[edges.length];
  31. if(dis[n-1] == Integer.MAX_VALUE/2) return ans;
  32. boolean[] vis = new boolean[n];
  33. dfs(n-1, dis, ans, vis, g);
  34. return ans;
  35. }
  36. void dfs(int x, int[] dis, boolean[] ans, boolean[] vis, List<int[]>[] g){
  37. vis[x] = true;
  38. for(int[] t : g[x]){
  39. int y = t[0], w = t[1], i = t[2];
  40. if(dis[y] + w != dis[x])
  41. continue;
  42. ans[i] = true;
  43. if(!vis[y]){
  44. dfs(y, dis, ans, vis, g);
  45. }
  46. }
  47. }
  48. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/859592
推荐阅读
相关标签
  

闽ICP备14008679号