国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

LintCode Interleaving Positive and Negative Numbers

2019-11-08 18:42:16
字體:
供稿:網(wǎng)友

description: Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers.

Notice

You are not necessary to keep the original order of positive integers or negative integers.

Have you met this question in a real interview? Yes Example Given [-1, -2, -3, 4, 5, 6], after re-range, it will be [-1, 5, -2, 4, -3, 6] or any other reasonable answer.

同樣是使用two pointers的算法進(jìn)行處理。但是此時的two pointer的方法是使用的同向的雙指針進(jìn)行的

class Solution { /** * @param A: An integer array. * @return: void */ public void rerange(int[] A) { // write your code here if (A == null || A.length == 0 || A.length == 1) { return; } int pos = 0; int neg = 0; for (int i = 0; i < A.length; i++) { if (A[i] < 0) { neg++; } else { pos++; } } int posind = 1; int negind = 0; if (neg < pos) { posind = 0; negind = 1; } while (posind < A.length && negind < A.length) { if (A[posind] > 0) { posind += 2; } if (A[negind] < 0) { negind += 2; } if (posind < A.length && negind < A.length) { swap(A, posind, negind); } } } PRivate void swap(int[] A, int left, int right) { int temp = A[left]; A[left] = A[right]; A[right] = temp; }}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 同仁县| 广昌县| 曲靖市| 阳信县| 萝北县| 舟山市| 东至县| 朝阳市| 楚雄市| 汪清县| 清远市| 定西市| 宜兴市| 锦屏县| 龙门县| 米泉市| 陆良县| 遂平县| 新源县| 德江县| 云南省| 绵竹市| 嫩江县| 松滋市| 沧州市| 岑溪市| 突泉县| 从化市| 安仁县| 句容市| 武鸣县| 美姑县| 彭水| 奉新县| 潞西市| 喜德县| 临泉县| 秀山| 沂南县| 伊川县| 南召县|