当前位置:网站首页>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 force deduction method summarizes the single elements in the 540 ordered array
- ThinkPHP uses redis to update database tables
- swagger中响应参数为Boolean或是integer如何设置响应描述信息
- Function: store the strings entered in the main function in reverse order. For example, if you input the string "ABCDEFG", you should output "gfedcba".
- 不得不会的Oracle数据库知识点(一)
- Hash table, string hash (special KMP)
- Conditional test, if, case conditional test statements of shell script
- 查询效率提升10倍!3种优化方案,帮你解决MySQL深分页问题
- 2-Redis架构设计到使用场景-四种部署运行模式(下)
- MySQL -- Introduction and use of single line functions
猜你喜欢
Mobile asynchronous sending SMS verification code solution -efficiency+redis
MySQL - use of aggregate functions and group by groups
Ka! Why does the seat belt suddenly fail to pull? After reading these pictures, I can't stop wearing them
AI helps make new breakthroughs in art design plagiarism retrieval! Professor Liu Fang's team paper was employed by ACM mm, a multimedia top-level conference
【.NET+MQTT】.NET6 环境下实现MQTT通信,以及服务端、客户端的双边消息订阅与发布的代码演示
Introduction to unity shader essentials reading notes Chapter III unity shader Foundation
2020-12-02 SSM advanced integration Shang Silicon Valley
OS interrupt mechanism and interrupt handler
Introduction to A-frame virtual reality development
Print diamond pattern
随机推荐
Oracle database knowledge points that cannot be learned (III)
Msp32c3 board connection MSSQL method
Fundamentals of machine learning: feature selection with lasso
Long article review: entropy, free energy, symmetry and dynamics in the brain
Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
SRCNN:Learning a Deep Convolutional Network for Image Super-Resolution
The first training of wechat applet
基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能
Characteristics of ginger
Gnupg website
[common error] custom IP instantiation error
A little understanding of GSLB (global server load balance) technology
mysql使用视图报错,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
PMP 考试常见工具与技术点总结
“疫”起坚守 保障数据中台服务“不打烊”
Force deduction solution summary 1189- maximum number of "balloons"
swagger中响应参数为Boolean或是integer如何设置响应描述信息
I don't care about you. OKR or KPI, PPT is easy for you
Notice on Soliciting Opinions on the draft of information security technology mobile Internet application (APP) life cycle security management guide
How to delete MySQL components using xshell7?