当前位置:网站首页>[nine day training] content III of the problem solution of leetcode question brushing Report
[nine day training] content III of the problem solution of leetcode question brushing Report
2022-07-01 03:26:00 【Ze en】
write in front
Hello everyone , I am a Ze En, I hope after you read it , It can help you , Insufficient, please correct ! Learn and communicate together
2021 Blog star of the year, Internet of things and embedded development TOP5, Zhou Bang 43, General list 3343
This paper is written by Ze En original CSDN First episode If you need to reprint, please inform
Personal home page : Beating soy sauce desu_ Ze En_CSDN Blog
Welcome to → give the thumbs-up + Collection ️ + Leaving a message.
Series column : Nine day training (LeetCode) Algorithm _ Beating soy sauce desu-CSDN Blog
Creation time :2021 : 12 . 13 Japan
️ We are not on the stage of our choice , Acting is not the script we chose
This chapter's blog topic links
- 33. Search rotation sort array - Power button (LeetCode)
- 81. Search rotation sort array II - Power button (LeetCode)
- 153. Look for the minimum value in the rotation sort array - Power button (LeetCode)
- 70. climb stairs - Power button (LeetCode)
- 509. Fibonacci number - Power button (LeetCode)
- 1137. The first N One tibonacci number - Power button (LeetCode)
Catalog
This chapter's blog topic links
Look for the minimum value in the rotation sort array
The first N Fibonacci sequence
Search rotation sort array
subject :
Example :
🧨 Tips :
1 <= nums.length <= 5000-10^4 <= nums[i] <= 10^4- nums Every value in is unmatched
- Ensure data in the title
numsRotated on a previously unknown subscript -10^4 <= target <= 10^4
Solution to the problem
- Direct use of violence , use for Loop traversal , See if num[n] == target, Otherwise return to -1.
int search(int* nums, int numsSize, int target){
unsigned int n = 0;
for(n=0;n<numsSize;n++)
{
if(nums[n] == target)
{
return target;
}
}
return -1;
}Search rotation sort array
subject :
Example :
🧨 Tips :
1 <= nums.length <= 5000-104 <= nums[i] <= 104- Ensure data in the title
numsRotated on a previously unknown subscript -10^4 <= target <= 10^4
Solution to the problem
- This topic is the same as the above topic , But the return value of the traversal of this topic is different from that of the loop that has retreated .
bool search(int* nums, int numsSize, int target){
int n = 0;
for(n=0;n<numsSize;n++)
{
if(nums[n] == target)
{
return true;
}
}
return false;
}Look for the minimum value in the rotation sort array
subject :
Example :
🧨 Tips :
n == nums.length1 <= n <= 5000-5000 <= nums[i] <= 5000numsAll integers in Different from each othernumsIt turns out to be an ascending array , And carried on1tonSecond rotation
Solution to the problem
This topic mainly focuses on binary search , But I use for Loop traversal , Then judge . So as to compare with each other , Then find the minimum value in the array .
int findMin(int* nums, int numsSize){
int i = 0;
int mid = 0;
for(i=1;i<numsSize;i++)
{
if(nums[mid]>nums[i])
mid = i;
}
return nums[mid];
}climb stairs
subject :
Example :
Solution to the problem
use for loop , Then exchange the variable values in it . Last Return value to return , When for When the cycle does not meet the conditions , Be careful : Here's the variable c Initialization must be 1.
int climbStairs(int n) {
int a = 0, b = 0, c = 0;
int i;
for (i = 1; i <= n; i++)
{
a = b; //a = 0,b = 0,a = 1
b = c; //b = 1,
c = a + b; //c = 1,c = 2
}
return c;
}Fibonacci sequence
subject :
Example :
🧨 Tips :
0 <= n <= 30
Solution to the problem
At the beginning of this topic, I thought of using recursion , It's over . In recursive terms , Less code , But the efficiency is relatively low .
Defined recursively as follows :F(0)=0,F(1)=1, F(n)=F(n - 1)+F(n - 2)
int fib(int N){
if (N == 0 || N == 1)
{
return N;
}
return fib(N - 1) + fib(N - 2);
}The first N Fibonacci sequence
subject :
Example :
🧨 Tips :
0 <= n <= 37- The answer is guaranteed to be 32 An integer , namely
answer <= 2^31 - 1.
Solution to the problem
First n = 0 perhaps n == 1 as well as 2, Each is a return 0 and 1 Of . Then do this problem according to this idea : And in n >= 0 Under the condition of Tn+3 = Tn + Tn+1 + Tn+2.
int tribonacci(int n){
if(n==0)
{
return 0;
}
if(n==1||n==2)
{
return 1;
}
int a=0,b=1,c=1;
int d = 0;
int i = 0;
for(i=3;i<=n;i++)
{
d=a+b+c;
a=b;
b=c;
c=d;
}
return d;
}
边栏推荐
- Design of serial port receiving data scheme
- Druid监控统计数据源
- Feign远程调用和Getaway网关
- How the network is connected: Chapter 2 (Part 2) packet receiving and sending operations between IP and Ethernet
- The value of the second servo encoder is linked to the NC virtual axis of Beifu PLC for display
- JS daily development tips (continuous update)
- Mybati SQL statement printing
- go实现命令行的工具cli
- Edge Drawing: A combined real-time edge and segment detector 翻译
- split(),splice(),slice()傻傻分不清楚?
猜你喜欢

Edge Drawing: A combined real-time edge and segment detector 翻译

Latest interface automation interview questions

Edge drawing: a combined real-time edge and segment detector

Redis高效点赞与取消功能

VMware vSphere 6.7 virtualization cloud management 12. Vcsa6.7 update vCenter server license

C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display

Analyze datahub, a new generation metadata platform of 4.7K star

C#实现基于广度优先BFS求解无权图最短路径----完整程序展示

Redis efficient like and cancel function

Druid monitoring statistics source
随机推荐
MySQL index --01--- design principle of index
Data exchange JSON
Depth first traversal of C implementation Diagram -- non recursive code
So easy 将程序部署到服务器
10、Scanner.next() 无法读取空格/indexOf -1
Server rendering technology JSP
Introduction to ieda right click source file menu
md5sum操作
Finally in promise
Filter
The value of the second servo encoder is linked to the NC virtual axis of Beifu PLC for display
Design practice of current limiting components
打包iso文件的话,怎样使用hybrid格式输出?isohybrid:command not found
后台系统右边内容如何出现滚动条和解决双滚动条的问题
Split(), split(), slice(), can't you tell?
POI导出excel,按照父子节点进行分级显示
线程数据共享和安全 -ThreadLocal
力扣-两数之和
Introduction and installation of Solr
Ridge regression and lasso regression