当前位置:   article > 正文

力扣126双周赛

力扣126双周赛

k三题

求出加密整数的和

  1. class Solution {
  2. public int sumOfEncryptedInt(int[] nums) {
  3. int ans = 0;
  4. for(int i =0;i<nums.length;i++){
  5. char [] chars = String.valueOf(nums[i]).toCharArray();
  6. Arrays.sort(chars);
  7. int k = chars[chars.length-1]-'0';
  8. int res = 0;
  9. for(int j =0;j<chars.length;j++){
  10. res=res*10+k;
  11. }
  12. ans += res;
  13. }
  14. return ans;
  15. }
  16. }

执行操作标记数组中的元素

  1. class Solution {
  2. public long[] unmarkedSumArray(int[] nums, int[][] queries) {
  3. PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> {
  4. if (nums[a] != nums[b]) {
  5. return nums[a]-nums[b];
  6. }
  7. return a-b;
  8. });
  9. Set<Integer> markedSet = new HashSet<>();
  10. for (int i = 0; i < nums.length; i++) {
  11. pq.offer(i);
  12. }
  13. long[] answer = new long[queries.length];
  14. long currentSum = 0;
  15. for (int num : nums) {
  16. currentSum=(long)((long)currentSum+(long)num);
  17. }
  18. for (int i = 0; i < queries.length; i++) {
  19. if (pq.isEmpty()) break;
  20. int index = queries[i][0];
  21. int ki = queries[i][1];
  22. // 如果该位置未被标记,则标记它
  23. if (!markedSet.contains(index)) {
  24. markedSet.add(index);
  25. currentSum= currentSum-(long)nums[index];
  26. }
  27. // 标记ki个最小的未标记元素
  28. while (ki > 0 && !pq.isEmpty()) {
  29. Integer top = pq.poll();
  30. if (!markedSet.contains(top)) {
  31. markedSet.add(top);
  32. currentSum -= nums[top];
  33. ki--;
  34. }
  35. }
  36. answer[i] = currentSum;
  37. }
  38. return answer;
  39. }
  40. }

 

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

  1. class Solution {
  2. public String minimizeStringValue(String s) {
  3. int[] countChar = new int[30];
  4. PriorityQueue<Integer> pq = new PriorityQueue<Integer>(
  5. (a, b) -> {
  6. if (countChar[a] != countChar[b])
  7. return countChar[a] - countChar[b];
  8. return a - b;
  9. }
  10. );
  11. StringBuilder sb = new StringBuilder(s);
  12. char res[] = s.toCharArray();
  13. for (int i = 0; i < s.length(); i++) {
  14. if (res[i] != '?') {
  15. countChar[res[i] - 'a']++;
  16. }
  17. }
  18. List<Character> list = new ArrayList<>();
  19. for (int i = 0; i < 26; i++) {
  20. pq.add(i);
  21. }
  22. for (int i = 0; i < s.length(); i++) {
  23. if (res[i] == '?') {
  24. Integer peek = pq.poll();
  25. char c = (char) (peek + 'a');
  26. list.add(c);
  27. countChar[peek]++;
  28. pq.add(peek);
  29. }
  30. }
  31. Collections.sort(list);
  32. int k = 0;
  33. for (int j = 0; j < s.length(); j++) {
  34. if (res[j] == '?') {
  35. sb.setCharAt(j, list.get(k++));
  36. }
  37. }
  38. return String.valueOf(sb);
  39. }
  40. }

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

闽ICP备14008679号