Largest Number

Given a list of non negative integers, arrange them such that they form the largest number.

For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.

Note: The result may be very large, so you need to return a string instead of an integer.

看到这道题的第一感觉就是需要进行排序,但是想了想,却没有什么合适的排序算法,有种感觉就是用跟基数排序类似的算法,或者自己直接改动基数排序

结果由于数据位数的不确定,没有实施成功,然后就百度了下

import java.util.*;
public  class LargestNumber{
        public static void main(String[] args){
                int[] num = {3232543,0,0,34350,34,12312329,86,5};
                System.out.println("结果:" + getLargestNumber(num));
        }
        private static String getLargestNumber(int[] num){
                if(num.length < 1 ){
                        return "";
                }
                String[] strs = new String[num.length];
                for(int i = 0; i < num.length ; i ++){
                        strs[i] = String.valueOf(num[i]);
                }
                //按照数字在结果中的位置排序
                Arrays.sort(strs,new Cmp());
                for(int i = 0 ; i < num.length  ; i ++){
                        System.out.print(strs[i] + ",");
                }
                System.out.println();
                //把所有的字符串拼接在一起
                String res = "";
                for(int i = num.length - 1; i >= 0 ; i--){
                        res = res.concat(strs[i]);
                }
                //去掉数字开头的0,如输入[0,0]
                int i = 0;
                while(i < num.length && res.charAt(i) == 0){
                        i++;
                }
                if(i == num.length)
                        return "0";
                return res.substring(i);
        }
}
class Cmp implements Comparator<String>{
        @Override
        public int compare(String a,String b){
                String ab = a.concat(b);
                String ba = b.concat(a);
                return ab.compareTo(ba);
        }
}

这个程序里最重要的是排序所用的比较器,这个比较器是自己定义的,

扩展:

Arrays.sort(T[], Comparator < ? super T > c) 是用来对用户自定义的对象数组排序功能的。Java 官方文档简单描述了它的作用,但不足以让我们深刻理解。为了更深入地理解它,这篇文章将梳理相关的关键点。

1、简单实例:如何使用Arrays.sort()

通过阅读下面代码,你能快速正确了解这个方法的用途。Comparator(比较器)用于根据Dogs的size比较其大小,并作为sort方法的参数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.Arrays;
import java.util.Comparator;
 
class Dog{
     int size;  
     public Dog( int s){
         size = s;
     }
}
 
class DogSizeComparator implements Comparator<Dog>{
 
     @Override
     public int compare(Dog o1, Dog o2) {
         return o1.size - o2.size;
     }
}
 
public class ArraySort {
 
     public static void main(String[] args) {
         Dog d1 = new Dog( 2 );
         Dog d2 = new Dog( 1 );
         Dog d3 = new Dog( 3 );
 
         Dog[] dogArray = {d1, d2, d3};
         printDogs(dogArray);
 
         Arrays.sort(dogArray, new DogSizeComparator());
         printDogs(dogArray);
     }
 
     public static void printDogs(Dog[] dogs){
         for (Dog d: dogs)
             System.out.print(d.size + " " );
 
         System.out.println();
     }
}
输出:
1
2
2 1 3
1 2 3

2、策略模式的使用

这是运用策略模式的一个很好的场景,为什么策略模式对于这种场景非常适用?简单来说,策略模式使不同的算法在运行时得以选择。在这个例子中,通过传递不同的Comparator,可以选择不同的算法。基于上例,现在假设你有一个Comparator,用weight来代替size来比较Dogs。你可以简单创建一个新的Comprator如下:

class Dog{
     int size;
     int weight;
 
     public Dog( int s, int w){
         size = s;
         weight = w;
     }
}
 
class DogSizeComparator implements Comparator<Dog>{
 
     @Override
     public int compare(Dog o1, Dog o2) {
         return o1.size - o2.size;
     }
}
 
class DogWeightComparator implements Comparator<Dog>{
 
     @Override
     public int compare(Dog o1, Dog o2) {
         return o1.weight - o2.weight;
     }
}
 
public class ArraySort {
 
     public static void main(String[] args) {
         Dog d1 = new Dog( 2 , 50 );
         Dog d2 = new Dog( 1 , 30 );
         Dog d3 = new Dog( 3 , 40 );
 
         Dog[] dogArray = {d1, d2, d3};
         printDogs(dogArray);
 
         Arrays.sort(dogArray, new DogSizeComparator());
         printDogs(dogArray);
 
         Arrays.sort(dogArray, new DogWeightComparator());  
         printDogs(dogArray);
     }
 
     public static void printDogs(Dog[] dogs){
         for (Dog d: dogs)
             System.out.print( "size=" +d.size + " weight=" + d.weight + " " );
 
         System.out.println();
     }
}

输出:

1
2
3
size= 2 weight= 50 size= 1 weight= 30 size= 3 weight= 40
size= 1 weight= 30 size= 2 weight= 50 size= 3 weight= 40
size= 1 weight= 30 size= 3 weight= 40 size= 2 weight= 50

Comparator仅仅是一个接口,任何实现了Comparator在运行时都可以被使用,这是策略模式的核心理念。

3、为什么使用“super”

很显然,如果”Comparator<T>c”作为参数,但是第二个参数是”Comparator< ? super T > c”,使用<? super T>意味着类型可以是T或者是它的超类。为什么允许超类型呢?答案是:这种方式允许所有子类使用同一个comparator。看看下面这个例子一目了然。

import java.util.Arrays;
import java.util.Comparator;
 
class Animal{
     int size;
}
 
class Dog extends Animal{
     public Dog( int s){
         size = s;
     }
}
 
class Cat extends Animal{
     public Cat( int s){
         size  = s;
     }
}
 
class AnimalSizeComparator implements Comparator<Animal>{
 
     @Override
     public int compare(Animal o1, Animal o2) {
         return o1.size - o2.size;
     }
     //in this way, all sub classes of Animal can use this comparator.
}
 
public class ArraySort {
 
     public static void main(String[] args) {
         Dog d1 = new Dog( 2 );
         Dog d2 = new Dog( 1 );
         Dog d3 = new Dog( 3 );
 
         Dog[] dogArray = {d1, d2, d3};
         printDogs(dogArray);
 
         Arrays.sort(dogArray, new AnimalSizeComparator()); 
         printDogs(dogArray);
 
         System.out.println();
 
         //when you have an array of Cat, same Comparator can be used.
         Cat c1 = new Cat( 2 );
         Cat c2 = new Cat( 1 );
         Cat c3 = new Cat( 3 );
 
         Cat[] catArray = {c1, c2, c3};
         printDogs(catArray);
 
         Arrays.sort(catArray, new AnimalSizeComparator()); 
         printDogs(catArray);
     }
 
     public static void printDogs(Animal[] animals){
         for (Animal a: animals)
         System.out.print( "size=" +a.size + " " );
         System.out.println();
     }
}
输出:
1
2
3
4
5
size= 2 size= 1 size= 3
size= 1 size= 2 size= 3
 
size= 2 size= 1 size= 3
size= 1 size= 2 size= 3

4、总结

总的来说,从Arrays.sort()中你应该了解到:

  1. generic(范型)——super
  2. 策略模式
  3. 归并排序——nlog(n)时间复杂度
  4.  java.util.Collections.sort(List<T>list, Comparator<?super T> c)类似于Arrays.sort

参考:Arrays.sort(T[], java.util.Comparator)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码有问题,修改一下,MOV r0, #0x00002000 ; Initialize pointer to first number MOV r1, #9 ; Initialize counter with number of elements LDR r7, [r0] ; Load first number as largest LDR r8, [r0] ; Load first number as smallest Loop: ADD r0, r0, #4 ; Move pointer to next number LDR r2, [r0] ; Load the number in r2 CMP r7, r2 ; Compare largest with current number MOVLT r7, r2 ; If current number is smaller, update largest CMP r8, r2 ; Compare smallest with current number MOVGT r8, r2 ; If current number is larger, update smallest SUBS r1, r1, #1 ; Decrement counter BNE Loop ; Loop until all numbers are compared ; Display largest number on console MOV r0, #1 ; File descriptor for stdout LDR r1, =largest ; Address of string to be displayed MOV r2, #10 ; Length of string MOV r7, #4 ; Syscall number for write SWI 0 ; Call operating system ; Display largest number on LCD screen LDR r0, =0x40020C14 ; Address of LCD data register MOV r1, r7 ; Load largest number from r7 STR r1, [r0] ; Store the number in the LCD data register ; Display smallest number on console MOV r0, #1 ; File descriptor for stdout LDR r1, =smallest ; Address of string to be displayed MOV r2, #12 ; Length of string MOV r7, #4 ; Syscall number for write SWI 0 ; Call operating system ; Display smallest number on LCD screen LDR r0, =0x40020C14 ; Address of LCD data register MOV r1, r8 ; Load smallest number from r8 STR r1, [r0] ; Store the number in the LCD data register largest: .asciz "Largest number: %d\n" smallest: .asciz "Smallest number: %d\n"
最新发布
05-27

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值