当前位置:网站首页>Daily question brushing record (XXV)
Daily question brushing record (XXV)
2022-07-18 21:10:00 【Unique Hami melon】
List of articles
- The first question is : 1394. Find the lucky number in the array
- The second question is : 1512. A good number of pairs
- Third question : Interview questions 10.11. Peak and valley
- Fourth question : Interview questions 16.26. Calculator
- Fifth question : Interview questions 17.12. BiNode
- Sixth question : Interview questions 17.19. Two missing numbers
The first question is : 1394. Find the lucky number in the array
LeetCode: 1394. Find the lucky number in the array
describe :
In an array of integers , If the frequency of an integer is the same as its value , We call this whole number 「 Lucky number 」.
Give you an array of integers arr, Please find out and return a lucky number .
- If there are more than one lucky number in the array , Just go back Maximum the .
- If there are no lucky numbers in the array , Then return to -1 .


Their thinking :
- First use a length of 501 Array of , For storage , Store the number of occurrences of each number .
- To return the same number of occurrences , And the biggest .
- In this way, you can directly consider the array length , Because the maximum number of occurrences is the array length , So give priority to array length .
- And then smaller and smaller to find out whether there is one that meets the conditions .
Code implementation :
class Solution {
public int findLucky(int[] arr) {
int[] count = new int[501];
for(int val : arr) {
count[val]++;
}
for(int i = arr.length; i>0 ;i--) {
if(count[i] == i){
return i;
}
}
return -1;
}
}
The second question is : 1512. A good number of pairs
LeetCode: 1512. A good number of pairs
describe :
Give you an array of integers nums .
If a set of numbers (i,j) Satisfy nums[i] == nums[j] And i < j , You can think of this as a group Good number of pairs .
Returns the number of good pairs .
Their thinking :
- First create a length of 101 Array of , Used to store the number of occurrences .
- For the first appearance of numbers , Do not add , After each occurrence, add the number of occurrences .
- for example
1, 1, 1, 3
- First appearance 1, res Do not add , Now count 1 Time
- The second time 1, res Add up , res=1, Now count 2 Time
- A third time 1, res Add up , res=3, Now count 3 Time
- End of traversal return res
Code implementation :
class Solution {
public int numIdenticalPairs(int[] nums) {
int[] count = new int[101];
int res = 0;
for(int num : nums) {
res += count[num];
count[num]++;
}
return res;
}
}
Third question : Interview questions 10.11. Peak and valley
LeetCode: Interview questions 10.11. Peak and valley
describe :
In an array of integers ,“ peak ” Is an element greater than or equal to an adjacent integer , Accordingly ,“ valley ” Is an element less than or equal to an adjacent integer . for example , In the array {5, 8, 4, 2, 3, 4, 6} in ,{8, 6} It's the peak , {5, 2} It's the valley . Now, given an array of integers , Sort the array in alternating order of peaks and valleys .
Their thinking :
- This question means one big and one small , Alternate .
- You can sort the array .
- Then use the double pointer , A pointing head , One points to the tail .
- Put the largest one at a time (right–), Put another smallest (left++),
Code implementation :
class Solution {
public void wiggleSort(int[] nums) {
int[] arr = new int[nums.length];
for(int i = 0; i < nums.length; i++) {
arr[i] = nums[i];
}
Arrays.sort(arr);
int left = 0;
int right = arr.length-1;
for(int i = 0; i < nums.length; i++) {
if(i%2==0){
nums[i] = arr[right--];
}else{
nums[i] = arr[left++];
}
}
}
}
Fourth question : Interview questions 16.26. Calculator
LeetCode: Interview questions 16.26. Calculator
describe :
Given an integer containing a positive integer 、 Add (+)、 reduce (-)、 ride (*)、 except (/) Arithmetic expression of ( Except brackets ), Calculate the result .
The expression contains only non negative integers ,+, - ,*,/ Four operators and spaces . Division of integers preserves only the integral part .
Their thinking :
- Here a stack is used to store data .
- Store the current data , it is to be noted that , There will be continuous numbers
- The first operation , Directly stack , Then record the operator .
- If it's time to add, subtract, multiply and divide again , See what the previous operator is
- If it is
-, Deposit in-num- If it is
+, Deposit innum- If it is
*, Deposit instack.pop() * num- If it is
/, Deposit instack.pop() / num- After the traversal , Stack storage is all data , At this point, the result is to add all the data directly
Code implementation :
class Solution {
public int calculate(String s) {
Stack<Integer> stack = new Stack<>();
// It's convenient to directly stack for the first time
char opt = '+';
int num = 0;
for(int i = 0; i < s.length(); i++) {
if(Character.isDigit(s.charAt(i))){
num = num * 10 + (s.charAt(i) - '0');
}
if((!Character.isDigit(s.charAt(i)) && s.charAt(i)!=' ') || i == s.length() - 1){
if(opt == '+') stack.push(num);
else if(opt == '-') stack.push(-num);
else if(opt == '*') stack.push(stack.pop() * num);
else stack.push(stack.pop() / num);
num = 0;
opt = s.charAt(i);
}
}
int res = 0;
while(!stack.isEmpty()){
res += stack.pop();
}
return res;
}
}
Fifth question : Interview questions 17.12. BiNode
LeetCode: Interview questions 17.12. BiNode
describe :
Binary tree data structure TreeNode Can be used to represent a one-way list ( among left empty ,right For the next linked list node ). Implement a method , The binary search tree into a one-way list , The requirements still conform to the nature of binary search tree , The conversion operation should be original , That is to modify it directly on the original binary search tree .
Returns the header node of the converted unidirectional linked list .
Their thinking :
- Here we use the method of middle order traversal .
- First, there is a precursor node , Used to link .
- In the sequence traversal , Let the pioneer node link ( Link on the right node ), And leave the left node of the node empty . Let this node point to root.
- Finally, return to the next node of the precursor node .
Code implementation :
class Solution {
TreeNode cur = new TreeNode(-1);
TreeNode pre = cur;
public TreeNode convertBiNode(TreeNode root) {
if (root == null) return null;
convertBiNode(root.left);
cur.right = root;
root.left = null;
cur = root;
convertBiNode(root.right);
return pre.right;
}
}
Sixth question : Interview questions 17.19. Two missing numbers
LeetCode: Interview questions 17.19. Two missing numbers
describe :
Given an array , Contains from 1 To N All integers , But two numbers are missing . Can you in O(N) Only in time O(1) Space to find them ?
Return these two numbers in any order .
Their thinking :
- The mathematical idea used here
- First, find the total sum of the array
total- And then use it Summation formula Get
1 ~ len(nums)+2Andsum.- And then let this
sum - totalFind the sum of two vanishing numbers .- Then find the average of these two numbers
mid- Traverse... Again , Seek less than
midAnd ,1 ~ midThere must be a missing number in .- Let the sum at this time subtract The sum that satisfies the condition in the array . Is one of the results .
- Let the sum of two numbers Subtracting one result is the other result .
Code implementation :
class Solution {
public int[] missingTwo(int[] nums) {
int total = 0;
for(int num : nums) {
total += num;
}
int sum = (nums.length+2+1)*(nums.length+2)/2 - total;
int mid = sum/2;
total = 0;
for(int num : nums) {
if(num <= mid) {
total += num;
}
}
int res = (1+mid)*mid/2 - total;
return new int[]{
res, sum-res};
}
}
边栏推荐
- Win10 如何将FAT32格式磁盘不用格式化无损转化为NFTS格式
- 劍指 Offer 57. 和為s的兩個數字
- Raspberry pie record
- 动态添加路由刷新页面会出现白屏
- R语言ggplot2可视化:ggplot2可视化密度图(density plot)并使用geom_vline函数添加均值竖线、添加均值数值标签(Mean Line or Vertical Line )
- AMASLAB-EPIC-KBS9工控机刷机文档
- docker mysql
- 【js 封装一个简单的异步API,获取异步操作结果和过程解析】
- 关于#sql#的问题:orcale sql, 为什么MERCHANT table的外键inventory—id 语句是无效的
- Sword finger offer 57 - ii Continuous positive sequence with sum s
猜你喜欢

Stm32f407---- power management

Sword finger offer 57 And are two numbers of S

剑指 Offer 54. 二叉搜索树的第k大节点

基于 Servlet 项目——博客系统

sentinel

Remember these points and you can quickly find bugs

Uniapp request request encapsulation method

stm32F407----电源管理

sentinel

Sword finger offer 57 - ii Continuous positive sequence with sum s
随机推荐
UE4 Lights UWorld to FScene [2]
一步到位玩透Ansible-目录
R language uses pcauchy function to generate Cauchy distribution cumulative distribution function data, and uses plot function to visualize Cauchy distribution cumulative distribution function data
R语言ggplot2可视化:ggplot2可视化密度图(density plot)并使用geom_vline函数添加均值竖线、添加均值数值标签(Mean Line or Vertical Line )
Electron安装配置
What is industrial planning? How to make industrial planning for industrial parks
Win10 how to convert FAT32 format disks into NFTs format without formatting
每日刷题记录 (二十五)
摸清企业选址动机及需求,高效开展招商引资工作
Ten million level data MySQL distinct group by
产业园区增强企业服务能力的三大路径
【缓存】一种新的缓存 Caffeine Cach 介绍
(manual) [sqli-labs54-57] limit the number of injections: joint injection, error echo, get injection
UE4 shadow: perobjectshadow verification
Metrics study notes
Paddle CrowdNet 人群密度估计
Have you met these use case design questions during the interview?
Practical project: problems encountered in data access layer
VMware recovery snapshot failed to create an anonymous paging file of 5040 MB: insufficient system resources to complete the requested service
The concept and properties of | double integral under high numbers | high numbers | handwritten notes