题目来源
力扣670最大交换
题目概述
给定一个非负整数,你至多可以交换一次数字中的任意两位。返回你能得到的最大值。
解题思路
- 我们能确定 组成的最大数从最高位到最低位应该是降序排序的;
- 我们只需要知道每一位应该填入的数字,并且找到不符合的最高数位i;
- 交换i与最后一个最大值j即可。
代码实现
java实现
public class Solution { public int maximumSwap(int num) { // 统计每个数出现的次数,用于找最大值 int[] count = new int[10]; // 将入参转为数组 int[] numberToArray = new int[8]; // 记录入参长度 int length = 0; int n = num; while (n > 0) { int current = n % 10; numberToArray[length++] = current; count[current]++; n /= 10; } // 最高位在最后 int currentIndex = length - 1; // 最大值为9 int maxIndex = 9; while (maxIndex >= 0) { // 寻找当前最大值 while (maxIndex >= 0 && count[maxIndex] == 0){ maxIndex--; } // 如果当前最大值和当前最高位符合,继续找 if (currentIndex >= 0 && maxIndex == numberToArray[currentIndex]) { count[maxIndex]--; currentIndex--; }else { break; } } // 符合最大数要求,直接返回 if (currentIndex == 0) { return num; } // 交换找到的最高位和其后最大值 for (int i = 0; i < currentIndex; i++) { if (numberToArray[i] == maxIndex) { int temp = numberToArray[i]; numberToArray[i] = numberToArray[currentIndex]; numberToArray[currentIndex] = temp; } } // 组成结果返回 int result = 0; for (int i = length - 1; i >= 0; i--) { result *= 10; result += numberToArray[i]; } return result; } }
c++实现
class Solution { public: int maximumSwap(int num) { // 记录每个数出现的次数 int count[10] = {0}; // 入参转数组 int numberToArray[8] = {0}; // 记录入参长度 int length = 0; int n = num; while (n > 0) { int current = n % 10; numberToArray[length++] = current; count[current]++; n /= 10; } // 最高位在最后 int currentIndex = length - 1; // 最大值为9 int maxIndex = 9; while (maxIndex >= 0) { // 寻找当前最大值 while (maxIndex >= 0 && count[maxIndex] == 0){ maxIndex--; } // 如果当前最大值和当前最高位符合,继续找 if (currentIndex >= 0 && maxIndex == numberToArray[currentIndex]) { count[maxIndex]--; currentIndex--; }else { break; } } // 符合最大数要求,直接返回 if (currentIndex == 0) { return num; } // 交换找到的最高位和其后最大值 for (int i = 0; i < currentIndex; i++) { if (numberToArray[i] == maxIndex) { int temp = numberToArray[i]; numberToArray[i] = numberToArray[currentIndex]; numberToArray[currentIndex] = temp; } } // 组成结果返回 int result = 0; for (int i = length - 1; i >= 0; i--) { result *= 10; result += numberToArray[i]; } return result; } };
猜你喜欢
- 11天前(大理悦云雅阁酒店电话)雅阁酒店集团|端午佳节礼遇,大理悦云雅阁度假酒店
- 11天前(四川率先建立“双定向”基层文化人才职称评审通道机制)四川率先建立“双定向”基层文化人才职称评审通道机制
- 11天前(哈弗h9优惠9万是真的吗)热浪来袭,哈弗H9超值补贴火热加码
- 11天前(万达酒店及度假村连续五年荣获“中国饭店集团60强”)万达酒店及度假村连续五年荣获“中国饭店集团60强”
- 11天前(福朋喜来登酒店宴会厅)福朋喜来登品牌亮相北部湾城市群 阳江中心福朋喜来登酒店开业
- 11天前(屿见不一样是哪个酒店)屿见白纱,遇见自己 “佳能PhotoGirls屿见白纱”摄影派对玩转海岛
- 11天前(中国最好的避暑山庄)2025中国十大避暑山庄评选揭晓,澳涞山庄夺魁
- 11天前(当科学邂逅喜剧:科技馆喜剧嘉年华背后的"文旅破壁者")当科学邂逅喜剧:科技馆喜剧嘉年华背后的"文旅破壁者"
- 11天前(天津四季酒店开业时间)天津四季酒店邀你开启灿烂暑假
- 11天前(上海迪士尼 夏天)酷爽夏日,奇妙相伴!来上海迪士尼度假区清凉入夏
网友评论
- 搜索
- 最新文章
- (2020广州车展哈弗)你的猛龙 独一无二 哈弗猛龙广州车展闪耀登场
- (哈弗新能源suv2019款)智能科技颠覆出行体验 哈弗重塑新能源越野SUV价值认知
- (2021款全新哈弗h5自动四驱报价)新哈弗H5再赴保障之旅,无惧冰雪护航哈弗全民电四驱挑战赛
- (海南航空现况怎样)用一场直播找到市场扩张新渠道,海南航空做对了什么?
- (visa jcb 日本)优惠面面俱到 JCB信用卡邀您畅玩日本冰雪季
- (第三届“堡里有年味·回村过大年”民俗花灯会活动)第三届“堡里有年味·回村过大年”民俗花灯会活动
- (展示非遗魅力 长安启源助力铜梁龙舞出征)展示非遗魅力 长安启源助力铜梁龙舞出征
- (阿斯塔纳航空公司)阿斯塔纳航空机队飞机数量增至50架
- (北京香港航班动态查询)香港快运航空北京大兴新航线今日首航
- (我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉)我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉
- 热门文章