Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C++ to find the first positive integer

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use C++ to find the first positive integer". In daily operation, I believe many people have doubts about how to use C++ to find the first positive integer. The editor consulted all kinds of data and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the doubt of "how to find the first positive integer with C++". Next, please follow the editor to study!

1. The problem description gives you an unsorted array of integers. Please find out the smallest positive integer that does not appear. Output: 3 example 2: input: [3pyrrine 4magory 1] output: 2Example 3: input: [7recover8jurisdiction 9pj11] output: 1 hint: the time complexity of your algorithm should be O (n), and you can only use extra space at the constant level. two。 Problem solving ideas / * problem solving ideas: 1. The first positive number is an integer greater than 0, such as 1, 2, 3, 4, 5. .2, put the number in the correct position: nums [I]: stored in the subscript nums [I]-1 position, do not comply with the rule to skip 3, traverse the array return the first: nums [I]! = iTunes 1, is the missing first positive number, otherwise return numsSize+1*/3. Test result

4. Solving 1int firstMissingPositive (int* nums, int numsSize) {/ / mid temporary variables for data exchange int mid = 0; / / traversing nums array for (int I = 0; I)

< numsSize; i++) { //将 nums[i]:存储在下标为nums[i]-1位置,不符合该规则的跳过 while ((nums[i] >

0) & (nums [I] < numsSize+1) & & (nums [I]! = nums [nums [I]-1]) {mid = nums [I]; nums [I] = nums [mid-1]; nums [mid-1] = mid;}} / / sets the return value variable res, and returns numsSize+1 int res = numsSize+1 if all values are correct / / iterate through the array and return the first incorrect number for (int I = 0; I < numsSize; inumbers +) {if (nums [I]! = I + 1) {res = I + 1; break;}} return res;} so far, the study on "how to find the first positive integer with C++" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report