`
wolf521hf
  • 浏览: 888 次
  • 性别: Icon_minigender_1
  • 来自: 芮城
最近访客 更多访客>>
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
log4j.properties
log4j.rootLogger=debug, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug

log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug,stdout 
输入一个整数数组,调整数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分。要求时间复杂度为O(n) 数组、奇数偶数
package org.base.array;

public class ArrayUtil {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] source = {1,3,2,4,6,7,5};
        int[] result = new ArrayUtil().unevenBeforeOfEven(source);
        for(int i = 0;i<result.length;i++)
        {
        	System.out.print(result[i]+" ");
        }
	}
	/**
	 * 输入一个整数数组,调整数组中数字的顺序,使得所有奇数位于数组的前半部分,
	 * 所有偶数位于数组的后半部分。要求时间复杂度为O(n)
	 * @param arraySource
	 * @return
	 */
	public int[] unevenBeforeOfEven(int[] arraySource)
	{
		if(null == arraySource || 0 == arraySource.length)
		{
			return null;
		}
		int temp = 0;
		int start = 0;
		int end =  arraySource.length - 1;
		while (start < end)
		{
			if(!isEven(arraySource[start]))
			{
				start++;
				continue;				
			}
			if(isEven(arraySource[end]))
			{
				end--;
				continue;
			}
			temp = arraySource[start];
			arraySource[start] = arraySource[end];
			arraySource[end]= temp;
			start++;
			end--;
		}
		return arraySource;
	}
	/**
	 * 判断输入的整数是否为偶数,如果是偶数则返回true 否则返回false
	 * 函数isEven判断一个数字是不是偶数并没有用%运算符而是用&。理由是通常情况下位运算符比%要快一些
	 * @param numberSource
	 * @return
	 */
	public boolean isEven(int numberSource)
	{
		return (numberSource & 1) == 0;
	}

}
Global site tag (gtag.js) - Google Analytics