java顺序表查找重复元素_iOS/Java查找字符串中重复字符,开始下标,重复个数..?

查找字符串中重复字符,下标,重复个数..?

思路:一遍循环将字符串转换为字典,字典中保存想要的结果集

1,最外层是数组,保证输入顺序与字符串中出现顺序一致.

2,获取数组lastObject

3,比较lastObject的key,与当前字符是否相同, 相同:数据加

4,key不同,判断出现次数,lenth==1,替换, >1新增.

//1.声明一个共用数组,self.arrayM: NSMutableArray

//2.code

//3.kvc获取结果集

- (void)repeatInfoIn:(NSString *)string

{

NSInteger count = string.length;

if (count <= 1) return;

for (NSInteger i = 0; i < string.length; i++) {

NSString *ichar = [string substringWithRange:NSMakeRange(i, 1)];

if (i == 0) {

NSDictionary *valueDict = @{@"key": ichar, @"index": @(i), @"count": @(1), @"subStr": ichar};

[self.arrayM addObject:valueDict];

continue;

}

NSMutableDictionary *previousDict = [[self.arrayM lastObject] mutableCopy];

NSString *previousKey = previousDict[@"key"];

NSInteger previousCount = [previousDict[@"count"] integerValue];

if ([previousKey isEqualToString:ichar]) {

previousCount += 1;

NSString *subStr = [NSString stringWithFormat:@"%@%@", previousDict[@"subStr"], ichar];

previousDict[@"count"] = @(previousCount);

previousDict[@"subStr"] = subStr;

[self.arrayM replaceObjectAtIndex:(self.arrayM.count-1) withObject:previousDict];

}else {

if (previousCount == 1) {

NSDictionary *valueDict = @{@"key": ichar, @"index": @(i), @"count": @(1), @"subStr": ichar};

[self.arrayM replaceObjectAtIndex:(self.arrayM.count-1) withObject:valueDict];

}else {

NSDictionary *valueDict = @{@"key": ichar, @"index": @(i), @"count": @(1), @"subStr": ichar};

[self.arrayM addObject:valueDict];

}

}

}

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

[super touchesBegan:touches withEvent:event];

NSString *string = @"asdqwezccdddd";

[self repeatInfoIn:string];

NSLog(@"%@", self.arrayM);

NSArray *subStrArray = [self.arrayM valueForKeyPath:@"subStr"];

NSLog(@"%@", subStrArray);

}

/* java中可能有更简便的方法,我这里只是一种思路推演. */

static void linkedHashMapOperate() {

String str = "aaasdqwezccdddd";

ArrayList>> list = new ArrayList<>();

for (int i = 0; i < str.length(); i++) {

char key = str.charAt(i);

int lastIndex = Math.max(0, list.size()-1);

if (i == 0) {

HashMap> hashMap = new HashMap<>();

var value = new HashMap();

value.put("index", i);

value.put("lenth", 1);

value.put("key", key);

hashMap.put(key, value);

list.add(hashMap);

continue;

}

HashMap> hashMap = list.get(lastIndex);

Set>> entries = hashMap.entrySet();

char lastKey = key;

var value = new HashMap();

for (Map.Entry> entry: entries) {

lastKey = (char) entry.getKey();

value = entry.getValue();

}

if (lastKey == key) {//说明上一个元素与当前元素重复

int index = (int) value.get("index");

int lenth = (int) value.get("lenth");

index += 1;

lenth += 1;

value.put("index", index);

value.put("lenth", lenth);

}else {

//判断上一个元素lenth==1

int lenth = (int) value.get("lenth");

if (lenth==1) {//替换--->key

value.put("key",key);

hashMap.remove(lastKey);

hashMap.put(key,value);

list.set(lastIndex, hashMap);

}else {//新增

HashMap> nHashMap = new HashMap();

var nValue = new HashMap();

nValue.put("index", i);

nValue.put("lenth", 1);

nValue.put("key", key);

nHashMap.put(key, nValue);

list.add(nHashMap);

}

}

}

System.out.println(list);

}

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页