剑指秋招——牛客网《剑指offer》部分题目总结(java)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/hnu_zzt/article/details/95325273

牛客网大佬很详细的总结:https://www.nowcoder.com/discuss/198840

  • 从尾到头打印链表:题目链接
    题目描述: 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
    解题思路: 使用头插法可以得到一个逆序的链表。
    在这里插入图片描述
public ArrayList<Integer> printListFromTailToHead(ListNode head) {
    //头插法构建逆序链表
    ListNode prev=null;    //需要一个指针保存head的翻转的值
    while(head!=null){
        ListNode next=head.next;    //需要一个指针保存next指针
        head.next=prev;   //翻转(头元素指向prev)
        prev=head;   //挪动prev指针
        head=next;   //挪动head指针
    }
    // 构建 ArrayList
    ArrayList<Integer> ret = new ArrayList<>();
    while (prev != null) {
        ret.add(prev.val);
        prev = prev.next;
    }
    return ret;
}
  • 重建二叉树题目链接
    在这里插入图片描述
    解题思路: 利用递归思想,每次都将左右两边的子树当作一根新的二叉树处理:
    1、找出二叉树的根节点
    2、 构建左子树----->找出该左子树的根节点,构建该左字树的左子树,构建该左子树的右子树…直到序列中没有左子树的序列
    3、构建右子树----->找出该右子树的根节点,构建该右字树的左子树,构建该右子树的右子树…直到序列中没有右子树的序列
public class Solution {
 
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
 
        TreeNode(int x) {
            val = x;
        }
    }
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        TreeNode root=reConstructBinaryTree(pre,0,pre.length-1,in,0,in.length-1);
        return root;
    }
    /**
     *
     * @param pre 前序遍历序列的数组
     * @param startPre 前序遍历序列中 新子树开始的序号的下标
     * @param endPre   前序遍历序列中 新子树结束的序号的下标
     * @param in 中序遍历序列的数组
     * @param startIn  中序遍历序列中 新子树开始的序号的下标
     * @param endIn    中序遍历序列中 新子树结束的序号的下标
     * @return
     */
    private TreeNode reConstructBinaryTree(int [] pre,int startPre,int endPre,int [] in,int startIn,int endIn) {
        /**
         * 作为退出递归的条件
         * 当该子树在序列中的开始的下标大于结束的下标时,说明已经没有子树,将该子树置为null
         */
        if(startPre>endPre||startIn>endIn)
            return null;
        /**
         * 新子树的根节点(一定是当前前序遍历序列中的pre[startPre])
         */
        TreeNode root=new TreeNode(pre[startPre]);
 
 
        for(int i=startIn;i<=endIn;i++)
            /**
            根据前序遍历的序列和中序遍历的序列,找出新子树的根节点,并递归计算新子树下的左子树和右子数
             i即是中序遍历序列中本树的根节点的位置
             */
            if(in[i]==pre[startPre]){
                /**
                 *startPre = startPre + 1 :startPre是前序遍历序列中本树的根节点,所以下次递归要从本树的子树序列开始
                 * endPre = startPre + i -startIn :下次递归本树的子树序列结束的位置,在中序遍历序列中i的左边的所有序列都是本树的左子树,所以i也是本树的左子树的个数
                 */
                root.left=reConstructBinaryTree(pre,startPre+1,i-startIn+startPre,in,startIn,i-1);
                /**
                 * 里面的加加减减好难解释啊...有种只可意会不可言传的感觉。。。
                 * 相信你们将程序单步调试几遍后就会明白
                 */
                root.right=reConstructBinaryTree(pre,i-startIn+startPre+1,endPre,in,i+1,endIn);
                break;
            }
        return root;
    }
}

整理:
用JAVA实现找出输入字符串中的出现次数最多的字符及其次数:(Map、Set的使用)

//通过Map 类实现,通过键值对的方式,可以将输入的字符串的每一个字符,作为键,每个字符出现的次数作为值:如下:

public class Find {
  public static void main(String[] args){
    String scan=new Scanner(System.in).nextLine();//获取键盘上输入的字符串;
    Map<Character,Integer> map = new HashMap<Character,Integer>();//新建一个HashMap对象;    
       //通过FOR循环,把String的键值存放到map
    for(int i=0;i<scan.length();i++){
      char temp=scan.charAt(i);//通过循环,找到字符串的每一位字符并存入到temp中;
      if(map.containsKey(temp)){//如果map里面有temp这个字符
        map.put(temp, map.get(temp)+1);//把temp的值加1;
      }else{//如果map里面没有temp这个字符,
        map.put(temp, 1);//把temp的值设为1;
      }
    }
/*Collection c = map.entrySet();
Iterator it = c.iterator();
Map.Entry<Character, Integer> entry;
while(it.hasNext()){
entry = (Map.Entry<Character, Integer>) it.next();
}
*/
    int maxnum = Collections.max(map.values());//调用Collections类的max方法,获取map的值的集合;并找出最大的那个值;
    Set<Character> set = new HashSet<Character>();//建立一个set对象
    for(Map.Entry<Character, Integer> entry1:map.entrySet()){ //通过集合的循环,把map的值放到entry1里,通过entry1找到值最大的maxnum的key;
        if(entry1.getValue()==maxnum){
           set.add(entry1.getKey());
        }
    }
System.out.println("出现次数最多的字母为:"+set+" 最多出现次数为"+maxnum);

 }

}

猜你喜欢

转载自blog.csdn.net/hnu_zzt/article/details/95325273