剑指编程(6)

来源:互联网 发布:移动工作站推荐 知乎 编辑:程序博客网 时间:2024/05/17 23:28

一、
输入一个复杂链表
(每个节点中有节点值,以及两个指针,
一个指向下一个节点,另一个特殊指针指向任意一个节点)
返回结果为复制后复杂链表的head。
(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)

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;        }        RandomListNode p = new RandomListNode(pHead.label);        //由于复制只需要复制链表本身,随机节点指针引用原来的即可        p.random = pHead.random;        p.next = Clone(pHead.next);        return p;    }}

二、
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。
要求不能创建任何新的结点,只能调整树中结点指针的指向。

class TreeNode {    int val = 0;    TreeNode left = null;    TreeNode right = null;    public TreeNode(int val) {        this.val = val;    }}public class Solution {    //二叉搜索树的中序遍历的输出就是排序的    //只需要left指向前,right指向后    TreeNode head = null;    TreeNode pre = null;    public TreeNode Convert(TreeNode pRootOfTree) {        if(pRootOfTree == null) {            return null;        }        //递归的中序遍历        Convert(pRootOfTree.left);        if(head == null) {            head = pRootOfTree;            pre = pRootOfTree;        }else {            pre.right = pRootOfTree;            pRootOfTree.left = pre;            pre = pRootOfTree;        }        Convert(pRootOfTree.right);        return head;    }}

三、
输入一个字符串,按字典序打印出该字符串中字符的所有排列。
例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。

public class Solution {    //全排列问题可以用递归    public ArrayList<String> Permutation(String str) {       char[] chs = str.toCharArray();       ArrayList<String> res = new ArrayList<>();        if(str == null || str.length() == 0) {            return res;        }       //因为可能有字符重复,所以需要set去重       Set<String> set = new TreeSet<>();//treeSet保证结果集为字典序       recursion(chs, set, 0);       res.addAll(set);       return res;    }    public void recursion(char[] chs, Set<String> set, int pos) {        if(pos == chs.length) {            set.add(String.valueOf(chs));            return;        }        //策略就是把某个位置可能出现的字符的所有情况列出,并递归搜索下去        for(int i = pos; i < chs.length; i++) {            //i == pos时,表示该位置保持不变,同样合法            swap(chs, i, pos);            recursion(chs, set, pos + 1);            swap(chs, i, pos);        }    }    public void swap(char[] chs, int i, int j) {        char tmp = chs[i];        chs[i] = chs[j];        chs[j] = tmp;    }}

四、
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。
例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。
由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。
如果不存在则输出0。

public class Solution {    //以空间换时间,用HashMap存出现次数    public int MoreThanHalfNum_Solution(int [] array) {        Map<Integer, Integer> map = new HashMap<>();        for(int i = 0; i < array.length; i++) {            if(!map.containsKey(array[i])) {                map.put(array[i], 1);            }else {                map.put(array[i], map.get(array[i]) + 1);            }        }        for(int i : map.keySet()) {            if(map.get(i) > array.length / 2) {                return i;            }        }        return 0;    }}

五、
输入n个整数,找出其中最小的K个数。
例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。

public class Solution {    //有关数组前K个数的,用堆排序基本能解决且效率高    //最小的K个数,则应构建大顶堆    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {        ArrayList<Integer> res = new ArrayList<>();        if(input == null || input.length == 0 || k > input.length) {            return res;        }        for(int i = input.length / 2 - 1; i >= 0; i--) {            adjustHeap(input, i, input.length);        }        for(int i = input.length - 1; i > input.length - 1 - k; i--) {            res.add(input[0]);            int tmp = input[0];            input[0] = input[i];            input[i] = tmp;            adjustHeap(input, 0, i);        }        return res;    }    public void adjustHeap(int[] input, int pos, int len) {        int tmp = input[pos];        while(2 * pos + 1 < len) {            int child = 2 * pos + 1;            if(child + 1 < len && input[child + 1] < input[child]) {                child++;            }            if(input[child] < tmp) {                input[pos] = input[child];            }else {                break;            }            pos = child;        }        input[pos] = tmp;    }}
原创粉丝点击