Problem 1:
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
public class Solution {
public void reOrderArray(int [] array){
int len = array.length;
if(len == 0)
return;
int j = 0;
for (int i = 0; i < len; i++)
{
j = i;
while (array[j] % 2 == 0)
{
if(j == len - 1)
return;
j++; //once encounter an even number then j++ until come across an odd number
}
if(j != i)
{
int temp = array[j];
for (int k = j; k > i; k--) //move even numbers to the right
{
array[k] = array[k - 1];
}
array[i] = temp; //insert the odd number into array[i] in front of moved even numbers
}
}
return;
}
}
Problem 2:
输入一个链表,输出该链表中倒数第k个结点。
Slow version:
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
int totalNum = 0;
if(head != null){
totalNum++;
}else{
return null;
}
// count sum of listnode
ListNode currentNode = head.next;
while(currentNode != null){
totalNum++;
currentNode = currentNode.next;
}
if(totalNum < k){ // k's value oversize the length of the list
//throw new RuntimeException("k's value oversize the length of the list");
return null;
}
//the (totalNum-k+1)th listnode is the answer
ListNode resultNode = head;
for(int i=1; i<=totalNum-k; i++){
resultNode = resultNode.next;
}
return resultNode;
}
}
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
ListNode fast, slow;
fast = slow = head;
int count = 0;
if(head == null || k == 0)
return null;
else
count++;
while(fast.next != null)
{
if(count >= k)
slow = slow.next;
fast = fast.next;
count++;
}
return count < k ? null : slow;
}
}
Problem 3:
输入一个链表,反转链表后,输出新链表的表头。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null || head.next == null)
return head;
ListNode pre = null;
ListNode next;
while(head != null)
{
next = head.next; //be careful of the order of pointer assignment
head.next = pre;
pre = head;
head = next;
}
return pre; //Attention: return pre not head
}
}
Problem 4:
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public boolean HasSubtree(TreeNode root1,TreeNode root2) {
if(root1 == null || root2 == null)
return false;
return Judge(root1, root2) || HasSubtree(root1.left,root2) || HasSubtree(root1.right,root2);
}
public boolean Judge(TreeNode root1,TreeNode root2){
if(root2 == null) // when root2 tree first traverse end
return true;
if(root1 == null) // when root1 tree first traverse end
return false;
if(root1.val != root2.val)
return false;
return Judge(root1.left, root2.left) && Judge(root1.right, root2.right);
}
}
Problem 5:
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
ListNode head = new ListNode(-1);
ListNode cur = head;
while(list1 != null && list2 != null){
if(list1.val <= list2.val){
cur.next = list1;
list1 = list1.next;
}
else{
cur.next = list2;
list2 = list2.next;
}
cur = cur.next;
}
if(list1 != null) cur.next = list1; // only need to add head pointer of the rest list into new list not necessarily using while loop
if(list2 != null) cur.next = list2;
return head.next;
}
}