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;
}
}