Set Mismatch

class Solution {
public:
    vector<int> findErrorNums(vector<int>& nums) {
        int len = nums.size();
        vector<int> res;
        vector<int> a(len+1);   //因为数组从0开始,所以除去0,在后面加上1位

        for (int i = 0; i<len; i++) {
            if (a[nums[i]]==0) {
                a[nums[i]]+=1;
            }else{
                res.push_back(nums[i]);
            }
        }

        for (int j = 1; j<=len; j++) {
            if (a[j]==0) {
                res.push_back(j);
            }
        }

        return res;
    }
};
2018/1/18 posted in  leetcode

Find Anagram Mappings

class Solution {
public:
    vector<int> anagramMappings(vector<int>& A, vector<int>& B) {
        unordered_map<int, int> map;
        vector<int> res;
        for (int i = 0; i<B.size(); i++) {
            map[B[i]]=i;
        }
        for (int j = 0; j<A.size(); j++) {
            res.push_back(map[A[j]]);
        }
        return res;
    }
};
2018/1/18 posted in  leetcode

Length of Last Word

![](media/15162389890253/15162389958774.jpg)
class Solution {
public:
    int lengthOfLastWord(string s) {
        int len = s.size();
        int cout = 0;
        for (int i=len-1; i>=0; i--) {
            if(cout>0&&s[i]==' ') break;

            if (s[i]==' ') {
                continue;
            }else{
                cout++;
            }

        }
        return cout;
    }
};
2018/1/18 posted in  leetcode

Find matching parenthesis


#import <Foundation/Foundation.h>

int findParenMatch(NSString *s, int n) {
    int res = -1;
    NSString *tmp = [s substringWithRange:NSMakeRange(n, 1)];
    if ([tmp isEqualToString:@"("]) {
        int logF = 0;
        for (int i = n+1; i<s.length; i++) {
            NSString *tmpStrF = [s substringWithRange:NSMakeRange(i, 1)];
            if ([tmpStrF isEqualToString:@"("]) logF++;
            if ([tmpStrF isEqualToString:@")"]) {
                logF--;
                if (logF<0) {
                    res = i;
                    break;
                }
            }
        }
    }else if ([tmp isEqualToString:@")"]){
        int logB = 0;
        for (int i = n-1; i>=0; i--) {
            NSString *tmpStrB = [s substringWithRange:NSMakeRange(i, 1)];
            if ([tmpStrB isEqualToString:@")"]) logB++;
            if ([tmpStrB isEqualToString:@"("]) {
                logB--;
                if (logB<0) {
                    res = i;
                    break;
                }
            }
        }
    }
    return res;
}
2018/1/10 posted in  codeWars

KVO自己的理解

  1. KVO利用runtime,生成了一个对象的子类,并生成子类对象替换原来对象的isa指针,重写了set方法。
  2. KVO是基于KVC的,可以明显的发现在改变容器的时候,通过KVC改变改变容器中的值或者使用set方法时,会触发KVO通知函数,而简单的使用addObject:方法时却没有触发,这是因为KVO只响应set方法。可以说kvc是kvo的入口
  3. 直接使用KVO在项目中不是很好用,代码结构比较松散,需要自己封装或使用其它三方框架。
2018/1/8 posted in  iOS

Where my anagrams at?

#import <Foundation/Foundation.h>

NSArray *anagrams(NSString *s, NSArray *a) {
    NSMapTable *map = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory valueOptions:NSMapTableStrongMemory];
    for (int i = 0; i<[s length]; i++) {
        NSString *str = [s substringWithRange:NSMakeRange(i, 1)];
        if ([map objectForKey:str]) {
            [map setObject:@([[map objectForKey:str] intValue]+1) forKey:str];
        }else{
            [map setObject:@(1) forKey:str];
        }
    }
    NSMutableArray *res = a.mutableCopy;
    for (NSString *ele in a) {
        if(ele.length!=s.length){
            [res removeObject:ele];
            continue;
        }
        NSMapTable *tmp = [map copy];
        for (int j=0; j<[ele length]; j++) {
            NSString *tmpStr = [ele substringWithRange:NSMakeRange(j, 1)];
            if ([tmp objectForKey:tmpStr]) {
                int log =[[tmp objectForKey:tmpStr] intValue]-1;
                if (log<0){
                    [res removeObject:ele];
                    break;
                }
                [tmp setObject:@(log) forKey:tmpStr];
            }else{
                [res removeObject:ele];
                break;
            }
        }
    }
    return res;
}
2018/1/6 posted in  codeWars