国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 乐山市| 新津县| 会宁县| 朝阳区| 平安县| 永安市| 井冈山市| 宿松县| 昌黎县| 惠安县| 改则县| 焉耆| 三都| 叙永县| 临武县| 阜宁县| 霞浦县| 东海县| 太仆寺旗| 阿尔山市| 丰都县| 沂水县| 抚宁县| 铁力市| 隆子县| 慈溪市| 新郑市| 建昌县| 炎陵县| 日土县| 庄浪县| 朝阳县| 视频| 盖州市| 绥化市| 白沙| 乌兰浩特市| 山阴县| 仁怀市| 浪卡子县| 报价|