In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article "java how to find the maximum number" article, the sample code introduced in the article is very detailed, has a certain reference value, interested friends must refer to, for "java how to find the maximum number", Xiaobian sorted out the following knowledge points, please follow the pace of Xiaobian step by step slowly understand, then let us enter the theme.
Given a set of nonnegative integers, rearrange them to form the largest integer.
Example 1:
Input: Output:
Example 2:
Input: Output:
Answer:
1public String largestNumber(int[] num) {
2 if (num == null || num.length == 0)
3 return "";
4 //Convert int to string and sort
5 String[] s_num = new String[num.length];
6 for (int i = 0; i
< num.length; i++) 7 s_num[i] = String.valueOf(num[i]); 8 // 排序,然后决定哪个字符串在前面 9 Comparator comp = new Comparator() { 10 @Override 11 public int compare(String str1, String str2) { 12 String s1 = str1 + str2; 13 String s2 = str2 + str1; 14 return s2.compareTo(s1); 15 } 16 }; 17 Arrays.sort(s_num, comp); 18 if (s_num[0].charAt(0) == '0') 19 return "0"; 20 StringBuilder sb = new StringBuilder(); 21 for (String s : s_num) 22 sb.append(s); 23 return sb.toString(); 24} 解析: 先排序,这里的排序并不是直接比较,而是str1和str2先组合然后再比较哪种组合比较大,为什么不能直接比较str和str2呢,举个例子,比如str1是3,str2是30,如果str1和str2比较,那比较之后组合的结果是303,因为30>3, but in fact the maximum value of 3 and 30 is 330, which obviously cannot be compared like this.
Java collections are mainly divided into four categories: 1, List list: ordered, repeatable;2, Queue queue: ordered, repeatable;3, Set collection: non-repeatable;4, Map mapping: disordered, unique keys, values are not unique.
The above is "java how to find the maximum number" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.