Hard 计算0到n之间2的个数 @CareerCup
2013-12-13 03:33
330 查看
一种是Brute force,O(nlogn)
另一种是找规律O(n),见http://hawstein.com/posts/20.4.html
package Hard;
/**
* Write a method to count the number of 2s between 0 and n.
译文:
写一个函数,计算0到n之间2的个数。
*
*/
public class S18_4 {
// O(n)
public static int count2s(int n){
int count = 0;
int factor = 1;
int low = 0, cur = 0, high = 0;
while(n/factor != 0){
low = n - (n/factor) * factor; // 低位
cur = (n/factor) % 10; // 当前位
high = n / (factor*10); // 高位
if(cur < 2){
count += high * factor;
}else if(cur > 2){
count += (high+1) * factor;
}else{
count += high*factor + low + 1;
}
factor *= 10;
}
return count;
}
//=============================================
public static int numberOf2s(int n) {
int count = 0;
while (n > 0) {
if (n % 10 == 2) {
count++;
}
n = n / 10;
}
return count;
}
// Brute force way O(nlogn)
public static int numberOf2sInRange(int n) {
int count = 0;
for (int i = 2; i <= n; i++) { // Might as well start at 2
count += numberOf2s(i);
}
return count;
}
public static void main(String[] args) {
for (int i = 0; i < 1000; i++) {
int b = numberOf2sInRange(i);
int v = numberOf2sInRange(i);
System.out.println("Between 0 and " + i + ": " + v + ", " + b);
}
}
}
另一种是找规律O(n),见http://hawstein.com/posts/20.4.html
当某一位的数字小于2时,那么该位出现2的次数为:更高位数字x当前位数
当某一位的数字大于2时,那么该位出现2的次数为:(更高位数字+1)x当前位数
当某一位的数字等于2时,那么该位出现2的次数为:更高位数字x当前位数+低位数字+1
package Hard;
/**
* Write a method to count the number of 2s between 0 and n.
译文:
写一个函数,计算0到n之间2的个数。
*
*/
public class S18_4 {
// O(n)
public static int count2s(int n){
int count = 0;
int factor = 1;
int low = 0, cur = 0, high = 0;
while(n/factor != 0){
low = n - (n/factor) * factor; // 低位
cur = (n/factor) % 10; // 当前位
high = n / (factor*10); // 高位
if(cur < 2){
count += high * factor;
}else if(cur > 2){
count += (high+1) * factor;
}else{
count += high*factor + low + 1;
}
factor *= 10;
}
return count;
}
//=============================================
public static int numberOf2s(int n) {
int count = 0;
while (n > 0) {
if (n % 10 == 2) {
count++;
}
n = n / 10;
}
return count;
}
// Brute force way O(nlogn)
public static int numberOf2sInRange(int n) {
int count = 0;
for (int i = 2; i <= n; i++) { // Might as well start at 2
count += numberOf2s(i);
}
return count;
}
public static void main(String[] args) {
for (int i = 0; i < 1000; i++) {
int b = numberOf2sInRange(i);
int v = numberOf2sInRange(i);
System.out.println("Between 0 and " + i + ": " + v + ", " + b);
}
}
}
相关文章推荐
- Hard 计算0到n之间2的个数 @CareerCup
- Hard 不用+号实现两个数之和 @CareerCup
- Hard 不用+号实现两个数之和 @CareerCup
- Hard 随机洗牌函数 @CareerCup
- Hard 随机选择subset @CareerCup
- Hard 大文本找两个单词最短距离 @CareerCup
- Hard 单词变型成另一个单词 @CareerCup
- Recursion 计算表达式的括号组合 @CareerCup
- Hard 找到由其它单词组成的最长单词 @CareerCup
- Hard 随机选择subset @CareerCup
- Hard 在字符串S中查找T中的字符串 @CareerCup
- Hard 随机洗牌函数 @CareerCup
- Hard 动态查找中位数 @CareerCup
- 计算同一天内,两个时间之间的分钟数
- 计算两个日期之间相差几天几小时几分钟
- Moderate 加入空格使得可辨别单词数量最多 @CareerCup
- 计算两个日期之间的所有日期
- 百度地图实现计算两点之间的距离
- Android学习之计算2个日期之间有多少年月日
- 百度地图API:计算任意两个城市之间的距离