第二十五题:复杂链表的复制
题目描述:
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
思路:
第一步:扫描原链表,把复制的结点就连在原结点的后面;例如:(原链表)A->B->C —–> A->A’->B->B’->C->C’
第二步:扫描原链表,如果结点有random指向,则把对应的复制结点的random指向对应的复制结点;例如:A指向C,则把A’指向C’。因为都是连在一个链表上的,所有能以o(1)的时间复杂度找到。
第三步:
分离两个链表,注意空指针的指向即可。
代码:
/*
public class RandomListNode {
int label;
RandomListNode next = null;
RandomListNode random = null;
RandomListNode(int label) {
this.label = label;
}
}
*/
public class Solution {
public RandomListNode Clone(RandomListNode pHead)
{
if (pHead == null) {
return null;
}
clone(pHead);
random(pHead);
return getRandomListNode(pHead);
}
public void clone(RandomListNode pHead){
while (pHead != null) {
RandomListNode cloneNode = new RandomListNode(pHead.label);
cloneNode.next = pHead.next;
pHead.next = cloneNode;
pHead = cloneNode.next;
}
}
public void random(RandomListNode pHead) {
while (pHead != null) {
if (pHead.random != null) {
pHead.next.random = pHead.random.next;
}
pHead = pHead.next.next;
}
}
public RandomListNode getRandomListNode(RandomListNode pHead) {
RandomListNode p1 = pHead.next;
RandomListNode ret = pHead.next;
pHead.next = p1.next;
pHead = pHead.next;
while (pHead != null) {
p1.next = pHead.next;
p1 = p1.next;
pHead.next = p1.next;
pHead = pHead.next;
}
return ret;
}
}
第二十六题:二叉搜索树与双向链表
题目描述:
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
思路:
由于是二叉搜索树,要转换成一个排序的双向链表,那么对这棵二叉树进行中序遍历得到的排序的链表。
代码:
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
protected TreeNode lastLeft = null;
public TreeNode Convert(TreeNode pRootOfTree) {
if (pRootOfTree == null) {
return null;
}
if (pRootOfTree.left == null && pRootOfTree.right == null) {
lastLeft = pRootOfTree;
return pRootOfTree;
}
TreeNode left = Convert(pRootOfTree.left);
if (lastLeft != null) {
pRootOfTree.left = lastLeft;
lastLeft.right = pRootOfTree;
}
lastLeft = pRootOfTree;
TreeNode right = Convert(pRootOfTree.right);
if(right != null) {
pRootOfTree.right = right;
right.left = pRootOfTree;
}
return left != null ? left : pRootOfTree;
}
}
第二十七题:字符串的排列
题目描述:
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
思路:
其实就是模拟一个排列组合的过程,比如字符串abc,先对0号位进行a,b,c三个字母的轮换,a是首位的情况下,b和c组合后输出,即输出abc,acb;然后b是首位的情况下,ac组合输出。
所以很自然地就会发现整个过程需要用到交换函数,需要递归调用。
代码:
import java.util.ArrayList;
import java.util.Collections;
public class Solution {
public ArrayList<String> Permutation(String str) {
ArrayList<String> ret = new ArrayList<String>();
if (str == null || str.length() == 0) {
return ret;
}
permutation(ret, 0, str.toCharArray());
Collections.sort(ret);
return ret;
}
public void permutation(ArrayList<String> retArrayList, int index, char[] s) {
if (index == s.length - 1) {
retArrayList.add(new String(s));
}else {
for (int i = index; i < s.length; i++) {
if (i == index || s[i] != s[index]) {
swap(s, index, i);
permutation(retArrayList, index+1, s);
swap(s, index, i);
}
}
}
}
public void swap(char[] s, int begin, int end) {
char tmp = s[begin];
s[begin] = s[end];
s[end] = tmp;
}
}
第二十八题:数组中出现次数超过一半的数字
题目描述:
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。
思路:
由于这个数字出现的次数超过了一半,那么我们可以扫描整个数组,得到 一个数字就记录该数字,并记录次数为1,当有出现该数字时次数加1,出现其他数字时,如果次数大于1则减1,否则换一个数字,并记录该数字次数为1;当遍历完整个数组后,如果此数组有一个数字次数大于一半那么只有可能是最后得到的数字。
代码:
public class Solution {
public int MoreThanHalfNum_Solution(int [] array) {
if (array == null || array.length == 0) {
return 0;
}
if(array.length == 1) {
return array[0];
}
int tmp = array[0];
int count = 1;
for (int i = 1; i<array.length; i++) {
if(tmp == array[i]) {
count ++;
}else {
if(count > 1){
count --;
}else{
tmp = array[i];
count = 1;
}
}
}
if(!CheckMoreThanHalf(array, tmp)){
return 0;
}
return tmp;
}
public boolean CheckMoreThanHalf(int[] array, int tmp) {
int count = 0;
boolean ret = false;
for (int i = 0; i < array.length; i++) {
if (array[i] == tmp) {
count ++;
}
}
if(count*2 > array.length) {
ret = true;
}
return ret;
}
}