【leetcode】solution in java——Easy3

转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6412505.html

心得:看到一道题,优先往栈,队列,map,list这些工具的使用上面想。不要来去都是暴搜,数组遍历。

11:Single Number

Given an array of integers, every element appears twice except for one. Find that single one.

此题:给出一个数组,每个元素出现两次除了一个只出现一次,找到出现一次那个。

思路:这种有很明显映射关系的题:数—出现次数,第一时间就想到用map。

 Map<Integer, Integer> map= new HashMap<Integer, Integer>();
        int res = -1;
        for(int i:nums){
            if(map.get(i)==null){
                map.put(i, 1);
            }else{
                map.put(i, 2);
            }
        }
        Set<Integer> keyset=map.keySet();
        Iterator<Integer> iterator=keyset.iterator();
        while(iterator.hasNext()){
            int curr=iterator.next();
            if(map.get(curr)==1){
                res=curr;
                break;
            }
        }
        return res;

 12:Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

此题:求二叉树的深度。

思路:与树有关的,第一时间想到队列、BFS、DFS这些关键字。

法一:BFS,利用队列(Java中LinkedList实现了Queue接口,用法同queue),一层层处理,处理一层,depth++;

 public int maxDepth(TreeNode root) {
            int depth=0;
            LinkedList<TreeNode> linkedList=new LinkedList<TreeNode>();
            linkedList.add(root);
            int current_count=1;
//while循环主要用来执行出队入队操作
while(!linkedList.isEmpty()){ TreeNode temp=linkedList.poll(); current_count--; if(temp.left!=null){ linkedList.add(temp.left); } if (temp.right!=null) { linkedList.add(temp.right); }
//current_count用来记录每一层的结点数目。然后执行current_count次循环处理完当前层结点,depth++。
if(current_count==0){ depth++; current_count=linkedList.size(); } } return depth; }

法二:二叉树的深度,最快的做法是DFS。实现DFS就一个思路:递归,返回值。

            //定义边界
            if(root==null){
                return 0;
            }else{
               //递归
                int m=maxDepth(root.left);
                int n=maxDepth(root.right);
                //确定递归的返回值
                return m>n?m+1:n+1;
            }

 13:Sum of Two Integers

alculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

此题:求两整数和,但不能用运算符+/-。

思路:位运算的应用。位运算实现四则运算请阅读我的博文:http://www.cnblogs.com/ygj0930/p/6412875.html

 public int getSum(int a, int b) {
        int res=a;
        int xor=a^b;
        int forward=(a&b)<<1;
        if(forward!=0){
            res=getSum(xor, forward);
        }else{
            res=xor;
        }
        return res;    
    }

14:Find the Difference

Given two strings s and t which consist of only lowercase letters.

String t is generated by random shuffling string s and then add one more letter at a random position.

Find the letter that was added in t.

此题:给出两个字符串,t是s的乱序并且t中多了一个字符。求t中多出的那个字符。

思路:此题容易忽略一个地方——多出的字符可能是s中已经存在的字符。如果多出的字符是s中没有的,比如 s=abc t=abcd ,那么用map很快就能做出来。把s各个字符存入map,然后遍历t各个字符去map.get(ch),get不出来value的就是多出来的。但是如果 s=a,t=aa,那么就求不出来了。所以map不行。转换思路,既然t中包含s的所有字符,那么我们就可以用排除法,从t中逐一剔除s中的元素,直到t留下的最后一个即是多出来的。并且,存放t的容器必须允许重复值的出现而且方便查找删除——很自然地,我们想到list。

public char findTheDifference(String s, String t) {
            char res = 0;
            char[] ss=s.toCharArray();
            char[] ts=t.toCharArray();
            List<Character> list=new ArrayList<Character>();           
            //把t存入list
            for(Character ch:ts){
                list.add(ch);
            } 
            //从t中逐一删除s的元素
            for(Character ch:ss){
                list.remove(ch);
            }
            //留下的最后一个就是多出来的
            return list.get(0);            
        }

15:Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

For example:

Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.

Follow up:
Could you do it without any loop/recursion in O(1) runtime?

此题:求一个整数各位数字相加,直到成为一个数字。

思路:如果按照题目所述去模拟,加一位移除一位的话,无疑是很慢的。而且此题有限制:不能用循环不能用递归,时间复杂度控制在O(1)。由O(1)我们可以看出,题目要求我们一步到位,也就是说,找出一条公式可以直接求。那么找公式,无疑就是找规律了。

观察一个数X,可以发现 X=Xn*10^n+Xn-1*10^(n-1)......+X0*10^0,我们把各位权10^n拆分成99...99+1的形式,就可以得到X=Xn*(99..9+1)+Xn-1*(9..9+1)+....的形式,化开括号就有了  X=(Xn+Xn-1+Xn-2+...+X0)+(Xn*99...9+Xn-1*9.99+...)的形式,很明显可以看出,后面的括号内是9的倍数,可以被9整除。而前面的括号内的和(求各位上数字的和)可以令为X’,继续套用上面拆分方法处理X’,把X’拆分为 各位数值之和+9的某倍数  的形式......直到最终  X=Xs+9*Xo 的形式,Xs是一个个位数,9*Xo为拆分以来众多9的倍数相加得到的9的总倍数。由此可见,对任一个非负整数我们都可以用 一个个位数+9的倍数  形式表示。并且这条公式的推断过程,就是不断求各位上数值的和最终剩下一位的过程,由此可得,求一个数各位数字相加直到成为一个数字,就是求这条公式中的那  个位数。所以,解此题,直接套用公式即可。

public int addDigits(int num) {
        //由 num=mod+9*X可以看出,求mod就是用num对9取余即可
        int mod=num%9;
        //若mod为0,说明num是9的倍数,分两种情况处理:如果num是0,则返回0.如果num是非0的9的倍数,那么num=9*M=9+9*(M-1),所以返回9.
        if(mod==0){
            if(num==0){
                return 0;
            }else{
                return 9;
            }
        }
        return mod;
    }

 

转载于:https://www.cnblogs.com/ygj0930/p/6412505.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值