当前位置:网站首页>Force buckle day32
Force buckle day32
2022-07-04 01:23:00 【Congruence_ vinegar】
540、 A single element in an ordered array
Give you an ordered array of integers only , among Each element appears twice , Only one number will appear once . Give you an ordered array of integers only , Each of these elements will appear twice , Only one number will appear once . The solution you designed Must satisfy O(log n)
Time complexity and O(1)
Spatial complexity .
Topic analysis :1. Ordered array 2. The time complexity is O(logn)———— Two points search
Through the example, it can be found that : The subscript of a single number must be even .
class Solution {
public int singleNonDuplicate(int[] nums) {
// The subscript of a single number must be even
int left=0,right=nums.length-1;
while(left<right){ //left<=right The array index range will be exceeded
int mid=left+(right-left)/2;
if(nums[mid]==nums[mid-1]){// The midpoint is equal to the one on the left
if((mid-left)%2==0){//mid There are even elements on the left of , The subscript of a single number is even , It shows that a single number may exist here
right=mid-2;//-2 Is to exclude nums[mid-1] This element , because nums[mid]==nums[mid-1]
}else{
left=mid+1;// If mid There are only an odd number of elements on the left , Then go directly to the other side to find
}
}else if(nums[mid]==nums[mid+1]){// The midpoint is equal to the one on the right
if((right-mid)%2==0){//mid There are even elements on the right of , It shows that a single number may exist here
left=mid+2;//+2 Is to exclude nums[mid+1] This element , because nums[mid]=nums[mid+1]
}else{
right=mid-1;
}
}else{
return nums[mid];
}
}
return nums[left];
}
}
541、 Reverse string II
Given a string s
And an integer k
, From the beginning of the string , Each count to 2k
Characters , Just reverse this 2k
The first... In the character k
Characters .
- If the remaining characters are less than
k
individual , Reverse all remaining characters . - If the remaining characters are less than
2k
But greater than or equal tok
individual , Before reversalk
Characters , The rest of the characters remain the same .
1. The character inversion of the string is to convert the string into an array
2. From the beginning of the string , Each count to 2k
Characters , Just reverse this 2k
The first... In the character k
Characters . Use the following for Circular thinking , Do not use
int count=1; for(int i=0;i<2*k;i++){ if(count==2*k){ sb.reverse(); } count=count*2; sb.append(s.charAt(i)); }
Use this idea ==>for (int i = 0; i < n; i =i + 2 * k) { // Learn this for loop
reverse(arr, i, Math.min(i + k, n) - 1);
}
3. there Math.min It's to judge If the remaining characters are less than k
individual , Reverse all remaining characters .
class Solution {
public String reverseStr(String s, int k) {
int n = s.length();
char[] arr = s.toCharArray();
for (int i = 0; i < n; i =i + 2 * k) {// Learn this for loop
reverse(arr, i, Math.min(i + k, n) - 1);
}
return new String(arr);
}
public void reverse(char[] arr, int left, int right) {
while (left < right) {
char temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
}
543、 The diameter of a binary tree
Given a binary tree , You need to calculate its diameter and length . Two branches of a tree Diameter length Is one of the path lengths of any two nodes Maximum . This path may or may not pass through the root node .
The longest diameter of the binary tree is 4-2-1-3/5-2-1-3
The longest diameter of the binary tree can be 8-7-5-2-1-3 return 5
namely : The maximum depth of the left and right nodes , The longest diameter is added together
If maxLength Function termination condition 3 The return is return max; because max Depending on left+right, If return max, that right and left As long as there is recursion , They have been changing ( It's changing all the time )【 Example :4 Of left=1,9 Of left=0, So free return left=1, Then it becomes left=0 了 】 that max And it's changing all the time . Should be
return Math.max(left,right)+1, use max To hold the left and right The length of , such max It will be fixed .
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int max=0;
public int diameterOfBinaryTree(TreeNode root) {
if(root==null) return 0;
maxLength(root);
return max;
}
public int maxLength(TreeNode root){
if(root==null) return 0;// Termination conditions 1
if(root.left==null&root.right==null) return 1;// Termination conditions 2
// Recursion of single-layer logic , When you end recursion ,return Termination conditions 2,3 Can be used .
// If the termination condition 3 Of return yes return max, It would be wrong . Outside say
int left=maxLength(root.left);
int right=maxLength(root.right);
max=Math.max(left+right,max);
return Math.max(left,right)+1;// Termination conditions 3
}
}
边栏推荐
- The super fully automated test learning materials sorted out after a long talk with a Tencent eight year old test all night! (full of dry goods
- How can enterprises optimize the best cost of cloud computing?
- 7.1 learning content
- Summary of common tools and technical points of PMP examination
- 使用dnSpy对无源码EXE或DLL进行反编译并且修改
- GUI application: socket network chat room
- Leetcode 121 best time to buy and sell stock (simple)
- Msp32c3 board connection MSSQL method
- QML add gradient animation during state transition
- Function: find the sum of the elements on the main and sub diagonal of the matrix with 5 rows and 5 columns. Note that the elements where the two diagonals intersect are added only once. For example,
猜你喜欢
A malware detection method for checking PLC system using satisfiability modulus theoretical model
How to delete MySQL components using xshell7?
Beijing invites reporters and media
How to set the response description information when the response parameter in swagger is Boolean or integer
我管你什么okr还是kpi,PPT轻松交给你
Analysis and solution of lazyinitializationexception
Audio resource settings for U3D resource management
Network layer - routing
Huawei rip and BFD linkage
@EnableAsync @Async
随机推荐
leetcode 121 Best Time to Buy and Sell Stock 买卖股票的最佳时机(简单)
Future源码一观-JUC系列
[turn] solve the problem of "RSA public key not find" appearing in Navicat premium 15 registration
Pratique technique | analyse et solution des défaillances en ligne (Partie 1)
Function: write function fun to find s=1^k+2^k +3^k ++ The value of n^k, (the cumulative sum of the K power of 1 to the K power of n).
MySQL statement learning record
Notice on Soliciting Opinions on the draft of information security technology mobile Internet application (APP) life cycle security management guide
基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能
QML add gradient animation during state transition
Oracle database knowledge points that cannot be learned (II)
手机异步发送短信验证码解决方案-Celery+redis
Design of database table foreign key
2-redis architecture design to use scenarios - four deployment and operation modes (Part 2)
It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction
0 basic learning C language - nixie tube dynamic scanning display
It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction
CLP information - how does the digital transformation of credit business change from star to finger?
A-Frame虚拟现实开发入门
be based on. NETCORE development blog project starblog - (14) realize theme switching function
Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL