新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
LeetCode中怎么實(shí)現(xiàn)按奇偶排序數(shù)組
這篇文章給大家介紹LeetCode中怎么實(shí)現(xiàn)按奇偶排序數(shù)組,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
在山陽等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都做網(wǎng)站、成都網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需定制網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),網(wǎng)絡(luò)營(yíng)銷推廣,外貿(mào)網(wǎng)站建設(shè),山陽網(wǎng)站建設(shè)費(fèi)用合理。
先聲明一個(gè)數(shù)組 result
用來存放最終結(jié)果然后遍歷數(shù)組兩次,第一次將偶數(shù)放入 result
中,第二次將奇數(shù)存入result
中主要進(jìn)行的是遍歷操作,此時(shí)時(shí)間復(fù)雜度為 .
4. 實(shí)現(xiàn)
public int[] sortArrayByParity(int[] A) {
// 存放最終結(jié)果
int[] result = new int[A.length];
int index = 0;
// 第一次遍歷,將偶數(shù)存入 result 數(shù)組
for (int item : A) {
if (item % 2 == 0) {
result[index++] = item;
}
}
// 第二次遍歷,將奇數(shù)存入 result 數(shù)組
for (int item : A) {
if (item % 2 != 0) {
result[index++] = item;
}
}
return result;
}
關(guān)于LeetCode中怎么實(shí)現(xiàn)按奇偶排序數(shù)組就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)站名稱:LeetCode中怎么實(shí)現(xiàn)按奇偶排序數(shù)組
地址分享:http://fisionsoft.com.cn/article/pgcsss.html