zoukankan      html  css  js  c++  java
  • 🌞188 · 插入五

    题目描述

    给定一个数字,在数字的任意位置插入一个5,使得插入后的这个数字最大。


    示例

    输入:  a = 234
    输出: 5234
    

    题解

    public class Solution {
        /**
         * @param a: A number
         * @return: Returns the maximum number after insertion
         */
        public int InsertFive(int a) {
            // write your code here
            if(a<0){
                String str = ""+a;
                int b = -1;
                for (int i = 1; i < str.length(); i++) {
                    b = i;
                    String as = ""+str.charAt(i);
                    int inas = Integer.valueOf(as);
                    if (inas>5){
                        break;
                    }
                }
                StringBuilder s = new StringBuilder(str);
                s.insert(b,'5');
                int c = Integer.valueOf(s.toString());
                return c;
            }
            String str = ""+a;
            int b = -1;
            for (int i = 0; i < str.length(); i++) {
                b = i;
                String as = ""+str.charAt(i);
                int inas = Integer.valueOf(as);
                if (inas<5){
                    break;
                }
            }
            StringBuilder s = new StringBuilder(str);
            s.insert(b,'5');
            int c = Integer.valueOf(s.toString());
            return c;
        }
    }
    
  • 相关阅读:
    UESTC
    Education Round 8 A
    Gym
    Gym
    hdoj 1159 Common Subsequence
    UVA
    UESTC
    51Nod 1068 Bash游戏 V3 (这规律不好找)
    51Nod 1066 Bash游戏
    51Nod 1002 数塔取数问题
  • 原文地址:https://www.cnblogs.com/charlottepl/p/15700081.html
Copyright © 2011-2022 走看看