2011.04.22——— 星期三的面试题

本文总结了常见的编程面试题目,包括字符串转整数的实现、查找两数组交集、二分查找算法、链表逆序输出等,并介绍了使用Map统计字符串中出现最多的字符及其频率的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天面试去了 把几道面试题 记一下吧

1、实现把一个整数字符串转换为整数
看一下Integer.parsentInt()
/**
* Parses the string argument as a signed integer in the radix
* specified by the second argument. The characters in the string
* must all be digits of the specified radix (as determined by
* whether {@link java.lang.Character#digit(char, int)} returns a
* nonnegative value), except that the first character may be an
* ASCII minus sign <code>'-'</code> (<code>'\u002D'</code>) to
* indicate a negative value. The resulting integer value is returned.
* <p>
* An exception of type <code>NumberFormatException</code> is
* thrown if any of the following situations occurs:
* <ul>
* <li>The first argument is <code>null</code> or is a string of
* length zero.
* <li>The radix is either smaller than
* {@link java.lang.Character#MIN_RADIX} or
* larger than {@link java.lang.Character#MAX_RADIX}.
* <li>Any character of the string is not a digit of the specified
* radix, except that the first character may be a minus sign
* <code>'-'</code> (<code>'\u002D'</code>) provided that the
* string is longer than length 1.
* <li>The value represented by the string is not a value of type
* <code>int</code>.
* </ul><p>
* Examples:
* <blockquote><pre>
* parseInt("0", 10) returns 0
* parseInt("473", 10) returns 473
* parseInt("-0", 10) returns 0
* parseInt("-FF", 16) returns -255
* parseInt("1100110", 2) returns 102
* parseInt("2147483647", 10) returns 2147483647
* parseInt("-2147483648", 10) returns -2147483648
* parseInt("2147483648", 10) throws a NumberFormatException
* parseInt("99", 8) throws a NumberFormatException
* parseInt("Kona", 10) throws a NumberFormatException
* parseInt("Kona", 27) returns 411787
* </pre></blockquote>
*
* @param s the <code>String</code> containing the integer
* representation to be parsed
* @param radix the radix to be used while parsing <code>s</code>.
* @return the integer represented by the string argument in the
* specified radix.
* @exception NumberFormatException if the <code>String</code>
* does not contain a parsable <code>int</code>.
*/
public static int parseInt(String s, int radix)
throws NumberFormatException
{
if (s == null) {
throw new NumberFormatException("null");
}

if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
}

if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
}

int result = 0;
boolean negative = false;
int i = 0, max = s.length();
int limit;
int multmin;
int digit;

if (max > 0) {
if (s.charAt(0) == '-') {
negative = true;
limit = Integer.MIN_VALUE;
i++;
} else {
limit = -Integer.MAX_VALUE;
}
multmin = limit / radix;
if (i < max) {
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
} else {
result = -digit;
}
}
while (i < max) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
if (negative) {
if (i > 1) {
return result;
} else { /* Only got "-" */
throw NumberFormatException.forInputString(s);
}
} else {
return -result;
}
}

2、找出两个数组中相同项
3、二分法 有返回 1 没有返回-1
先排序。。
int[] arrays = new int[]{1,2,3,4,5,6};
int find = 4;
int result = -1;
int start = 0;
int end = arrays.length-1;
int index = 0
while(true){
index = (start+end)/2;

if(find > arrays[index]){
start = index+1;
}else if(find < arrays[index]){
end = index-1;
}else{
result = 1;
break;
}

}

4、单向链表 反向输出 输入 a->c->b->e 输出e->b->c->a
参考:[url]http://article.yeeyan.org/view/9225/173996[/url]
[url]http://apps.hi.baidu.com/share/detail/30195452[/url]
5、字符串中重复最多的字母及其次数
答案竟然是用map,key为字母 value为次数
6、sql

参考:[url]http://blog.youkuaiyun.com/ccsuliuxing/archive/2007/04/18/1568821.aspx[/url]
[url]http://topic.youkuaiyun.com/u/20110110/09/32a1e6ad-c619-4f1f-b79a-b2a226af56bc.html[/url]

表A(id number(10),name varchar2(10),regdate date)用户ID,用户姓名,注册时间.
表B(id number(10),groupid varchar2(10))用户ID,用户分组组号
表A(id number(10),name varchar2(10))用户ID,用户姓名

写出下面的SQL语句
1.统计A表中每个月注册用户数
2.统计A表中有姓名相同的用户数
3.如果表A中有姓名相同的用户,把相同的查出,写入表C中
4.A中ID有多个相同的数据,A中姓名相同的ID只保留注册时间最大的数据

1 
select count(*),to_char(regdate, 'yyyymm ') from A group by to_char(regdate, 'yyyymm ');
2
select count(*) from (select name from A group by name having count(*) > 1);
3
insert into C(name2) select name from A group by name having count(*) > 1;
4
delete from A E where e.regdate < (select max(regdate) from a X where E.id = X.id);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值