当前位置:网站首页>Niuke interview high frequency list (group 1) difficulty: simple & medium
Niuke interview high frequency list (group 1) difficulty: simple & medium
2022-07-25 03:48:00 【Ruizxzx】
(1)NC78 Reverse a linked list
(2)NC140 Sort
Two common sorts
Use fast platoon to realize
public int[] MySort (int[] arr) {
fastSort(arr,0,arr.length-1);
return arr;
}
private void fastSort(int[] arr,int l,int r){
if(l>=r)
return;
int num = arr[r];
int sl = l,sr=r;
while(l<r){
while(l<r&&arr[l]<num) l++;
// Be sure to judge before filling
if(l<r)
arr[r--] = arr[l];
while(l<r&&arr[r]>num)r--;
// Be sure to judge before filling
if(l<r)
arr[l++] = arr[r];
}
arr[r] = num;
fastSort(arr,sl,l-1);
fastSort(arr,l+1,sr);
}Realize by merging
public class Solution {
public int[] MySort (int[] arr) {
// write code here
mergeSort(arr,0,arr.length-1);
return arr;
}
private void mergeSort(int[] arr , int l , int r){
if(l>=r)
return;
int mid = (l+r)>>1;
// Continue recursion on both sides
mergeSort(arr,l,mid);
mergeSort(arr,mid+1,r);
// Both ends recursively , Just merge this layer
merge(arr,l,mid,r);
}
private void merge(int[] arr,int l,int mid,int r){
int[] t = new int[r-l+1];
int index = 0;
int h1 = l, h2 = mid+1;
while(h1<=mid&&h2<=r){
if(arr[h1]<arr[h2])
t[index++] = arr[h1++];
else
t[index++] = arr[h2++];
}
while(h1<=mid)
t[index++]=arr[h1++];
while(h2<=r)
t[index++]=arr[h2++];
// Note that remember to transfer the values in the temporary array to the original array
for(int i = 0;i<t.length;i++)
arr[l++]=t[i];
}
}Heap sort
Priority queue
(3)NC45 Implementation of binary tree preorder , In the middle order and after order
Return to form
public int[][] threeOrders (TreeNode root) {
// Distinguish with a linked list to store
ArrayList<Integer> prelist = new ArrayList<Integer>();
ArrayList<Integer> inlist = new ArrayList<Integer>();
ArrayList<Integer> postlist = new ArrayList<Integer>();
preOrder(root,prelist);
inOrder(root,inlist);
postOrder(root,postlist);
// Then create a two-dimensional array according to the length of the linked list
int[][] res = new int[3][prelist.size()];
for(int i=0;i<prelist.size();i++){
res[0][i] = prelist.get(i);
}
for(int i=0;i<inlist.size();i++){
res[1][i] = inlist.get(i);
}
for(int i=0;i<postlist.size();i++){
res[2][i] = postlist.get(i);
}
return res;
}(4)NC119 The smallest K Number
This topic can be divided into chapters , Large top pile . It's easier to implement the big top stack . Or use Array Bring their own sort Sort
import java.util.*;
public class Solution {
public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
ArrayList<Integer> res = new ArrayList<Integer>();
if(k<=0||input.length==0)
return res;
if(input.length<=k){
for(int i =0;i<input.length;i++)
res.add(input[i]);
return res;
}
PriorityQueue<Integer> queue = new PriorityQueue<Integer>((o1,o2)->(o2-o1));
for(int i = 0;i<input.length;i++){
if(queue.size()>=k){
if(queue.peek()>input[i]){
queue.poll();
queue.offer(input[i]);
}
}
else{
queue.offer(input[i]);
}
}
while(!queue.isEmpty()){
res.add(queue.poll());
}
return res;
}
}(5)NC15 Find the sequence traversal of binary tree
(6)NC88 Search for the first K Big
Use fast platoon , There are two points to note
1、 Be sure to remember , temporal t Put in
2、 Recursive time , The operation of reducing the scope
public class Solution {
public int findKth(int[] a, int n, int K) {
// write code here
if(n<=0||K>n)
return -1;
sortInt(a,0,n-1,K-1);
return a[K-1];
}
private void sortInt(int[] a, int l,int r,int k){
int t = a[r];
int nl = l,nr=r;
while(nl<nr){
while(nl<nr&&a[nl]>t)nl++;
if(nl<nr) a[nr--]=a[nl];
while(nl<nr&&a[nr]<=t)nr--;
if(nl<nr) a[nl++]=a[nr];
}
// Remember to t Go back
a[nl]=t;
if(nl==k)
return;
//+1,-1 The operation of reducing the scope , Otherwise, it will cause a dead cycle
if(nl<k)
sortInt(a,nl+1,r,k);
else
sortInt(a,l,nl-1,k);
}
}边栏推荐
- Record once C # extract audio files with ffmempeg
- 使用 “display: flex;justify-content: center;align-items: center; ” 解决流式栅格布局无法居中的问题
- Implementing DDD based on ABP -- domain logic and application logic
- Array the same value of key and merge the value value (collation)
- [golang] golang realizes sending wechat service number template messages
- Bubble sort / heap sort
- How should enterprise users choose aiops or APM?
- Optimization of MySQL sorting index fields
- Analysis of browser working principle
- Time complexity and space complexity
猜你喜欢

Machine learning notes - building a recommendation system (4) matrix decomposition for collaborative filtering

Analysis of DNS domain name resolution process
![[kaggle] how to effectively avoid oom and the long process of alchemy](/img/d1/cf6ecdeea9aa97d0eb93aa963687a5.png)
[kaggle] how to effectively avoid oom and the long process of alchemy

原创 | ueditor1.4.3-asmx绕过waf

Force deduction brush question 26. Delete duplicates in the ordered array

Network security - comprehensive penetration test -cve-2018-10933-libssh maintain access

Hw2021 attack and defense drill experience - Insights

Network security - information hiding - use steganography to prevent sensitive data from being stolen

Detailed explanation of three factory modes

Force deduction brush question 7. Integer inversion
随机推荐
How does Jupiter notebook change themes and font sizes?
Unity: test rotation function
Fifth day of force deduction
[template engine] microservice Learning Notes 6: freemaker
Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
Secondary vocational network security skills competition P100 vulnerability detection
Day 9 (capture traffic and routing strategy)
Advantages and disadvantages of zero trust security
Force deduction brush question 14. Longest common prefix
01_ Education 4
Calculation method of confusion matrix
Implementing DDD based on ABP -- domain logic and application logic
Original | record a loophole excavation in Colleges and Universities
Matplotlib tutorial (I) [getting to know Matplotlib first]
原创 | ueditor1.4.3-asmx绕过waf
C language function operation
原创|记一次高校漏洞挖掘
Hal library serial port for note taking
292. Nim game
2022-07-19 study notes of group 5 self-cultivation class (every day)