C#短网址算法
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
//62进制
public static int BASE_NUM = 62;
//62进制字母顺序
public static final char[] array = {''G'', ''q'', ''w'', ''0'', ''H'', ''e'', ''T'', ''F'', ''9'', ''r'', ''V'', ''t'', ''y'', ''u'', ''N'', ''i'', ''6'', ''D'', ''o'', ''p'', ''L'', ''a'', ''s'', ''d'', ''K'', ''f'', ''g'', ''h'', ''j'', ''k'', ''4'', ''l'', ''z'', ''x'', ''c'', ''v'', ''b'', ''S'', ''n'', ''m'', ''1'', ''Z'', ''3'', ''5'', ''Q'', ''W'', ''E'', ''R'', ''7'', ''Y'', ''U'', ''I'', ''O'', ''2'', ''P'', ''A'', ''J'', ''X'', ''C'', ''B'', ''8'', ''M''};
/**
* 将10进制数转为62进制字符串(短网址)
*
* @param number
* @return
*/
public static String getShortUrlByLongNum(Long number) {
Long rest = number;
Stack<Character> stack = new Stack<Character>();
StringBuilder result = new StringBuilder(0);
if (0 == rest) {
return String.valueOf(array[0]);
}
while (rest != 0) {
stack.add(array[new Long((rest - (rest / BASE_NUM) * BASE_NUM)).intValue()]);
rest = rest / BASE_NUM;
}
for (; !stack.isEmpty(); ) {
result.append(stack.pop());
}
return result.toString();
}
/**
* 通过短网址返回10进制数
*
* @param shortUrl
* @return
*/
public static Long getLongNumByShortUrl(String shortUrl) {
long multiple = 1;
long result = 0;
Character c;
for (int i = 0; i < shortUrl.length(); i++) {
c = shortUrl.charAt(shortUrl.length() - i - 1);
result += valueOfCharacter(c) * multiple;
multiple = multiple * BASE_NUM;
}
return result;
}
/**
* 字母对应的值 如array数组 G对应0 q对应1
*
* @param c
* @return
*/
private static int valueOfCharacter(Character c) {
for (int i = 0; i < array.length; i++) {
if (c == array[i]) {
return i;
}
}
return -1;
}
准备一个被打乱的数组,存放A-Za-Z0-9这62个字符 ,预先产生一个网址ID,将这个ID通过 _10_to_62 解析转换成62进制的短网址符。 存储ID,短网址code,页面URL 算法如下,很简单,时间效率也比较高,甚至,有了存储的ID,要不要存储短网址code都无所谓, 这个算法时间复杂度本来就是n,而一般的短网址长度也就1~7位,时间复杂度也可以算作O(1)常量了。 该文章在 2021/1/29 17:11:11 编辑过 |
关键字查询
相关文章
正在查询... |