剑指编程(6)

一、
输入一个复杂链表
(每个节点中有节点值,以及两个指针,
一个指向下一个节点,另一个特殊指针指向任意一个节点)
返回结果为复制后复杂链表的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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值