数组排序的Java编程
我昨天参加了一次面试,我没有找到下面给定问题的解决方案。 如何使用起始3位数字和剩余2、1、4、5等数字对整数数组进行排序 例:输入为数组排序的Java编程,java,arrays,Java,Arrays,我昨天参加了一次面试,我没有找到下面给定问题的解决方案。 如何使用起始3位数字和剩余2、1、4、5等数字对整数数组进行排序 例:输入为 i={1,34,323,456,5432,34566,33,45,654} 产出是, i={323,456,654,1,34,33,45,5432} 这看起来很傻,但你可以这样做: 将每个长度不为3位的元素乘以1000进行多倍运算。 对数组进行排序。 用1000除以所有长度不超过3位数的值。由于需要按长度排序,请首先将int[]转换为String[]。然后,
i={1,34,323,456,5432,34566,33,45,654}
产出是,
i={323,456,654,1,34,33,45,5432}
这看起来很傻,但你可以这样做: 将每个长度不为3位的元素乘以1000进行多倍运算。 对数组进行排序。
用1000除以所有长度不超过3位数的值。由于需要按长度排序,请首先将
int[]
转换为String[]
。然后,您可以使用比较器对字符串[]
进行排序
对于排序,首先按长度排序,然后按值排序。由于您的主要排序是按长度进行的,因此对于次要的按值排序,可以进行简单的字符串比较
至于奇怪的排序顺序3、2、1、4、5、6、7、8、9,基本上是翻转值1和3。一个简单的方法是当1我假设人们可能希望改变数字的顺序,并允许以任何顺序指定它们时,说4-x
。所以我有一个“排序”数组,它对应于应该处理的位数。我还假设应该在最终数组中按递增顺序对值进行排序。我还认为排序是实验的一部分,所以我没有使用array.sort
以下是我对该方法的看法:
public static void main(String[] args)
{
int[] arr = {1,34,323,456,5432,34566,33,45,654};
int[] sort = {3, 2, 1, 4, 5 };
int lastPos = 0;
int loopPos = 0;
int[] output = new int[arr.length];
// process the whole array
for (int s = 0; s < sort.length; ++s) {
String fmt = String.format("^[0-9]{%d}$", sort[s]);
Pattern pat = Pattern.compile(fmt);
// set to end of the current location
loopPos = lastPos;
for (int i = 0; i < arr.length; ++i) {
int val = arr[i];
Matcher m = pat.matcher(Integer.toString(val));
if (m.matches()) {
// start at the beginning of the loopPos
// continue until lastPos
if (lastPos == loopPos) {
output[lastPos++] = val;
}
else {
for (int ins = loopPos; ins < lastPos; ++ins) {
int atPos = output[ins];
// if we are smaller, must insert before
if (val <= atPos) {
for (int rev = lastPos; rev > loopPos; --rev) {
output[rev] = output[rev - 1];
}
output[ins] = val;
++lastPos;
break;
}
else if (val > atPos) {
output[lastPos++] = val;
break;
}
}
}
}
} //i
} //s
System.out.println(Arrays.toString(output));
}
publicstaticvoidmain(字符串[]args)
{
int[]arr={1,3432345654323456,33,45654};
int[]sort={3,2,1,4,5};
int-lastPos=0;
int-loopPos=0;
int[]输出=新的int[arr.length];
//处理整个阵列
for(int s=0;satPos){
输出[lastPos++]=val;
打破
}
}
}
}
}//我
}//s
System.out.println(Arrays.toString(output));
}
样本结果:
[32345654334566]
当心数字溢出。我只想做最简单的事情。无论如何,你是对的,因为我的建议不适用于超过7位数的数字@安德烈亚斯
sort321(1,34,323,456,5432,34566,33,45,654);
public static void sort3first(int ... intArray) {
String[] strArray = new String[intArray.length];
for (int i = 0; i < intArray.length; i++)
strArray[i] = Integer.toString(intArray[i]);
Arrays.sort(strArray, (s1, s2) -> Integer.compare((s1.length() == 3 ? 0 : s1.length()),
(s2.length() == 3 ? 0 : s2.length())));
System.out.println(Arrays.toString(strArray));
}
public static void main(String[] args)
{
int[] arr = {1,34,323,456,5432,34566,33,45,654};
int[] sort = {3, 2, 1, 4, 5 };
int lastPos = 0;
int loopPos = 0;
int[] output = new int[arr.length];
// process the whole array
for (int s = 0; s < sort.length; ++s) {
String fmt = String.format("^[0-9]{%d}$", sort[s]);
Pattern pat = Pattern.compile(fmt);
// set to end of the current location
loopPos = lastPos;
for (int i = 0; i < arr.length; ++i) {
int val = arr[i];
Matcher m = pat.matcher(Integer.toString(val));
if (m.matches()) {
// start at the beginning of the loopPos
// continue until lastPos
if (lastPos == loopPos) {
output[lastPos++] = val;
}
else {
for (int ins = loopPos; ins < lastPos; ++ins) {
int atPos = output[ins];
// if we are smaller, must insert before
if (val <= atPos) {
for (int rev = lastPos; rev > loopPos; --rev) {
output[rev] = output[rev - 1];
}
output[ins] = val;
++lastPos;
break;
}
else if (val > atPos) {
output[lastPos++] = val;
break;
}
}
}
}
} //i
} //s
System.out.println(Arrays.toString(output));
}