Largest Number

25 篇文章 0 订阅

Largest Number

leetcode sort


原题链接 
参考 - Cindy_niu


题意

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..

思路:

1 对于两个备选数字a和b, 如果str(a) + str(b) > str(b) + str(b), 则a在b之前,否则b在a之前,按照这个原则对原数组从大到小排序即可。我们只需要写出sort函数的自定义比较函数即可,注意的是,这里比较函数必须为静态函数。还有我们,要注意一个特殊情况,如果字符串后的字符串从0开始,我们返回“0”。

2 首先,我们将num转成字符串存入数组,然后根据自定义比较函数排序,之后再对排序后的字符串连接成一个字符串,最后排除特殊情况,得到所求结果。

注意:

1 注意特殊情况,如果字符串开头含0,最后返回“0”

2 sort中的比较函数compare要声明为静态成员函数或全局函数,不能作为普通成员函数,否则会报错. Line 26: invalid use of non-static member function

因为:非静态成员函数是依赖于具体对象的,而std::sort这类函数是全局的,因此无法再sort中调用非静态成员函数。静态成员函数或者全局函数是不依赖于具体对象的, 可以独立访问,无须创建任何对象实例就可以访问。同时静态成员函数不可以调用类的非静态成员。

code

 
 
  1. class Solution {
  2. public:
  3. string largestNumber(vector<int> &num) {
  4. vector<string>res;
  5. for(int i = 0; i < num.size(); i++) {
  6. //if(num[i] == 0) {
  7. // res.push_back("0");
  8. // continue;
  9. //}
  10. //string r;
  11. //IntToString(num[i], r);
  12. //res.push_back(r);
  13. res.push_back(to_string(num[i]));
  14. }
  15. sort(res.begin(), res.end(), cmp);
  16. string s;
  17. for(int i = 0; i < res.size(); i++) {
  18. s += res[i];
  19. }
  20. // special case, since arranged by descending order,
  21. // just judge the first element of res is "0",
  22. if(res[0] == "0") return "0";
  23. return s;
  24. }
  25. void IntToString(int num, string& res) {
  26. if(num == 0) return;
  27. IntToString(num / 10, res);
  28. res += (num % 10) + '0';
  29. }
  30. private:
  31. static bool cmp(const string& a, const string& b) {
  32. return a + b > b + a;
  33. }
  34. };
这段代码有问题,修改一下,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
The code you provided looks correct to me. However, I would make a minor change to the string format specifier used in the printf statements. Since we are printing integers, we should use "%d" instead of "%s". Here is the modified code: ``` 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, #16 ; 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, #18 ; 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" ``` This code will display the largest and smallest numbers on both the console and LCD screen. The string format specifier has been changed to "%d" in the printf statements.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值