题目来源
力扣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天前(哈弗h9优惠9万是真的吗)热浪来袭,哈弗H9超值补贴火热加码
- 11天前(四川推进世界重要旅游目的地建设工作)四川推进世界重要旅游目的地建设
- 11天前(艾美酒店连锁)艾美酒店全球夏日计划回归,联手Wishbone主厨推出创新冰饮
- 11天前(福朋喜来登酒店宴会厅)福朋喜来登品牌亮相北部湾城市群 阳江中心福朋喜来登酒店开业
- 11天前(新西兰“空降”上海:新西兰旅游局邀请你来“玩真的”!)新西兰“空降”上海:新西兰旅游局邀请你来“玩真的”!
- 11天前(苏梅岛普吉岛哪个好玩)苏梅岛金普顿基塔蕾度假酒店推出家庭度假套餐
- 11天前(中国旅游集团旗下酒店)中国旅游集团酒店控股有限公司战略投资雅阁酒店集团
- 11天前(“三天跨两城”催生租车新需求,神州租车清明跨城订单同比增长416%)“三天跨两城”催生租车新需求,神州租车清明跨城订单同比增长416%
- 11天前(锦江 iu)锦江荟APP原生鸿蒙版正式上线打造全场景旅行服务新体验
网友评论
- 搜索
- 最新文章
- (2020广州车展哈弗)你的猛龙 独一无二 哈弗猛龙广州车展闪耀登场
- (哈弗新能源suv2019款)智能科技颠覆出行体验 哈弗重塑新能源越野SUV价值认知
- (2021款全新哈弗h5自动四驱报价)新哈弗H5再赴保障之旅,无惧冰雪护航哈弗全民电四驱挑战赛
- (海南航空现况怎样)用一场直播找到市场扩张新渠道,海南航空做对了什么?
- (visa jcb 日本)优惠面面俱到 JCB信用卡邀您畅玩日本冰雪季
- (第三届“堡里有年味·回村过大年”民俗花灯会活动)第三届“堡里有年味·回村过大年”民俗花灯会活动
- (展示非遗魅力 长安启源助力铜梁龙舞出征)展示非遗魅力 长安启源助力铜梁龙舞出征
- (阿斯塔纳航空公司)阿斯塔纳航空机队飞机数量增至50架
- (北京香港航班动态查询)香港快运航空北京大兴新航线今日首航
- (我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉)我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉
- 热门文章