ARTS-2018.7.4

每周至少做一个 leetcode 的算法题(Algorithm)、阅读并点评一篇英文技术文章(Review)、学习一个技术技巧(Tip)、分享一篇有观点和思考的技术文章(Share)。

the Purpose of ARTS:

  • A means learn the Algorithm

  • R means learn the English

  • T means learn the techniques

  • S means learn how to influence people

Algorithms

有序整型数组去重,要求实现过程中不能创建新的数组

/*
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.

It doesn't matter what you leave beyond the returned length.

Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn't matter what values are set beyond the returned length.

*/

/* 思路
 设置两个指针,slow 和 fast,fast 在前,slow 在后。
 遍历时当 fast 所指向的值和 slow 指向的值不一样时,slow 往前挪一位,并将 fast 指向的值赋值给 slow。
 如果一样,就不挪。这样 slow 的那一排元素就是不重合的元素。
 
 PS:似乎算法这东西,很大概率都会和指针扯上关系。
 */
int removeDuplicates(int* nums, int numsSize) {
    if (numsSize == 0 || numsSize == 1) return numsSize;
    // 需要注意的是:这里的 i 是索引值,而不是个数。
    int i = 0;
    for (int j = 1; j < numsSize; j++) {
        if (nums[j] != nums[i]) {
            i += 1;
            nums[i] = nums[j];
        }
    }
    return i + 1;
}

Review

Core ML and Vision: Machine Learning in iOS 11 Tutorial

这是一篇 iOS 11 下如何进行机器学习的准入门文章。介绍了如何在 iOS 11 中离线环境下通过 Apple 提供的 CoreML 和 Vision 框架,用一个已经训练好的模型进行图片场景识别的例子。Apple 开发者网站提供了一些已经训练好的模型样本,可以直接拿来使用。

Tip

iOS 下,如何异步下载多张图片,当多张图片都下载完成之后得到响应。这里以 SDWebImage 框架下载图片为例。

dispatch_group_t group = dispatch_group_create();
// 有多张图片URL的数组
NSArray *imageURLArr = @[@"url1",@"url2",@"url3"];
for (NSString *imageUrlStr in imageURLArr) {
    
    dispatch_group_enter(group);    
    UIImageView *imageView = [UIImageView new];
    [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrlStr] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (error) {
            // 加载失败
        } else {
            // 加载成功
        }
        dispatch_group_leave(group);
    }];
}
// 下载图片完成后, 回到主线
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    // 刷新UI
});

以上例子是通过 group 的 enter 和 leave 的方式实现的,另外也可以通过 Dispatch Semaphore 来实现,可以试试。

需要注意的是:图片下载是异步执行的,所以 dispatch_group_leave 应该在下载操作结束之后的回调中执行。

Share

Designing iOS architecture: Motivation

这篇文章讲述了架构设计的重要性,和在设计前需要考虑的方方面面,并教你如何设计一个好的架构。要知道,不合理的架构会让代码变得丑陋,并且在以后会出现各种隐患,导致后期项目得难以扩展和维护。我们应该在设计架构时,既能够满足业务需求,也能够满足组织需求。在设计架构之前,要更多地自我提问,找出问题,解决问题。

你可能感兴趣的:(ARTS-2018.7.4)