数字序列中某一位的数字
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020030522372067.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NmMTE2OTk4MzI0MA==,size_16,color_FFFFFF,t_70
思路:网上的很多答案说得不清不楚的,其实只要多找几个数就能发现规律:
先判断n是落在哪一位区间,然后其实是由那个数字产生的,再从该数字上找出具体那一位
class Solution {
public int findNthDigit(int n)
{
if(n<10) //位于1位数区间的,可以直接返回
return n;
int i=1;
//开始寻找n属于那个区间
while(true)
{
if(n>(Math.pow(10, i)-Math.pow(10, i-1)*i))
{
if(i==1)
n=n-10;
else
n=n-(int)(Math.pow(10, i)-Math.pow(10,i-1))*i;
i++;
}
else
{
break;
}
}
int shang=n/i;
int yushu=n%i;
int N=(int) Math.pow(10,i-1)+shang;
//从左往右数,取N的第yuhsu位
while(i-yushu>1)
{
N=N/10;
i=i-1;
}
return N%10;
}
public static void main(String[] args)
{
Solution ss=new Solution();
System.out.print(ss.findNthDigit(19));
}
}
把数组排成最小的数
根据题目的要求,两个数字m和n能拼接称数字mn和nm。如果mn<nm,也就是m应该拍在n的前面,我们定义此时m小于n;反之,如果nm<mn,我们定义n小于m。如果mn=nm,m等于n。
public class Solution {
public String PrintMinNumber(int [] numbers) {
int n;
StringBuilder s = new StringBuilder();
ArrayList<Integer> list = new ArrayList<>();
n = numbers.length;
for (int i = 0; i < n; i++) {
list.add(numbers[i]);
}
list.sort((str1, str2) -> {
String s1 = str1 + "" + str2;
String s2 = str2 + "" + str1;
return s1.compareTo(s2);
});
list.forEach(s::append);
return s.toString();
}
}