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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值