题解 | #最大数#

最大数

https://www.nowcoder.com/practice/fc897457408f4bbe9d3f87588f497729

using System;
using System.Collections.Generic;


class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 最大数
     * @param nums int整型一维数组
     * @return string字符串
     */
    public string solve (List<int> nums) {
        // write code here
        if (nums == null || nums.Count == 0)
            return string.Empty;
        if (nums.Count == 1)
            nums[0].ToString();

        List<string> strNums = new List<string>() { };
        foreach (int val in nums)
            strNums.Add(val.ToString());
        strNums.Sort(StrMax);

        //List<string> strNums = new List<string>() { nums[0].ToString() };
        //for(int nIndex = 1; nIndex < nums.Count; nIndex++)
        //{
        //    //int nFindIndex = strNums.FindIndex(r => string.Compare(r, nums[nIndex].ToString(), true) > 0);
        //    int nFindIndex = strNums.FindIndex(StrMax);
        //    if (nFindIndex >= 0)
        //        strNums.Insert(nFindIndex, nums[nIndex].ToString());
        //    else
        //        strNums.Add(nums[nIndex].ToString());
        //}
        strNums.Reverse();
        return strNums[0].ToString().Equals("0") ? "0" : string.Join("", strNums);
    }

    public static int StrMax(string strA, string strB) {
        if (string.IsNullOrWhiteSpace(strA))
            return -1;
        if (string.IsNullOrWhiteSpace(strB))
            return 1;

        int nIndex = 0;
        for (; nIndex < strA.Length && nIndex < strB.Length; nIndex++) {
            if (strA[nIndex] > strB[nIndex])
                return 1;
            if (strA[nIndex] < strB[nIndex])
                return -1;
        }

        int nRtn = 0;
        if (strA.Length > strB.Length)
            nRtn = strA[nIndex] >= strA[nIndex - 1] ? 1 : strA[nIndex] == strA[nIndex - 1] ?
                   0 : -1;
        else if (strA.Length == strB.Length)
            nRtn = 0;
        else
            nRtn = strB[nIndex] >= strB[nIndex - 1] ? -1 : strB[nIndex] == strB[nIndex - 1]
                   ? 0 : 1;
        return nRtn;
    }
}

全部评论

相关推荐

美团 客服平台 薪资应该是后端算高的了,我们姑且称为nk了,给3w签字费
点赞 评论 收藏
转发
点赞 收藏 评论
分享
正在热议
# 牛客帮帮团来啦!有问必答 #
1151896次浏览 17149人参与
# 通信和硬件还有转码的必要吗 #
11208次浏览 101人参与
# OPPO开奖 #
19215次浏览 267人参与
# 和牛牛一起刷题打卡 #
19013次浏览 1635人参与
# 实习与准备秋招该如何平衡 #
203422次浏览 3627人参与
# 大厂无回复,继续等待还是奔赴小厂 #
4974次浏览 30人参与
# 不去互联网可以去金融科技 #
20450次浏览 256人参与
# 通信硬件薪资爆料 #
265952次浏览 2484人参与
# 国企是理工四大天坑的最好选择吗 #
2227次浏览 34人参与
# 互联网公司评价 #
97710次浏览 1280人参与
# 简历无回复,你会继续海投还是优化再投? #
25038次浏览 354人参与
# 0offer是寒冬太冷还是我太菜 #
454898次浏览 5124人参与
# 国企和大厂硬件兄弟怎么选? #
53910次浏览 1012人参与
# 参加过提前批的机械人,你们还参加秋招么 #
14646次浏览 349人参与
# 硬件人的简历怎么写 #
82289次浏览 852人参与
# 面试被问第一学历差时该怎么回答 #
19399次浏览 213人参与
# 你见过最离谱的招聘要求是什么? #
28177次浏览 248人参与
# 学历对求职的影响 #
161250次浏览 1804人参与
# 你收到了团子的OC了吗 #
538768次浏览 6387人参与
# 你已经投递多少份简历了 #
344266次浏览 4963人参与
# 实习生应该准时下班吗 #
96987次浏览 722人参与
# 听劝,我这个简历该怎么改? #
63525次浏览 622人参与
牛客网
牛客企业服务