一道面试算法题

</pre><p></p>看到群里面有人发面试题<p></p><p></p><pre name="code" class="java">Best Regards
Iris
Q1. 
Given a array of 10,000 random intergers, select the biggest 100 
numbers.
1) The order of the result numbers does not matter;
2) Take care about the algorithm performance and big O 

complexity.


Q2.
Find the string which has this hash: 25267566250558
The string has length 8.
Characters can be from: c,e,i,a,r,w,u,s,p
 
The hash function works like this:
hash(str):
    1. LETTERS = c, e, i, a, r, w, u, s, p
    2. h = 7
    3. for c in str:
        1. i = index of c in LETTERS
        2. h = 37 * h + i
    4. return h
 
Please send us the string you found, and the code you used to find 
it.
cueasa
--
收到请回复~谢谢 Iris
以回复邮件为准,否则视为主动放弃面试机会 


第一题,主要是排序就不说了,第二到题我的做法是

	public static char[] k = {'c', 'e', 'i', 'a', 'r', 'w', 'u', 's', 'p'};
	public static void find(double mumber) {
		for (int i = 8; i>=0 ; i--) {
			if (mumber == 7) {   //h已经为7了结束递归了
				System.out.println("结束了");
				return;
			}
			double temp = mumber - i;  //反过来求每一个h和字符下标
			if (temp % 37 == 0) {
//				System.out.println("下标为" + i);
				System.out.print(k[i]);
				find(temp / 37);		//递归查找下一个字符
			}
		}
	}

	public static void main(String[] args) {
		double double1 = 25267566250558L;
		find(double1);
	}

这题有刚好只有一个结果,所以没问题,我又想给它改良


改良版

public static char[] k = { 'c', 'e', 'i', 'a', 'r', 'w', 'u', 's', 'p' };

	public static void find(double mumber, char[] a,int j) {
		boolean hasResult = true;
		for (int i = 0; i <= 8; i++) { // 字母表长度为9
			if (mumber == 7) { // h 这个时候为7时停止,没考虑字符串str长度为8的限制
				System.out.println(a); // 打印结果
				// System.out.println("结束了");
				return;
			}
			double temp = mumber - i; //开始反向计算字符串与h = 37 * h + i
			if (temp % 37 == 0) {	//能整除
				a[j] = k[i];
				find(temp / 37, a,j+1);
			}
		}
	}
	
	public static void main(String[] args) {
		double double1 = 25267566250558L;
		find(double1, new char[9],0);
	}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值