Leetcode-第 283 场周赛
生活随笔
收集整理的這篇文章主要介紹了
Leetcode-第 283 场周赛
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第一題
題目描述
2194. Excel 表中某個范圍內的單元格
解題思路
簡單題目,兩次循環即可。唯一需要注意的,遍歷時候以字符遍歷。
?解題代碼
class Solution {public List<String> cellsInRange(String s) {String s1 = s.split(":")[0];String s2 = s.split(":")[1];char xs = s1.charAt(0);int ys = s1.charAt(1) - 48;char xe = s2.charAt(0);int ye = s2.charAt(1) - 48;List<String> res = new ArrayList<>();for (char i = xs; i <= xe; i++) {for (int j = ys; j <= ye; j++) {res.add(String.valueOf(i) + j);}}return res;} }第二題
題目描述
2195. 向數組中追加 K 個整數
解題思路
被這道題坑了,開始的思路就是想的一個數一個數的去搞,非常遺憾,超時了。
class Solution {public long minimalKSum(int[] nums, int k) {Arrays.sort(nums);long res = 0;int index = 0;int curMin = 1;int count =0;while (index<nums.length && count<k) {if (curMin < nums[index]) {res += curMin;curMin++;count++;} else if (curMin==nums[index]) {index++;curMin++;}else {index++;}}while (count<k){res += curMin;curMin++;count++;}return res;} }接下來還禮一種思路,先把數字加到set中,然后從1開始,假如出現在set里面,就不加到總和,否則加入總和。
class Solution {public long minimalKSum(int[] nums, int k) {HashSet<Integer> set = new HashSet<>(100000);for (int i : nums) {set.add(i);}long res = 0;int curMin = 1;int count = 0;while (count < k) {if (!set.contains(curMin)) {res += curMin;count++;} curMin++;}return res;}}遺憾的是,這個方法也不行。
最后想到了要一段一段的算,從nums的數組兩個數字之間開始算,加入最后一段超出了總數,那就最后一段以總數為準。
解題代碼
class Solution {public long minimalKSum(int[] nums, int k) {Arrays.sort(nums);long res = 0;int index = 0;int count = 0;int pre = 0;while (index < nums.length) {int cur = nums[index];int n = cur - pre - 1;if (n > 0) {// 兩個數之間有數if (k - count > n) {count += n;res += (long) (pre + cur) * n / 2;} else {res += (long) (pre + pre + 1 + k - count) * (k - count) / 2;return res;}}pre = cur;index++;}if (count < k) {// 假如數組中間已經沒有可以插入的數字了,將剩余的加上pre = nums[nums.length - 1];res += (long) (pre + pre + 1 + k - count) * (k - count) / 2;}return res;}}第三題
2196. 根據描述創建二叉樹
解題思路
依照父節點和子節點關系,直接去建立樹即可,用兩個map來存儲節點,以此來找到最終的根節點。
PS:題目不講理,說好的 節點數字小于等于10000,但其實要比10000大一些.很生氣。
解題代碼
class Solution {public TreeNode createBinaryTree(int[][] descriptions) {TreeNode[] treeNodes = new TreeNode[1000000];HashSet<Integer> setF = new HashSet<>();HashSet<Integer> setS = new HashSet<>();for (int[] d : descriptions) {int father = d[0];int son = d[1];setF.add(father);setS.add(son);if (treeNodes[son] == null) {treeNodes[son] = new TreeNode(son);}if (treeNodes[father] == null) {treeNodes[father] = new TreeNode(father);}// 都不為空if (d[2] == 0) {treeNodes[father].right = treeNodes[son];} else {treeNodes[father].left = treeNodes[son];}}for (int fa : setF) {if (!setS.contains(fa)) {return treeNodes[fa];}}return null;} }第四題
題目描述
2197. 替換數組中的非互質數
?
解題思路
拿到題目,就覺得這個要頻繁用到刪除操作,假如在數組上操作,心想必然超時。于是改為自定義雙向鏈表。給雙向鏈表一個自己的頭,用于最后返回,每三個數做一個判斷,前兩個先判斷,后兩個后判斷。后兩個判斷完成之后,還有把指針再往前指一下。因為很有可能這兩個又可以合并。
解題代碼
class Solution {public List<Integer> replaceNonCoprimes(int[] nums) {Node head = new Node(0);Node p = head;for (int i : nums) {Node n = new Node(i);p.setNext(n);n.setPre(p);p = n;}p = head.next;while (p.next != null) {int v1 = p.pre.val;int v2 = p.val;int v3 = p.next.val;if (v1 != 0 && gcd(v1, v2) > 1) {p.pre.next = p.next;p.next.pre = p.pre;p = p.pre;p.val = lcm(v1, v2);} else if (gcd(v2, v3) > 1) {p.val = lcm(v2, v3);if (p.next.next != null) {p.next.next.pre = p;}p.next = p.next.next;} else {p = p.next;}}p = head.next;List<Integer> list = new ArrayList<>();while (p != null) {// System.out.println(p.val);list.add(p.val);p = p.next;}return list;}public int gcd(int a, int b) {if (b == 0) return a;return gcd(b, a % b);}public int lcm(int a, int b) {return a / gcd(a, b) * b;} }class Node {Node pre;Node next;int val;public Node(int val) {this.val = val;}public Node getPre() {return pre;}public void setPre(Node pre) {this.pre = pre;}public Node getNext() {return next;}public void setNext(Node next) {this.next = next;}public int getVal() {return val;}public void setVal(int val) {this.val = val;} }總結
以上是生活随笔為你收集整理的Leetcode-第 283 场周赛的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第一次使用JMETER:JMETER h
- 下一篇: Leetcode-第 73 场双周赛