• 670. Maximum Swap


    package LeetCode_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.
    
    Example 1:
    Input: 2736
    Output: 7236
    Explanation: Swap the number 2 and the number 7.
     * */
    class Solution {
        /*
        * solution: Array,Time complexity:O(n), Space complexity:O(1)
        * */
        fun maximumSwap(num: Int): Int {
            val digits = num.toString().toCharArray()
            val map = IntArray(10)
            for (i in digits.indices) {
                map[digits[i] - '0'] = i
            }
            /*
            * array for saving last position of digit in this num, for example: 98368
            map is: 0,0,0,2,0,0,3,0,4,0, mean:
            last position of 9 is 0,
            last position of 8 is 4,
            last position of 3 is 2,
            last position of 6 is 3
            * */
            for (i in digits.indices) {
                //For each position,
                //we check whether there exists a larger digit in this num (start from 9 to current digit)
                for (d in 9 downTo digits[i] - '0' + 1) {
                    /*
                    * for example: 2736, the position of largest digit (7) is 1, 1 is behind 0, so swap they are,
                    * */
                    if (map[d] > i) {
                        swap(digits, map[d], i)
                        return String(digits).toInt()
                    }
                }
            }
            //if no need swap, return original num
            return num
        }
    
        private fun swap(str: CharArray, start: Int, end: Int) {
            val temp = str[end]
            str[end] = str[start]
            str[start] = temp
        }
    }
  • 相关阅读:
    vue笔记四
    vue笔记三(组件)
    vue笔记二
    Linux04——手动修改IP和关闭防火墙服务
    Linux03——磁盘分区和挂载
    Linux02——解压和压缩
    Linux01——常用命令
    Springmvc-crud-07(springmvc标签错误)
    Springmvc-crud-06(路径忘记加上“/”错误)
    Springmvc-crud-05(路径错误)
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13942868.html
Copyright © 2020-2023  润新知