classSolution{public:intmaximumSwap(intnum){stringres = to_string(num),back=res;for(inti=res.size()-2;i>=0;--i){ back[i] = max(back[i],back[i+1]); }for(inti=0;i!=res.size();++i){if(res[i]==back[i])continue;for(intj=res.size()-1;j>i;--j){if(res[j]==back[i...
classSolution {publicintmaximumSwap(intnum) {char[] chars =String.valueOf(num).toCharArray();intlen =chars.length;//先找最大数的值和下标,需要找最右侧的最大值intfrom = 0;booleanswap =false;while(from < len -1 && swap ==false) { charmax = '0';intmaxIndex = -1;for(inti = from;...
leetcode 670. Maximum Swap Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get. Example 1:Input:2736Output:7236Explanation:Swapthenumber2andthenumber7. Example 2:Input:9973Output:9973Explanation:N...
Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get. Example 1: Input: 2736 Output: 7236 Explanation: Swap the number 2 and the number 7. Example 2: Input: 9973 Output: 9973 Explanation: ...
https://leetcode.com/problems/maximum-swap/discuss/107084/C%2B%2B-3ms-O(n)-time-O(n)-space-DP-solution https://leetcode.com/problems/maximum-swap/discuss/107073/C%2B%2B-one-pass-simple-and-fast-solution%3A-1-3ms-O(n)-time
Leetcode 670: Maximum Swap Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get. Example 1: Input: 2736 Output: 7236 Explanation: Swap the number 2 and the number 7....
LeetCode 670: Maximum Swap Note: 1. Scan from the small level. 2. Only take the index larger one if same digits. classSolution {publicintmaximumSwap(intnum) {if(num < 12) {returnnum; } List<Integer> digits =newArrayList<>();while(num > 0) {...
[LeetCode] 670. Maximum Swap You are given an integer num. You can swap two digits at most once to get the maximum valued number. Return the maximum valued number you can get. Example 1: Input: num = 2736 Output: 7236 Explanation: Swap the number 2 and the number 7....
1classSolution {2func maximumSwap(_ num: Int) ->Int {3vararr:[Character] =Array(String(num))4varres:Int =num5varn:Int =arr.count6varstr:String =String()7foriin0..<n8{9forjin(i +1)..<n10{11arr.swapAt(i,j)12str =String(arr)13res = max(res, Int(str) ??0)14arr.swapAt(...
670. Maximum Swap packageLeetCode_670/*** 670. Maximum Swap *https://leetcode.com/problems/maximum-swap/description/* * Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. * Return the maximum valued number you could get....