题解 | #最大数#
最大数
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; } }