其他分享
首页 > 其他分享> > 【ARTS】001-第一周(2020.05.25-2020.05.31)

【ARTS】001-第一周(2020.05.25-2020.05.31)

作者:互联网

一、Algorithm

1. 题目

  本周的题目是 001-Two Sum。给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。

  示例:给定 nums = [2, 7, 11, 15],target = 9。因为 nums[0] + nums[1] = 2 + 7 = 9,所以返回 [0, 1]。

2. 题解

 1 struct object
 2 {
 3     int val;
 4     int idx;
 5 };
 6 
 7 int compare(void *objA, void *objB)
 8 {
 9     return ((struct object *)objA)->val - ((struct object *)objB)->val;
10 }
11 
12 /**
13  * Note: The returned array must be malloced, assume caller calls free().
14  */
15 int* twoSum(int* nums, int numsSize, int target, int* returnSize)
16 {
17     int i, j;
18 
19     if (numsSize < 2)
20     {
21         printf("The size of nums is invalid, please check!\n");
22         return NULL;
23     }
24 
25     struct object *objs = malloc(sizeof(*objs) * numsSize);
26 
27     for (i = 0 ; i < numsSize ; i++)     // O(1)
28     {
29         objs[i].val = nums[i];
30         objs[i].idx = i;
31     }
32 
33     qsort(objs, numsSize, sizeof(*objs), compare);   // O(nlogn)
34 
35     int count = 0;
36     int *result = malloc(sizeof(int) * 2);
37     *returnSize = 2;
38 
39     i = 0;
40     j = numsSize - 1;
41 
42     while (i < j)   // O(n)
43     {
44         int diff = target - objs[i].val;
45         if (diff > objs[j].val)
46             while (++i < j && objs[i].val == objs[i - 1].val) {}
47         else if (diff < objs[j].val)
48             while (--j > i && objs[j].val == objs[j + 1].val) {}
49         else
50         {
51             result[0] = objs[i].idx;
52             result[1] = objs[j].idx;
53             return result;
54         }
55     }
56 
57     return NULL;
58 }

 

二、Review

  《C++ Primer》英文版第一章。

 

三、Tips

  在这周的工作中,测试同事反馈预装 Amazon 后,打开 Amazon 到登录界面,会出现 Google Service Failed 提示。经过排查,此问题和预装 Google Service 和 Google Play 时的默认权限有关。

  Android 6.1 之后,权限的操作发生了变化。由安装时申请,变为使用时需要用到某项权限再申请。但有些应用在需要用到权限时,并不会申请。这就会导致问题。

  对于预装应用,我们可以在 code/frameworks/base/services/core/java/com/android/server/pm/permission/DefaultPermissionGrantPolicy.java 的 grantDefaultSystemHandlerPermissions 方法中进行默认权限的配置。

 1 // gsf
 2 final String gsfPackageName = "com.google.android.gsf";
 3 PackageParser.Package gsfPackage = getSystemPackage(gsfPackageName);
 4 if (gsfPackage != null
 5     && doesPackageSupportRuntimePermissions(gsfPackage)) {
 6   grantRuntimePermissions(gsfPackage, PHONE_PERMISSIONS, userId);
 7   grantRuntimePermissions(gsfPackage, CONTACTS_PERMISSIONS, userId);
 8   grantRuntimePermissions(gsfPackage, LOCATION_PERMISSIONS, userId);
 9   grantRuntimePermissions(gsfPackage, SMS_PERMISSIONS, userId);
10   grantRuntimePermissions(gsfPackage, MICROPHONE_PERMISSIONS, userId);
11 }
12 
13 // gms
14 final String gmsPackageName = "com.google.android.gms";
15 PackageParser.Package gmsPackage = getSystemPackage(gmsPackageName);
16 if (gmsPackage != null
17     && doesPackageSupportRuntimePermissions(gmsPackage)) {
18   grantRuntimePermissions(gmsPackage, PHONE_PERMISSIONS, userId);
19   grantRuntimePermissions(gmsPackage, CONTACTS_PERMISSIONS, userId);
20   grantRuntimePermissions(gmsPackage, LOCATION_PERMISSIONS, userId);
21   grantRuntimePermissions(gmsPackage, SMS_PERMISSIONS, userId);
22   grantRuntimePermissions(gmsPackage, MICROPHONE_PERMISSIONS, userId);
23   grantRuntimePermissions(gmsPackage, STORAGE_PERMISSIONS, userId);
24   grantRuntimePermissions(gmsPackage, CALENDAR_PERMISSIONS, userId);
25 }
26 
27 // vending
28 final String vendingPackageName = "com.android.vending";
29 PackageParser.Package vendingPackage = getSystemPackage(vendingPackageName);
30 if (vendingPackage != null
31     && doesPackageSupportRuntimePermissions(vendingPackage)) {
32   grantRuntimePermissions(vendingPackage, PHONE_PERMISSIONS, userId);
33   grantRuntimePermissions(vendingPackage, CONTACTS_PERMISSIONS, userId);
34   grantRuntimePermissions(vendingPackage, LOCATION_PERMISSIONS, userId);
35   grantRuntimePermissions(vendingPackage, SMS_PERMISSIONS, userId);
36   grantRuntimePermissions(vendingPackage, MICROPHONE_PERMISSIONS, userId);
37 }

 

四、Share

  《努力就会成功》

标签:25,ARTS,grantRuntimePermissions,val,int,userId,2020.05,objs,PERMISSIONS
来源: https://www.cnblogs.com/murongmochen/p/12974955.html