How do I create a URL shortener?

后端 未结 30 2055
我寻月下人不归
我寻月下人不归 2020-11-22 05:11

I want to create a URL shortener service where you can write a long URL into an input field and the service shortens the URL to \"http://www.example.org/abcdef\

30条回答
  •  别跟我提以往
    2020-11-22 05:22

    public class TinyUrl {
        
            private final String characterMap = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            private final int charBase = characterMap.length();
        
            public String covertToCharacter(int num){
                StringBuilder sb = new StringBuilder();
        
                while (num > 0){
                    sb.append(characterMap.charAt(num % charBase));
                    num /= charBase;
                }
        
                return sb.reverse().toString();
            }
        
            public int covertToInteger(String str){
                int num = 0;
                for(int i = 0 ; i< str.length(); i++)
                    num += characterMap.indexOf(str.charAt(i)) * Math.pow(charBase , (str.length() - (i + 1)));
        
                return num;
            }
    }
        
    class TinyUrlTest{
        
        public static void main(String[] args) {
            TinyUrl tinyUrl = new TinyUrl();
            int num = 122312215;
            String url = tinyUrl.covertToCharacter(num);
            System.out.println("Tiny url:  " + url);
            System.out.println("Id: " + tinyUrl.covertToInteger(url));
        }
    }
    

提交回复
热议问题