面试题-字符串转字典

昨天面试的时候遇到一道字符串转字典的题,当时把思路写了下来,今天用代码实现一下。

题目的内容大致是这样:

把一个字符串如:afh123jkkx2324klsdkjl839283a3b323n,取出字母作为key,数字作为value,存入一个字典。

由于字符串的内容只有字母和数字,所以我的大致思路是遍历字符串,判断每个字符的ASCII码,0-9的ASCII码是48~57。在每次字母和数字切换的位置标记为分割点,然后依次取出key,value.
下面用两种方式实现

第一种

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
- (void) stringToDic:(NSString *)string {

NSMutableDictionary *dic = [NSMutableDictionary dictionary];
BOOL isNumber = YES;
NSInteger startPoint = 0;//记录开始点
NSString *tempKey = @"";
NSString *tempValue = @"";
for (NSInteger i=0; i< string.length; i++) {

unichar charStr = [string characterAtIndex:i];
int ascii = toascii(charStr);//获取ASCII码
//判断key和value都不为空,保存到dic
if (![tempKey isEqualToString:@""] && ![tempValue isEqualToString:@""]) {
[dic setObject:tempValue forKey:tempKey];
tempKey = @"";
tempValue = @"";
}
if (ascii>=48 && ascii <= 57) {//数字
if (isNumber == NO) {
tempKey = [string substringWithRange:NSMakeRange(startPoint, i-startPoint)];
startPoint = i;
isNumber = YES;
}
}else{//字母
if (isNumber == YES) {
tempValue = [string substringWithRange:NSMakeRange(startPoint, i-startPoint)];
startPoint = i;
isNumber = NO;
}
}
if(i == string.length -1){//最后一个单独处理
if (![tempKey isEqualToString:@""] && ![tempValue isEqualToString:@""]) {
[dic setObject:tempValue forKey:tempKey];
}
}
}
NSLog(@"%@",dic);
}

目标字符串:afh123jkkx2324klsdkjl839283a3b323n
打印结果

1
2
3
4
5
6
7
{
a = 3;
afh = 123;
b = 323;
jkkx = 2324;
klsdkjl = 839283;
}

第二种

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
- (void) stringToDictionary:(NSString *)string {

NSMutableArray *array = [NSMutableArray array];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
BOOL isNumber = YES;
for (NSInteger i=0; i< string.length; i++) {

unichar charStr = [string characterAtIndex:i];
int ascii = toascii(charStr);

if (ascii>=48 && ascii <= 57) {//数字
if (isNumber == NO) {
isNumber = YES;
[array addObject:@(i)];
}


}else{//字母
if (isNumber == YES) {
isNumber = NO;
[array addObject:@(i)];
}
}
}

//最后一个单独处理
[array addObject:@(string.length)];
//根据分割点切割成字母和数字存入零时数组
NSMutableArray *keyValues = [NSMutableArray array];
for (NSInteger i = 0; i < array.count-1; i++) {
NSInteger start = [array[i] integerValue];
NSInteger lengt = [array[i+1] integerValue] -start;
NSString *itemStr = [string substringWithRange:NSMakeRange(start, lengt)];
[keyValues addObject:itemStr];
}
NSLog(@"%@",keyValues);
//从数组中取出元素存入字典,循环的步数为2
for (NSInteger i = 0; i<keyValues.count-1; i=i+2) {
NSString *key = keyValues[i];
NSString *value = keyValues[i+1];
[dic setObject:value forKey:key];
}
NSLog(@"%@",dic);
}

目标字符串:afh123jkkx2324klsdkjl839283a3b323n
打印结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2018-07-26 12:41:03.921236+0800 Demo[42540:6832719] (
afh,
123,
jkkx,
2324,
klsdkjl,
839283,
a,
3,
b,
323,
n
)
2018-07-26 12:41:03.921494+0800 Demo[42540:6832719] {
a = 3;
afh = 123;
b = 323;
jkkx = 2324;
klsdkjl = 839283;
}

注意

这里需要注意的是字母和数字可能不是成对出现,那么需要处理异常情况,这里只是处理了结束位置为字母的情况,还有一种开始位置为数字的情况没有讨论。