CareerCup Write a function that given a position returns the digit in that 0123456789101112131415..
来源:互联网 发布:华为云超越阿里云 编辑:程序博客网 时间:2024/05/17 23:25
Write a function that given a position returns the digit in that position.
(You could think that this sequence is an array where each cell only holds one digit so given an index return what digit is in that index, however you cannot really create an array since the sequence is infinite, you need a way to based on the index calculate the digit that goes there).
The function has to return a single digit.
Other examples:
index = 100, result = 5
index = 30, result = 2
index = 31, result = 0
index = 1000, result = 3
------------------------------------------------------------------------------------------------------------------------------
Analysis: Let's take a specific position as an example to analyze this problem. For example, what is the digit at the position 1001?
The first 10 digits are for 10 numbers with only one digit (0, 1, 2, ..., 9). Since the position 1001 is beyond of the range of the first 10 digits, we continue to look for the digit at the position at 991 (991 = 1001 - 10) in the following sequence.
The next 180 digits are for 90 numbers with two digits, from 10 to 99. Since 991 is greater than 180, the digit at position 991 is beyond the numbers with two digits. Let's continue to get the 811 (811 = 991-180) in the following sequence.
The next 2700 digits are for 900 numbers with three digits, from 100 to 999. Since 811 is less than 2700, the position 811 should be inside a number with three digits.
Every number has three digits, so the position 811 should be the second digit of the 270-th nubmer starting from 100 (811 = 270 * 3 + 1). Therefore, the digit at the position 811 is the second digit in the number 370, which is digit 7.
The overall solution can be implemented with the following code:
if(index < 0)
return -1;
{
int numbers = countOfIntegers(digits);
if(index < numbers * digits)
return digitAtIndex(index, digits);
}
return -1;
}
We can get the count of integers with n digits via the following function:
if(digits == 1)
return 10;
count *= 10;
return 9 * count;
}
After we know the digit inside an integer with m digits, we could get the digit with the following function:
int number = beginNumber(digits) + index / digits;
int indexFromRight = digits - index % digits;
for(int i = 1; i < indexFromRight; ++i)
number /= 10;
return number % 10;
}
In the function above, we need to know the first number with m digits. The first number with two digits is 10, and the first number with three digits is 100. These numbers can be calculated with the function below:
if(digits == 1)
return 0;
begin *= 10;
return begin;
}
More coding interview questions are discussed in my book <Coding Interviews: Questions, Analysis & Solutions>. You may find the details of this book on Amazon.com, or Apress.
The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a reference to http://codercareer.blogspot.com/. If you are going to use it in your books, please contact him via zhedahht@gmail.com . Thanks.
- CareerCup Write a function that given a position returns the digit in that 0123456789101112131415..
- Given a function RAND5 that generates random numbers in the range 0 to 5 write a function RAND7?
- Write a function that takes a string as input and returns the string reversed.
- The "data" option should be a function that returns a per-instance value in component definitions.
- Write a method that counts the number of occurrences of a given character in a string
- Write a method that returns all subsets of a set.
- Write a method that returns all subsets of a set
- 代码面试题:Find the first element in a given string A that also appears in another given string B
- Q9.4 Write a method that returns all subsets of a set.
- Write a function that computes log2() using sqrt()
- CareerCup Given an array A[], find (i, j) such that A[i] < A[j] and (j - i) is maximum.
- The function that returns the pointer (C Programming)
- Algorithm_Write a method that returns all subsets of a set.
- CareerCup Given an array of (unsorted) integers, arrange them such that a < b > c < d > e... etc.
- A function returns function
- CareerCup Find the no. of expressions that evaluate to a Walprime
- CareerCup Implement a stack that pops out the most frequently added item
- CareerCup Find the ceiling value present in the BST of a given ke
- JDK安装与环境变量配置(总结篇)卸载
- ROM型启动方式概述
- 开源搜索引擎和它的小伙伴们--初探
- openggsn自述文件
- 调用webservice
- CareerCup Write a function that given a position returns the digit in that 0123456789101112131415..
- VxWorks 基本启动方式
- IReport的使用
- a标签去下划线和点击前和点击后不变色行不行啊?
- 微信公众帐号开发教程第4篇-消息及消息处理工具的封装(Java版)
- 服务的生命周期,被杀死后后可自行恢复
- 判断NSString是否为数字,以及转换为数字
- T-SQL查询处理执行顺序(一)
- 电脑内存常见问题处理方法