The fourth week ofARTS
Algorithm
题目:992-按奇偶排序
Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even.
Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even.
You may return any answer array that satisfies this condition.
Example 1:
Input: [4,2,5,7] Output: [4,5,2,7] Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
Note:
- 2 <= A.length <= 20000
- A.length % 2 == 0
- 0 <= A[i] <= 1000
思路
新建一个辅助数组,遍历 A,偶数放到偶数索引处,奇数放到奇数索引处。代码:
public int[] sortArrayByParityII(int[] A) { int len = A.length; int[] result = new int[len]; int even = 0; int odd = 1; for(int i=0;i<len;i++){ if(A[i]%2==0){ result[even] = A[i]; even += 2; }else{ result[odd] = A[i]; odd += 2; } } return result; }
借鉴
这里列出从评论中得到的一个思路:遍历数组 A,仅交换不符合要求的元素即可,也就是说,如果偶数索引处的值为奇数,奇数索引处的值为偶数,再交换,否则不进行操作。代码:
public int[] sortArrayByParityII(int[] A) { int j = 1; for (int i = 0; i < A.length - 1; i = i + 2) { if ((A[i] & 1) != 0) { //偶数位为奇数 while ((A[j] & 1) != 0) { //奇数位为奇数 j = j + 2; } //奇数位为偶数才交换 int tmp = A[i]; A[i] = A[j]; A[j] = tmp; } } return A; }
Review
继续阅读 JVM 规范,这里是 JVM 中运行时数据区,原文链接:The Structure of the Java Virtual Machine。
个人翻译。
Tips
这里列出自己从 LeetCode 的一个评论中得到的技巧。
一个偶数和数字 1 按位与的结果一定是 0 ;而奇数和数字 1 按位与的结果一定是 1。 可以通过这种方式来判断奇偶。
System.out.println(5 & 1); // 1 System.out.println(10 & 1); // 0 if( number & 1 == 0){ // TODO number 为偶数情况下执行的操作 }else{ // TODO number 为奇数情况下执行的操作 }
这里列出了其他一些自己知道的位运算的技巧:
- 右移 m 位相当于乘以 2 的 m 次方;左移 m 位相当于除以 2 的 m 次方:
System.out.println(5 >> 2); // 5 / (2^2) = 1 System.out.println(5 << 2); // 5 * (2^2) = 20
- 不用临时变量交换两个数:
案例:a=6,b=5 交换a、b的值 第一步:b = a ^ b = 011; 第二步:a = a ^ b = 110 = 6; 第三部:b = a ^ b = 101 = 5;
- 任何数与 0 异或的结果仍是自身
这里有一个位运算技巧总结的博客:优秀程序员不得不知道的20个位运算技巧
Share
通过一段代码来看 Java 中的初始化过程:
这里列出了总结出来的初始化过程:
- 类初始化过程 一个类要创建实例需要先加载并初始化该类 main方法所在的类需要先加载和初始化
<clinit>方法
<clinit>方法 由静态类变量显式赋值代码和静态代码块组成,这两部分从上到下执行
-
实例初始化就是执行
<init>()方法 该方法可能重载有多个,有几个构造器就有几个该方法
<init>()方法
<init>()方法的首行是
super(参数),即对应父类的
<init>()方法
那么,方法的调用又是怎么样的呢?如果子类对方法重写呢?
Java 中,在 非静态方法前面其实有一个默认的对象this
,它在构造器中标识的是正在创建的对象,由于main中创建的是Son对象,因此Father类中i=test()
执行的是子类重写的test()
方法 。
- the fourth week of febuary 2011
- 20162314 《Program Design & Data Structures》Learning Summary Of The Fifth Week
- IIPP Week 4 - Lists, keyboard input, the basics of modelling motion
- [导入]Guru of the Week:#41 使用标准库
- Guru of the Week 条款00:kingofark和Guru of the Week
- Guru of the Week 条款18:Iterators(迭代子)
- Guru of the Week 条款01: 变量的初始化
- Guru of the Week 条款28:“Fast Pimpl”技术
- Teamwork(The fourth day of the team)
- FHC 2013: The Fourth International Workshop on Frontiers of Heterogeneous Computing
- fwnx -- working bugs in the two week of osp
- 20162314 《Program Design & Data Structures》Learning Summary Of The Eighth Week
- Book Of The Week #20040910 - 《呆伯特企管漫画——我不反商,我只是讨厌上班》
- Guru of the Week 条款23:对象的生存期(第二部分)
- [导入]Guru of the Week:#40 多态控制.
- Guru of the Week 条款14:类之间的关系(上篇)
- Guru of the Week 条款02: 临时对象
- Guru of the Week 条款27:转呼叫函数
- 20162314 《Program Design & Data Structures》Learning Summary Of The Second Week
- the summary of a week #2.7-2.14