面试题11:数值的整数次方
public class Solution {
public double Power(double base, int exponent) {
if(exponent == 0)
return 1.0;
if(exponent == 1)
return base;
double res = Power(base ,exponent/2);
boolean isNeg = false;
if(exponent < 0)
isNeg = true;
exponent = Math.abs(exponent);
res = res * res;
if(exponent % 2 == 1){
if(isNeg == false){
res = res * base;
}else if(isNeg == true){
res = res * 1/base;
}
}
return res;
}
}
面试题14:调整数组顺序使奇数位于偶数前面
public class Solution {
public void reOrderArray(int [] array) {
int len = array.length;
int index = 0;
for(int i=0;i<array.length;i++){
int tmp = array[i];
int j = i-1;
if((array[i]%2)!=0){
while(j>=0 && (array[j]%2)==0){
array[j+1] = array[j];
j--;
}
array[j+1] = tmp;
}
}
}
}
面试题15:链表中倒数第k个结点
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head == null || k==0) return null;
ListNode p1 = head;
ListNode p2 = head;
int i = 0;
while(i<k-1){
if(p2.next != null){
p2 = p2.next;
i++;
}else{
return null;
}
}
while(p2.next!=null){
p1 = p1.next;
p2 = p2.next;
}
return p1;
}
}
面试题16:反转链表
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null) return null;
if(head.next == null) return head;
ListNode pre = null;
ListNode p = head;
while(p != null){
ListNode tmp = p.next;
p.next = pre;
pre = p;
p = tmp;
}
return pre;
}
}
面试题17:合并两个排序的链表
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
if(list1 == null && list2 == null) return null;
ListNode newHead = new ListNode(0);
ListNode p = newHead;
while(list1 != null && list2 != null){
if(list1.val <= list2.val){
p.next = list1;
p = p.next;
list1 = list1.next;
}else{
p.next = list2;
p = p.next;
list2 = list2.next;
}
}
ListNode tmp = (list1==null)?list2:list1;
while(tmp!=null){
p.next = tmp;
p = p.next;
tmp = tmp.next;
}
return newHead.next;
}
}
面试题18:树的子结构
public class Solution {
public boolean HasSubtree(TreeNode root1,TreeNode root2) {
if(root2 == null) return false;
if(root1 == null) return false;
return isSub(root1,root2) || isSub(root1.left,root2) || isSub(root1.right,root2);
}
public boolean isSub(TreeNode root1,TreeNode root2){
if(root2 == null) return true;
if(root1 == null) return false;
if(root1.val != root2.val){
return false;
}else{
return isSub(root1.left,root2.left) && isSub(root1.right,root2.right);
}
}
}
面试题19:二叉树的镜像
public class Solution {
public void Mirror(TreeNode root) {
if(root == null) return;
if(root.left == null && root.right==null) return;
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
if(root.left != null){
Mirror(root.left);
}
if(root.right != null){
Mirror(root.right);
}
}
}
面试题20:顺时针打印矩阵
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printMatrix(int [][] matrix) {
ArrayList<Integer> list = new ArrayList<Integer>();
if(matrix.length == 0) return list;
int m = matrix.length;
int n = matrix[0].length;
int x=0,y=0;
while(m>0 && n>0){
if(m==1){
for(int i=0;i<n;i++){
list.add(matrix[x][y++]);
}
break;
}else if(n == 1){
for(int i=0;i<m;i++){
list.add(matrix[x++][y]);
}
break;
}
for(int i=0;i<n-1;i++){
list.add(matrix[x][y++]);
}
for(int i=0;i<m-1;i++){
list.add(matrix[x++][y]);
}
for(int i=0;i<n-1;i++){
list.add(matrix[x][y--]);
}
for(int i=0;i<m-1;i++){
list.add(matrix[x--][y]);
}
m = m - 2;
n = n - 2;
x = x + 1;
y = y + 1;
}
return list;
}
}