1347 · 尾随零与487· 姓名去重

1347 · 尾随零
描述

给定一个整数n,返回n!(n的阶乘)的尾随零的个数。

您的解法时间复杂度应为对数级别。
样例

样例1

输入: n = 5
输出: 1
解释:
1*2*3*4*5=120
样例2

输入: n = 10
输出: 2
解释:
1*2*3*4*5*6*7*8*9*10=3628800

解题思路:题目要求找尾随的0的个数,2和5相乘能产生0,在计算阶乘的过程中,只要知道有多少个2 x 5就能得到结论,2的个数比5会多出很多,这样只要计算出5的个数就可以了。
解法:

public class Solution {
    /**
     * @param n: a integer
     * @return: return a integer
     */
    public int trailingZeroes(int n) {
        // write your code here
        int cnt = 0;
        while(n != 0){
            cnt += n/5;
            n/=5;
        }
        return cnt;
    }
}

题目:487· 姓名去重
描述

给一串名字,将他们去重之后返回。两个名字重复是说在忽略大小写的情况下是一样的。

你可以假设名字只包含大小写字母和空格。
样例

例1:

输入:["James", "james", "Bill Gates", "bill Gates", "Hello World", "HELLO WORLD", "Helloworld"]


输出:["james", "bill gates", "hello world", "helloworld"]

例2:

输入:["cmy","Cmy"]

输出:["cmy"]

该题主要记录list与set是可以相互转换的

public class Solution {
    /**
     * @param names: a string array
     * @return: a string array
     */
    public List<String> nameDeduplication(String[] names) {
        // write your code here
        Set<String> st = new HashSet<>();
        for(String s : names){
            st.add(s.toLowerCase());
        }
        List<String> list = new ArrayList<>(st);   //将set转换为list
        //list.addAll(st);
        return list;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值