LeetCode 5108. Encode Number - Java - 2进制

题目链接https://leetcode-cn.com/problems/encode-number/


Given a non-negative integer num, Return its encoding string. @wowpH

The encoding is done by converting the integer to a string using a secret function that you should deduce from the following table:

enocde_number_wowpH

Example 1:

Input: num = 23
Output: "1000"

Example 2:

Input: num = 107
Output: "101100"

Constraints:

  • 0 <= num <= 10^9

题解

f(n) 前面加"1" 10进制 n
"" "1" 1 0
"0" "10" 2 1
"1" "11" 3 2
"00" "100" 4 3
"01" "101" 5 4
"10" "110" 6 5
"11" "111" 7 6
"000" "1000" 8 7

Java代码

class Solution {
    public String encode(int num) {
        return Integer.toBinaryString(num + 1).substring(1);
    }
}

原文链接https://www.cnblogs.com/wowpH/p/11880529.html


- wowpH -
posted @ 2019-11-18 10:51  wowpH  阅读(306)  评论(0编辑  收藏  举报