当前位置:网站首页>[C language questions -- leetcode 12 questions] take you off and fly into the garbage
[C language questions -- leetcode 12 questions] take you off and fly into the garbage
2022-06-24 15:42:00 【Ordinary people 1】
author :@ Ordinary person 1
special column :《 Little bird likes to brush questions 》
In a word : In the past , All is prologue
explain : The past is irreparable , The future can change
Thank you for your praise and attention , At the same time, you are welcome to visit my Ordinary house
Brush the topic ——Leetcode12 A simple question , Through this 12 A simple question , Make you right Leetcode New understanding , Enhance your ability to do questions .
below , Go straight to our topic .
List of articles
- 260. A number that appears only once III( difficulty : secondary )
- 728. Self divisor
- 922. Sort arrays by parity II
- 976. The maximum circumference of a triangle
- 1287. The number of occurrences in an ordered array exceeds 25% The elements of
- 1351. Count the negative numbers in an ordered matrix
- 1903. The largest odd number in a string
- 1979. Find the largest common divisor of the array
- 2089. Find the target subscript after array sorting
- 2124. Check all A All in B Before
- 2180. Count the number of integers whose sum of figures is even
- 2278. Percentage of letters in string
- summary
260. A number that appears only once III( difficulty : secondary )
Given an array of integers nums, There are exactly two elements that only appear once , All the other elements appear twice . Find the two elements that only appear once . You can press In any order Return to the answer .
Advanced : Your algorithm should have linear time complexity . Can you just use constant space complexity to achieve ?
Example 1:
Input :nums = [1,2,1,3,2,5]
Output :[3,5]
explain :[5, 3] It's also a valid answer .
Example 2:Input :nums = [-1,0]
Output :[-1,0]
Example 3:Input :nums = [0,1]
Output :[1,0]source : Power button (LeetCode)
This question Leetcode The difficulty is classified as medium , But I think we can still do it (bushi):
For the convenience of explanation, the variables here use the names I named in the process of doing the questions
Their thinking : First , We define a variable (eor) Initialize to 0 To traverse all the elements in a bitwise XOR array , At this point, we get the binary XOR result of two elements that only appear once . And then to process the binary results : For two different numbers , The binary result of XOR is 1 The words of : The number in this binary bit is different ( We can define a variable rightone To find the rightmost bit in the binary result at this time 1 The location of , As for how to find it, just look at the code , I'm not going to explain it here ). Based on this , We are dividing the elements of an array into two categories : One is the same element in the binary bit , The other is the element that is different in the binary bit . And then we're going through the array once ,& Bitwise AND rightone Find one of the numbers onlyone, As for the other number, it is directly based on the result of the first exclusive or of two numbers eor On XOR onlyone It can be concluded that .
below , Look at our code :

( reminder : If the variable here is defined as int Type words , An overflow error will occur , Don't ask me why I know , Because at the beginning, my own is int type , So instead of long The type is appropriate )
Now proceed to submit and run :

728. Self divisor
Self divisor It refers to the number that can be divided by every digit it contains .
for example ,128 It's a Self divisor , because 128 % 1 == 0,128 % 2 == 0,128 % 8 == 0.
Self divisor Inclusion is not allowed 0 .Given two integers left and right , Return a list , The elements of the list are ranges [left, right] All of the inside Self divisor .
Example 1:
Input :left = 1, right = 22
Output :[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
Example 2:Input :left = 47, right = 85
Output :[48,55,66,77]source : Power button (LeetCode)
How to say this question , The key is to find out every bit of a number , Let's see if it's divisible . For convenience , You can encapsulate a function to handle :


== Ben rookie This is what we do emm, as for bosses == I don't know
922. Sort arrays by parity II
Given an array of nonnegative integers nums, nums The middle half integer is Odd number , Half the whole number is even numbers .
Sort the array , For convenience nums[i] In an odd number of ,i It's also Odd number ; When nums[i] For even when , i It's also even numbers .
You can go back to Any array that meets the above conditions is the answer .
Example 1:
Input :nums = [4,2,5,7]
Output :[4,5,2,7]
explain :[4,7,2,5],[2,5,4,7],[2,7,4,5] It will also be accepted .
Example 2:Input :nums = [2,3]
Output :[2,3]source : Power button (LeetCode)
Find half the odd numbers in the array , Find out the even number of the other half , A little manipulation of the subscript will solve the problem :


976. The maximum circumference of a triangle
Given by some positive numbers ( Representative length ) Array of components nums , return Consisting of three lengths 、 The maximum perimeter of a triangle whose area is not zero . If we can't form any triangle with non-zero area , return 0.
Example 1:
Input :nums = [2,1,2]
Output :5
Example 2:Input :nums = [1,2,1]
Output :0source : Power button (LeetCode)
Their thinking : Put the array in order , find 3 The biggest element , Judge whether it can form a triangle :

1287. The number of occurrences in an ordered array exceeds 25% The elements of
Give you a non decreasing Orderly An array of integers , It is known that there is exactly one integer in this array , It occurs more than the total number of elements in the array 25%.
Please find and return this integer
Example :
Input :arr = [1,2,2,6,6,6,6,7,10]
Output :6source : Power button (LeetCode)
Literally , Just do the questions directly :

1351. Count the negative numbers in an ordered matrix
To give you one m * n Matrix grid, The elements of a matrix, whether by row or column , All in non increasing order . Please count and return grid in negative Number of .
Example 1:
Input :grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
Output :8
explain : Common matrix 8 Negative numbers .
Example 2:Input :grid = [[3,2],[1,0]]
Output :0source : Power button (LeetCode)
Traverse you

1903. The largest odd number in a string
Give you a string num , Represents a large integer . Please write in the string num All of the Non empty substring To find out in the The odd number with the largest value , And returns... As a string . If there is no odd number , Then return an empty string “” .
Substring Is a continuous sequence of characters in a string .
Example 1:
Input :num = “52”
Output :“5”
explain : Non empty substrings have only “5”、“2” and “52” .“5” It's the only odd number .
Example 2:Input :num = “4206”
Output :“”
explain : stay “4206” There is no odd number in .
Example 3:Input :num = “35427”
Output :“35427”
explain :“35427” It's an odd number in itself .source : Power button (LeetCode)
It is more convenient to traverse from back to front , Just the last one ( Relatively speaking , The last one at present ) An odd number means that it is an odd number >, Let's start the code directly :

1979. Find the largest common divisor of the array
Give you an array of integers nums , Returns the maximum number and the minimum number of decimals in the array greatest common divisor .
Two numbers of greatest common divisor Is the largest positive integer that can be divided by two numbers .
Example 1:
Input :nums = [2,5,6,9,10]
Output :2
explain :
nums The smallest number is 2
nums The biggest number is 10
2 and 10 The greatest common divisor of 2
Example 2:Input :nums = [7,5,6,8,3]
Output :1
explain :
nums The smallest number is 3
nums The biggest number is 8
3 and 8 The greatest common divisor of 1
Example 3:Input :nums = [3,3]
Output :3
explain :
nums The smallest number is 3
nums The biggest number is 3
3 and 3 The greatest common divisor of 3source : Power button (LeetCode)
Their thinking : After sorting the array , The first is the smallest element , The last dimension is the largest element , The next step is to find the greatest common divisor ( direct division algorithm Just do it ):

2089. Find the target subscript after array sorting
I'll give you a subscript from 0 The starting array of integers nums And a target element target .
Target subscript It's a satisfaction nums[i] == target The subscript i .
take nums Press The decreasing After sequencing , Return from nums A list of target subscripts in . If there is no target subscript , Return to one empty list . The returned list must be Increasing Sequential arrangement .
Example 1:
Input :nums = [1,2,5,2,3], target = 2
Output :[1,2]
explain : After ordering ,nums Turn into [1,2,2,3,5] .
Satisfy nums[i] == 2 The subscript of is 1 and 2 .
Example 2:Input :nums = [1,2,5,2,3], target = 3
Output :[3]
explain : After ordering ,nums Turn into [1,2,2,3,5] .
Satisfy nums[i] == 3 The subscript of is 3 .
Example 3:Input :nums = [1,2,5,2,3], target = 5
Output :[4]
explain : After ordering ,nums Turn into [1,2,2,3,5] .
Satisfy nums[i] == 5 The subscript of is 4 .
Example 4:Input :nums = [1,2,5,2,3], target = 4
Output :[]
explain :nums Does not contain a value of 4 The elements of .source : Power button (LeetCode)
take nums Press The decreasing After sequencing , This sentence shows that we need to sort first , Arrays are unordered , Later, you can traverse and search :

Submit run :

2124. Check all A All in B Before
To give you one only By the character ‘a’ and ‘b’ Composed string s . If in the string Every ‘a’ All appear in Every ‘b’ Before , return true ; otherwise , return false .
Example 1:
Input :s = “aaabbb”
Output :true
explain :
‘a’ In the subscript 0、1 and 2 ; and ‘b’ In the subscript 3、4 and 5 .
therefore , Every ‘a’ In every ‘b’ Before , So back true .
Example 2:Input :s = “abab”
Output :false
explain :
There is one. ‘a’ In the subscript 2 , And one ‘b’ In the subscript 1 .
therefore , Can't satisfy everyone ‘a’ In every ‘b’ Before , So back false .
Example 3:Input :s = “bbb”
Output :true
explain :
non-existent ‘a’ , Therefore, it can be regarded as every ‘a’ In every ‘b’ Before , So back true .source : Power button (LeetCode)
Their thinking :a Is it all there b Before , So we define two variables , One for 0 As a judgment b The subscript , For another 1 As a judgment a The subscript , Just compare directly :

2180. Count the number of integers whose sum of figures is even
Give you a positive integer num , Please count and return Less than or equal to num And the sum of the figures is even numbers The number of positive integers of .
Positive integer The sum of your numbers Is the result of the addition of the corresponding numbers on all its bits .
Example 1:
Input :num = 4
Output :2
explain :
Only 2 and 4 Satisfy less than or equal to 4 And the sum of the numbers is even .
Example 2:Input :num = 30
Output :14
explain :
Only 14 Integers must be less than or equal to 30 And the sum of the numbers is even , Namely :
2、4、6、8、11、13、15、17、19、20、22、24、26 and 28 .source : Power button (LeetCode)
Find out that the sum of the figures is even numbers A positive integer of , Encapsulated into a function for easy operation :

2278. Percentage of letters in string
Give you a string s And a character letter , Back in the s Medium is equal to letter Of characters percentage , Round down to the nearest percentage .
Example 1:
Input :s = “foobar”, letter = “o”
Output :33
explain :
Equal to letter ‘o’ The characters in s The percentage of is 2 / 6 * 100% = 33% , Rounding down , So back 33 .
Example 2:Input :s = “jjjj”, letter = “k”
Output :0
explain :
Equal to letter ‘k’ The characters in s The percentage of is 0% , So back 0 .source : Power button (LeetCode)
There's nothing to say , We're done

summary
Bloggers have limited ability . Maybe some questions have better practices , But now the blogger is a rookie , If you can do it ok 了 . meanwhile , We should also sum up more , Convenient for your review at the same time , Also share your learning process , This is also very good . That's all for now , Thank you for visiting

边栏推荐
- Redis highly available
- CVPR 2022 - Interpretation of selected papers of meituan technical team
- How to expand disk space on AWS host
- Differential privacy
- Is it safe for futures companies to open accounts
- "Industry foresight" future development trend of intelligent security monitoring industry
- Very exciting! 12000 words summarized the theory of network technology, reviewing the old and learning the new
- Wi-Fi 7 来啦,它到底有多强?
- The catch-up of domestic chips has scared Qualcomm, the leader of mobile phone chips in the United States, and made moves to cope with the competition
- Firefox browser uses plug-ins to set up proxy
猜你喜欢

Why is it easy for enterprises to fail in implementing WMS warehouse management system

还在担心漏测吗?快来使用jacoco统计下代码覆盖率

Three solutions for Jenkins image failing to update plug-in Center

How to expand disk space on AWS host

如何扩展aws主机上的磁盘空间

Here comes Wi Fi 7. How strong is it?

CVPR 2022 - Interpretation of selected papers of meituan technical team

【C语言刷题——Leetcode12道题】带你起飞,飞进垃圾堆

【我的OpenGL学习进阶之旅】OpenGL的坐标系的学习笔记

Recommend several super practical data analysis tools
随机推荐
Design of vga/lcd display controller system based on FPGA (Part 2)
Es search content top
Which account of Dongfang fortune is safer and better
Istio Troubleshooting: using istio to reserve ports causes pod startup failure
"Industry outlook" analysis of five major trends in China's security video surveillance industry
Junit5中的参数化测试(Parameterized Tests)指南
Recommend several super practical data analysis tools
Design of CAN bus controller based on FPGA (Part 2)
PHP application container deployment practice
Three solutions for Jenkins image failing to update plug-in Center
US Senate promotes bipartisan gun safety bill
如何在Thymeleaf3标签中使用嵌套标签
设备通过国标GB28181接入EasyCVR平台,出现断流情况该如何解决?
Istio practical tips: Customize Max_ body_ size
How to efficiently transfer enterprise business data?
The first in China! Tencent cloud key management system passes password application verification test
At? Let's blow the air conditioner together!
大智慧开户要选什么证券公司比较好,更安全一点
07. Tencent cloud IOT device side learning - Data Template
推荐几款超级实用的数据分析利器