[leetcode]Interleaving Positive and Negative Numbers

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

Note

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

Example

Given [-1, -2, -3, 4, 5, 6], after re-range, it will be [-1, 5, -2, 4, -3, 6] or any other legal answer.

Challenge

Do it in-place and without extra memory.

看了网上的一些解法,是很巧妙,但是编程起来感觉没自己的这个更清晰些!分享下咯

class Solution {
    /**
     * @param A: An integer array.
     * @return an integer array
     */
    public int[] rerange(int[] A) {
        if(A==null || A.length<=1)
            return A;

        int s = 0;
        int p1 = -1;
        int p2 = -1;
        for(int i=0;i<A.length;i++){
            if(p1 == -1 && A[i] > 0)
                p1 = i;
            if(p1 == -1 && A[i] < 0)
                p2 = i;
            s += A[i] > 0?1:-1;
        }
        if(s<0)
            p1 = p2;

        int tmp = A[0];
        A[0] = A[p1];
        A[p1] = tmp;

        for(int i=1;i<A.length;i++){
            int j=i;
            while(j<A.length && A[j] * A[i-1]>0)
                j++;
            if(j>i && j<A.length){
                tmp = A[i];
                A[i] = A[j];
                A[j] = tmp;
            }
        }
        return A;
   }
}
时间: 2024-12-18 22:18:56

[leetcode]Interleaving Positive and Negative Numbers的相关文章

Lintcode: Interleaving Positive and Negative Numbers 解题报告

Interleaving Positive and Negative Numbers 原题链接 : http://lintcode.com/zh-cn/problem/interleaving-positive-and-negative-numbers/ Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers. 注意 Yo

Interleaving Positive and Negative Numbers

Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers. Note: You are not necessary to keep the original order of positive integers or negative integers. Example Given [-1, -2, -3, 4, 5, 6]

LintCode-Interleaving Positive and Negative Numbers.

Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers. Note You are not necessary to keep the original order or positive integers or negative integers. Example Given [-1, -2, -3, 4, 5, 6],

LeetCode: Interleaving String

LeetCode: Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc"

[leetcode]Interleaving String @ Python

原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = "aabcc",s2 = "dbbca", When s3 = "aadbbcbcac", return true.Whe

[leetcode]_Sum Root to Leaf Numbers

题目:计算一棵二叉树所有路径组成的数的总和. 思考:也是DFS的基础应用.虽然还是套着别人的DFS框架写的,但是学习通常会经历先模拟,再创新的过程. 代码: 1 private int sum = 0; 2 public int sumNumbers(TreeNode root) { 3 dfs(root , 0); 4 return sum; 5 } 6 public void dfs(TreeNode node , int tempSum){ 7 if(node == null) retur

[LeetCode] Interleaving String(dp)

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 分析:非常好的DP的训练题,

True Positive|True Negative|False Positive|False Negative

True Positive|True Negative|False Positive|False Negative 表示分类正确: True Positive:本来是正样例,分类成正样例. True Negative:本来是负样例,分类成负样例. 表示分类错误: False Positive :本来是负样例,分类成正样例,通常叫误报. False Negative:本来是正样例,分类成负样例,通常叫漏报. true positive rate = true positive / (true po

LeetCode: Interleaving String [097]

[题目] Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. [题意] 给定字符