剑指offer面试题 java解答26-30
来源:互联网 发布:linux挂载iso镜像 yum 编辑:程序博客网 时间:2024/06/06 17:37
面试题26:复杂链表的复制
import java.util.HashMap;public class Test26{ private class Node<T>{ T valueT=null; Node next=null; Node sibling=null; public Node(T valueT) { this.valueT = valueT; } } private Node head=null; public Node getHeadNode(){ return head; } public <T> Node insertTail(T value){ Node nextNode=null; if (head==null) { head=new Node(value); return head; }else { nextNode=head; while (nextNode.next!=null) { nextNode=nextNode.next; } nextNode.next=new Node(value); return nextNode.next; } } private Node ReconnectNodes(Node head) { Node nowNode=head; Node cloneNodeHead=null; Node cloneNode=null; if (head!=null) { cloneNodeHead=nowNode.next; cloneNode=cloneNodeHead; nowNode=cloneNodeHead.next; while (nowNode!=null) { cloneNode.next=nowNode.next; cloneNode=nowNode.next; nowNode=cloneNode.next; } } return cloneNodeHead; } private void ConnectSiblingNodes(Node head) { if (head!=null) { Node nowNode=head; while (nowNode!=null) { Node nowCloneNode=nowNode.next; if (nowNode.sibling!=null) { nowCloneNode.sibling=nowNode.sibling.next; } nowNode=nowCloneNode.next; } } } private void CloneNodes(Node head) { if (head!=null) { Node nowNode=head; while (nowNode.next!=null) { Node cloneNode=new Node(nowNode.valueT); Node nextNode=nowNode.next; nowNode.next=cloneNode; cloneNode.next=nextNode; nowNode=nextNode; } Node cloneNode=new Node(nowNode.valueT); nowNode.next=cloneNode; } } public Node Clone(Node head){ CloneNodes(head); ConnectSiblingNodes(head); return ReconnectNodes(head); } public static void main(String[] args) { HashMap<Character, Object> map=new HashMap<Character, Object>(); Object[] a={'A','B','C','D','E'}; Test26 t=new Test26(); for (int i = 0; i < a.length; i++) { Node node=t.insertTail(a[i]); map.put((Character) a[i], node); } ((Node)(map.get('A'))).sibling=((Node)(map.get('C'))); ((Node)(map.get('B'))).sibling=((Node)(map.get('E'))); ((Node)(map.get('D'))).sibling=((Node)(map.get('B'))); Node cloneHead=t.Clone(t.getHeadNode()); if (cloneHead!=null) { while (cloneHead!=null) { System.out.print(cloneHead.valueT); if (cloneHead.sibling!=null) { System.out.println(cloneHead.sibling.valueT); }else { System.out.println(); } cloneHead=cloneHead.next; } } }}
面试题27:二叉搜索树与双向链表
public class Test27{ private class Node<T> { public T value; public Node<T> lChild; public Node<T> rChild; public Node(T value) { this.value = value; } } public Node root=null; private int pos = 0; public <T> void creatBiTree(T[] value){ pos=0; root=creatBiTree(root, value); } // 以node为根节点,创建一棵树,返回此根节点 private <T> Node creatBiTree(Node node, T[] value) { T t = value[pos]; pos++; if (t == null) { return null; } else { node = new Node<T>(t); node.lChild = creatBiTree(node.lChild, value); node.rChild = creatBiTree(node.rChild, value); } return node; } public Node Convert(Node root){ Node lastNode=null; Node tailNode=ConvertNode(root, lastNode); Node head=tailNode; while (head.lChild!=null) { head=head.lChild; } return head; } private Node ConvertNode(Node nowNode,Node lastNode){ if (nowNode==null) { return null; } if (nowNode.lChild!=null) { lastNode=ConvertNode(nowNode.lChild, lastNode); } nowNode.lChild=lastNode; if (lastNode!=null) { lastNode.rChild=nowNode; } lastNode=nowNode; if (nowNode.rChild!=null) { lastNode=ConvertNode(nowNode.rChild, lastNode); } return lastNode; } public static void main(String[] args) { Object[] a = { 10, 6, 4, null, null, 8, null, null, 14, 12, null, null, 16, null, null }; Test27 t = new Test27(); t.creatBiTree(a); Node head=t.Convert(t.root); while (head!=null) { System.out.println(head.value); head=head.rChild; } }}
面试题28:字符串的排列
public class Test28 { public void Permutation(String str){ permutation(str, 0); } //第i个字符和后面的全排列交换 private static void permutation(String s, int i) { char[] str=s.toCharArray(); if (i>s.length()) { return ; }else if (i==s.length()) { System.out.println(String.valueOf(str)); }else { for (int j = i; j < str.length; j++) { char tmp=str[j]; str[j]=str[i]; str[i]=tmp; permutation(String.valueOf(str), i+1); } } } public static void main(String[] args) { Test28 t=new Test28(); String str="abcd"; t.Permutation(str); }}
面试题29:数组中出现次数超过一半的数字
public class Test29 { public int MoreThanHalfNum(int[] numbers) throws Exception{ if (numbers==null) { throw new Exception("invalid input"); } int number=numbers[0]; int times=1; for (int i = 1; i < numbers.length; i++) { if (numbers[i]!=number) { if (--times==0) { number=numbers[i]; times=1; } }else { times++; } } int sum=0; for (int i = 0; i < numbers.length; i++) { if (numbers[i]==number) { sum++; } } if (sum>numbers.length/2) { return number; }else { throw new Exception("without"); } } public static void main(String[] args) throws Exception { int[] numbers={1,2,3,2,2,2,5,4,2}; Test29 t=new Test29(); int number=t.MoreThanHalfNum(numbers); System.out.println(number); }}
面试题30:最小的K个数
public class Test30 { private static int leftChild(int i) { return 2*i+1; } //下滤实现大顶堆 public static <T extends Comparable<? super T>> void percolateDownSort(T[] a,int i,int n) { int child; T tmp; for (tmp=a[i];leftChild(i)<n;i=child) { child=leftChild(i); if (child<n-1&&a[child].compareTo(a[child+1])<0) { child++; } if (tmp.compareTo(a[child])<0) { a[i]=a[child]; }else { break; } } a[i]=tmp; } public static <T extends Comparable<? super T>> T returnMax(T[] a) { int length=a.length; percolateDownSort(a, 0, length); return a[0]; } public Integer[] getLeastNumbers(int k,Integer[] a) throws Exception{ if (a==null||k<0||k>a.length) { throw new Exception("invalid input"); } Integer[] result=new Integer[k]; System.arraycopy(a, 0, result, 0, k); if (a.length<k) { return result; }else { for (int i = k; i < a.length; i++) { Integer max=returnMax(result); if (max>a[i]) { result[0]=a[i]; } } } return result; } public static void main(String[] args) throws Exception { Integer[] a={4,6,1,2,8,3,0,9,7,5}; Test30 t=new Test30(); Integer[] result=t.getLeastNumbers(4, a); for (int i = 0; i < result.length; i++) { System.out.println(result[i]); } }}
0 0
- 剑指offer面试题 java解答26-30
- 剑指offer面试题 java解答41-45
- 剑指offer面试题 java解答46-50
- 剑指offer面试题 java解答36-40
- 剑指offer面试题 java解答31-35
- 剑指offer面试题 java解答51-55
- 剑指offer面试题 java解答56-60
- 剑指offer面试题 java解答21-25
- 剑指offer面试题 java解答16-20
- 剑指offer面试题 java解答11-15
- 剑指offer面试题 java解答6-10
- 剑指offer面试题 java解答1-5
- 剑指offer面试题 java解答61-65
- 剑指offer面试题 java解答66-67
- 剑指offer面试题26
- 剑指offer面试题30
- JAVA面试题及解答
- JAVA面试题及解答
- Flex布局学习笔记
- Hadoop HA配置下ResourceManager启动不了(电脑之前宕机了)
- Tank Game bug
- 指示器
- flume 1.6.0 安装及配置
- 剑指offer面试题 java解答26-30
- sublime text 安装配置
- 一篇介绍MSE, EME, hls, dash, html5的文章
- TOJ 2404.Bin Packing
- C语言基本教程 第15课: 进阶方向概谈
- 存储技术
- Linux Socket 编程实例(一个Echo程序)
- ionic入门教程第九课-开始动手做项目吧!ion-nav-bar、ion-nav-buttons、ion-tabs
- Radon变换理论介绍与matlab实现--经验交流