首页 > 其他分享 >leetcode-1323-easy

leetcode-1323-easy

时间:2023-02-15 21:35:10浏览次数:35  
标签:1323 digit chars results number num easy Changing leetcode

Maximum 69 Number

You are given a positive integer num consisting only of digits 6 and 9.

Return the maximum number you can get by changing at most one digit (6 becomes 9, and 9 becomes 6).

Example 1:

Input: num = 9669
Output: 9969
Explanation: 
Changing the first digit results in 6669.
Changing the second digit results in 9969.
Changing the third digit results in 9699.
Changing the fourth digit results in 9666.
The maximum number is 9969.
Example 2:

Input: num = 9996
Output: 9999
Explanation: Changing the last digit 6 to 9 results in the maximum number.
Example 3:

Input: num = 9999
Output: 9999
Explanation: It is better not to apply any change.
Constraints:

1 <= num <= 104
num consists of only 6 and 9 digits.

思路一:转成 String,然后处理第一次遇到的 6。还有一种是把整数依次分解成个位数

    public int maximum69Number (int num) {
        char[] chars = String.valueOf(num).toCharArray();

        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '6') {
                chars[i] = '9';
                return Integer.parseInt(new String(chars));
            }
        }

        return num;
    }

标签:1323,digit,chars,results,number,num,easy,Changing,leetcode
From: https://www.cnblogs.com/iyiluo/p/17124768.html

相关文章

  • leetcode-1346-easy
    CheckifNandItsDoubleExistGivenanarrayarrofintegers,checkifthereexisttwoindicesiandjsuchthat:i!=j0<=i,j<arr.lengtharr[i]==2......
  • leetcode-908-easy
    SmallestRangeIYouaregivenanintegerarraynumsandanintegerk.Inoneoperation,youcanchooseanyindexiwhere0<=i<nums.lengthandchangenums......
  • leetcode-821-easy
    ShortestDistancetoaCharacterGivenastringsandacharactercthatoccursins,returnanarrayofintegersanswerwhereanswer.length==s.lengthand......
  • leetcode-10460-easy
    LastStoneWeightYouaregivenanarrayofintegersstoneswherestones[i]istheweightoftheithstone.Weareplayingagamewiththestones.Oneachtur......
  • leetcode-783-easy
    MinimumDistancdBetweenNodesGiventherootofaBinarySearchTree(BST),returntheminimumdifferencebetweenthevaluesofanytwodifferentnodesinthe......
  • leetcode-824-easy
    GoatLatinYouaregivenastringsentencethatconsistofwordsseparatedbyspaces.Eachwordconsistsoflowercaseanduppercaselettersonly.Wewouldlik......
  • 【LeetCode栈与队列#05】滑动窗口最大值
    滑动窗口最大值力扣题目链接(opensnewwindow)给定一个数组nums,有一个大小为k的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的k个数字。......
  • [Leetcode]435. 无重叠区间
    435.无重叠区间给定一个区间的集合 intervals ,其中intervals[i]=[starti,endi] 。返回需要移除区间的最小数量,使剩余区间互不重叠 。示例1:输入:intervals......
  • 【算法训练营day44】完全背包基础 LeetCode518. 零钱兑换II LeetCode377. 组合总和IV
    LeetCode518.零钱兑换II题目链接:518.零钱兑换II独上高楼,望尽天涯路组合问题和完全背包的混合应用,感觉脑中模拟的不是很清晰,但是靠着背包问题的代码技巧和模板就能比较......
  • 【算法训练营day43】LeetCode1049. 最后一块石头的重量II LeetCode494. 目标和 LeetCo
    LeetCode1049.最后一块石头的重量II题目链接:1049.最后一块石头的重量II独上高楼,望尽天涯路一开始还是没有想到怎么转化成01背包问题,所以直接看题解找思路慕然回首,灯......