当前位置:网站首页>Daily question brushing record (XXVII)
Daily question brushing record (XXVII)
2022-07-20 06:36:00 【Unique Hami melon】
List of articles
- The first question is : 1108. IP Address invalidation
- The second question is : 1431. The kid with the most candy
- Third question : 1720. Decode the XOR array
- Fourth question : 1863. Find the XOR sum of all subsets and then sum
- Fifth question : 2160. The smallest sum of the last four digits after splitting
- Sixth question : 2181. Merge nodes between zeros
The first question is : 1108. IP Address invalidation
LeetCode: 1108. IP Address invalidation
describe :
Give you an effective IPv4 Address address
, Go back to this IP Invalid version of address .
The so-called invalidation IP Address , In fact, it is to use "[.]"
Instead of every "."
.
Their thinking :
- Here we use string .
- As long as you encounter
.
Just put it together[.]
Code implementation :
class Solution {
public String defangIPaddr(String address) {
StringBuilder res = new StringBuilder();
for(int i = 0; i < address.length(); i++) {
if(address.charAt(i)=='.') {
res.append("[.]");
}else{
res.append(address.charAt(i));
}
}
return res.toString();
}
}
The second question is : 1431. The kid with the most candy
LeetCode: 1431. The kid with the most candy
describe :
Give you an array candies
And an integer extraCandies
, among candies[i]
On behalf of the i
The number of candy children have .
For every child , Check if there is a solution , Will be extra extraCandies
After giving out candy to the children , This child has most Of candy . Be careful , Allow more than one child to have most The number of sweets .
Their thinking :
- First traversal , Get it in the array Largest element .
- Second traversal , Look at the elements in the array plus
extraCandies
Is it greater than this maximum .
- If it is greater than , Namely true
- If it is less than , Namely false
Code implementation :
class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
List<Boolean> res = new ArrayList<>();
int max = 0;
for(int candie : candies) {
max = Math.max(candie,max);
}
for(int i = 0; i < candies.length ; i++) {
if(candies[i] + extraCandies >= max) {
res.add(true);
}else{
res.add(false);
}
}
return res;
}
}
Third question : 1720. Decode the XOR array
LeetCode: 1720. Decode the XOR array
describe :
Unknown An array of integers arr
from n It's a nonnegative integer .
After encoding, the length becomes n - 1
Another array of integers encoded
, among encoded[i] = arr[i] XOR arr[i + 1]
. for example ,arr = [1,0,2,1]
After coding, we get encoded = [1,2,3]
.
I'll give you the coded array encoded
And the original array arr
The first element of first(arr[0])
.
Please decode to return the original array arr
. It can be proved that the answer exists and is unique .
Their thinking :
- Their thinking from a ^ b = c, Limit know b, c, seek a, a = b ^ c
- According to the theme , The size of the result array is encoded The length of the array +1.
- The first element is
first
. The following elements areres[i] = res[i-1] ^ encoded[i-1]
Code implementation :
class Solution {
public int[] decode(int[] encoded, int first) {
int[] res = new int[encoded.length + 1];
res[0] = first;
for(int i = 0; i < encoded.length; i++) {
res[i+1] = encoded[i] ^ res[i];
}
return res;
}
}
Fourth question : 1863. Find the XOR sum of all subsets and then sum
LeetCode: 1863. Find the XOR sum of all subsets and then sum
describe :
Of an array XOR sum Defines all elements in an array by bit XOR Result ; If the array is empty , Then the sum of XORs is 0 .
- for example , Array
[2,5,6]
Of XOR sum by2 XOR 5 XOR 6 = 1
.
Give you an array nums
, Please find out nums
Each of them A subset of Of XOR sum , Calculate and return the sum of these values and .
Be careful : In the subject , Elements identical Different subsets of should many times Count .
Array a It's an array b One of the A subset of The precondition is : from b Delete a few ( Or maybe not ) Elements can get a .
Their thinking :
- Use backtracking to solve problems .
- Pay attention to pruning , Elements cannot be reused .
- Add the results of all XORs .
Code implementation :
class Solution {
private int res = 0;
public int subsetXORSum(int[] nums) {
bfs(nums,0,0);
return res;
}
public void bfs(int[] nums, int start , int tmp) {
res += tmp;
for(int i = start; i < nums.length; i++) {
bfs(nums, i+1, tmp^nums[i]);
}
}
}
Fifth question : 2160. The smallest sum of the last four digits after splitting
LeetCode: 2160. The smallest sum of the last four digits after splitting
describe :
Here's a four just Integers num . Please use num Medium digit , take num Split into two new integers new1 and new2 .new1 and new2 You can have Leading 0 , And num in all All digits must be used .
- For example , Here you are.
num = 2932
, The numbers you have include : Two 2 , One 9 And a 3 . Some of the possibilities[new1, new2]
The number pair is[22, 93],[23, 92],[223, 9] and [2, 329]
.
Please return what you can get new1 and new2 Of Minimum and .
Their thinking :
- First of all, all bits are arrayed .
- Then for this array Sort .
- for example a < b < c < d, Give Way
a*10 + d
,b*10 + c
, These are the two minimum sum pairs .- Returns the sum of these two numbers
Code implementation :
class Solution {
public int minimumSum(int num) {
int[] arr = new int[4];
for(int i = 0; i < 4; i++) {
arr[i] = num%10;
num /= 10;
}
Arrays.sort(arr);
return arr[0]*10+arr[3] + arr[1]*10+arr[2];
}
}
Sixth question : 2181. Merge nodes between zeros
LeetCode: 2181. Merge nodes between zeros
describe :
Give you a list of the head node head , The linked list contains 0 A series of separated integers . Linked list start and At the end of All nodes meet Node.val == 0
.
For every two adjacent 0 , Please merge all the nodes between them into one node , Its value is the sum of the values of all merged nodes . And then all of the 0 remove , The modified linked list should not contain any 0 .
Return the head node of the modified linked list head
.
Their thinking :
- Use one cur Point to the head node , This cur Used to move
- Use one pre Point to the head node , This pre Used for the modification after logarithmic summation . Always let pre Pointing to 0 The node of .
- Use one node, Point to pre Previous node of , In this way, the last one can be removed as 0 The situation of
- First, traverse , If cur.val != 0, Just calculate , Seek not 0 The value of this continuous node tmp.
- Give Way pre Replace with the value of the node , And then let pre Point to the next as 0 The node of . Give Way node Point to pre Previous node of .
- End of traversal , Give Way node.next empty . One more is 0 The node of , To get rid of
Code implementation :
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode mergeNodes(ListNode head) {
ListNode cur = head;
ListNode pre = head;
ListNode node = null;
while(cur != null) {
int tmp = 0;
while(cur != null && cur.val != 0) {
tmp += cur.val;
cur = cur.next;
}
if(tmp != 0) {
node = pre;
pre.val = tmp;
pre.next = cur;
pre = cur;
}
cur = cur.next;
}
node.next = null;
return head;
}
}
边栏推荐
- [Development Tutorial 4] crazy shell arm function mobile phone - development interface connection tutorial
- 广发证券怎么样?网上开户安全吗?
- [C exercise] arrow pattern
- JVM knowledge map (under update)
- 华为虚拟化FusionCompute知识点总结
- The difference between router and switch
- 小草满天飞
- Kstry框架一种服务编排的实现
- OGC WebGIS 常用服务标准(WMS/WMTS/TMS/WFS)速查
- shell命令背后的执行过程
猜你喜欢
高度警惕!战场上智能手机位置数据的武器化
Conditions and details of polar coordinate substitution for solving the limit of multivariate functions with high numbers
Tutoriel de requête SQL pour la science des données avec mon serveur SQL
3年软件测试经验,薪资一直卡在10k,自动化测试如何提升和发展?
华为虚拟化FusionCompute知识点总结
使用 My SQL Server 实现数据科学的 SQL 查询教程
Advanced numbers | [differential calculus of multivariate functions] concept chapter -- the relationship between continuity, partial differentiation and differentiability
The LAAS protocol elephant of defi 2.0 is the key to revitalizing the development of defi track
[depth] the new LAAS agreement elephant: the key to revitalizing the development of the defi track
WTO officially announced that sun Yuchen's MC12 speech covered major topics such as e-commerce
随机推荐
The LAAS protocol elephant of defi 2.0 is the key to revitalizing the development of defi track
Luogu p2024 [noi2001] food chain solution
.NET 序列化枚举为字符串
金融银行软件测试超大型攻略,最受欢迎的金融银行大揭 秘附面试题
fuser和lsof的用法
JS的DOM操作——元素盒子模型
【文件操作的重难点详解】
mTD-SCDMA与TD-LTE双网络垂直切换matlab仿真
There are two key skills for high availability of microservices, which you must use!
ovirt: api
ES6-Set和Map
VLAN聚合
INE Penetration Testing Basics 黑盒渗透测试过程
3年软件测试经验,薪资一直卡在10k,自动化测试如何提升和发展?
[jailhouse article] specific electronic platform to test the influence of hypervisors on the performance
[depth] the new LAAS agreement elephant: the key to revitalizing the development of the defi track
Leetcode- supplementary question 6 - sorting by hand
EN 1504-2 concrete structure protection and repair products - CE certification
Prepare for the attack and defense drill. Here is a security deployment map of Tencent!
中间抽头的变化对于ZVS振荡电路的影响