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 sort arrays by parity in LeetCode

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces how to sort the array by parity in LeetCode, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.

Train of thought

First declare an array result to store the final result, and then traverse the array twice, the first time the even number into the result, the second time the odd number into the result is mainly traversal operation, when the time complexity is .4. Implement public int [] sortArrayByParity (int [] A) {

/ / store the final result

Int [] result = new int [A.length]

Int index = 0

/ / for the first time, even numbers are stored in the result array.

For (int item: a) {

If (item% 2 = = 0) {

Result [index++] = item

}

}

/ / the second traversal, storing odd numbers in the result array

For (int item: a) {

If (item% 2! = 0) {

Result [index++] = item

}

}

Return result

}

On how to sort the array by parity in LeetCode is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report