赞
踩
k三题
- class Solution {
- public int sumOfEncryptedInt(int[] nums) {
- int ans = 0;
- for(int i =0;i<nums.length;i++){
- char [] chars = String.valueOf(nums[i]).toCharArray();
- Arrays.sort(chars);
- int k = chars[chars.length-1]-'0';
- int res = 0;
- for(int j =0;j<chars.length;j++){
- res=res*10+k;
- }
- ans += res;
- }
- return ans;
- }
- }
- class Solution {
- public long[] unmarkedSumArray(int[] nums, int[][] queries) {
- PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> {
- if (nums[a] != nums[b]) {
- return nums[a]-nums[b];
- }
- return a-b;
-
- });
- Set<Integer> markedSet = new HashSet<>();
-
- for (int i = 0; i < nums.length; i++) {
- pq.offer(i);
- }
-
-
- long[] answer = new long[queries.length];
- long currentSum = 0;
- for (int num : nums) {
- currentSum=(long)((long)currentSum+(long)num);
- }
-
- for (int i = 0; i < queries.length; i++) {
- if (pq.isEmpty()) break;
- int index = queries[i][0];
- int ki = queries[i][1];
-
- // 如果该位置未被标记,则标记它
- if (!markedSet.contains(index)) {
- markedSet.add(index);
- currentSum= currentSum-(long)nums[index];
- }
-
- // 标记ki个最小的未标记元素
- while (ki > 0 && !pq.isEmpty()) {
- Integer top = pq.poll();
- if (!markedSet.contains(top)) {
- markedSet.add(top);
- currentSum -= nums[top];
- ki--;
- }
- }
-
- answer[i] = currentSum;
- }
-
- return answer;
- }
- }
- class Solution {
- public String minimizeStringValue(String s) {
- int[] countChar = new int[30];
- PriorityQueue<Integer> pq = new PriorityQueue<Integer>(
- (a, b) -> {
- if (countChar[a] != countChar[b])
- return countChar[a] - countChar[b];
- return a - b;
- }
- );
- StringBuilder sb = new StringBuilder(s);
- char res[] = s.toCharArray();
- for (int i = 0; i < s.length(); i++) {
- if (res[i] != '?') {
- countChar[res[i] - 'a']++;
- }
- }
-
- List<Character> list = new ArrayList<>();
- for (int i = 0; i < 26; i++) {
- pq.add(i);
- }
- for (int i = 0; i < s.length(); i++) {
- if (res[i] == '?') {
- Integer peek = pq.poll();
- char c = (char) (peek + 'a');
- list.add(c);
- countChar[peek]++;
- pq.add(peek);
- }
- }
- Collections.sort(list);
- int k = 0;
- for (int j = 0; j < s.length(); j++) {
- if (res[j] == '?') {
- sb.setCharAt(j, list.get(k++));
- }
- }
- return String.valueOf(sb);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。