ARTS #011

ARTS #011

Posted by Dan on October 19, 2018

ARTS是由左耳朵耗子--陈皓发起的一个活动: 每周至少做一个leetcode的算法题、阅读并点评至少一篇英文技术文章、学习至少一个技术技巧、分享一篇有观点和思考的文章。(也就是Algorithm、Review、Tip、Share简称ARTS),至少坚持一年。

ARTS 011

这是第11篇

Algorihm 算法题

leetcode算法第241题.Reorganize String: 难度:中等


Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *.

Example 1:
Input: "2-1-1"
Output: [0, 2]
Explanation: 
((2-1)-1) = 0 
(2-(1-1)) = 2

Example 2:
Input: "2*3-4*5"
Output: [-34, -14, -10, -10, 10]
Explanation: 
(2*(3-(4*5))) = -34 
((2*3)-(4*5)) = -14 
((2*(3-4))*5) = -10 
(2*((3-4)*5)) = -10 
(((2*3)-4)*5) = 10

分治法,使用递归,代码如下,leetcode的运行时间是0ms:


int*  diffWaysToCompute1(int* str,int min,int max) {
    
    if(min == max){
        int *charCount = (int*)malloc(sizeof(int) * 2);
        charCount[0] = 2;
        charCount[1] = str[min];
        return charCount;
    }
    
  
    if (max - min == 2) {//if 里面的这个可以去掉,只是为了加快运算
        int minNum = str[min] ;
        int maxNum = str[max] ;
        int mid = str[min +1];
        int num = 0;
        if (mid==-42) {//*
            num = minNum * maxNum;
        }
        else if(mid==-43){//+
            num = minNum + maxNum;
        }
        else if(mid==-45){//-
            num = minNum - maxNum;
        }
        int* charCount = (int*)malloc(sizeof(int) * 2);
        charCount[0] = 2;
        charCount[1] = num;
        return charCount;
    }
    else
    {
        int *charCount = NULL;//
        for (int mid = min; mid < max; mid +=2) {
            int *nums_1 = diffWaysToCompute1(str,min,mid);
            int *nums_2 =  diffWaysToCompute1(str,mid+2,max);
            
            int nums_1_count = nums_1[0] - 1;
            int nums_2_count = nums_2[0] - 1;
            int index = 1;
            if (charCount == NULL) {
                charCount =(int*)malloc(sizeof(int) * (nums_1_count * nums_2_count +1));
                 charCount[0] = nums_1_count * nums_2_count + 1;
            }
            else{
                charCount=(int*)realloc(charCount,sizeof(int)*(charCount[0]+ nums_1_count * nums_2_count));
                index = charCount[0];
                charCount[0] = nums_1_count * nums_2_count + charCount[0];
                
            }
        
            int op = str[mid + 1];
            for (int i= 1; i <= nums_1_count; i++) {
                int left = nums_1[i];
                for (int j= 1; j <= nums_2_count; j++) {
                    int right = nums_2[j];
                    int num = 0;
                    if (op==-42) {//*
                        num = left * right;
                    }
                    else if(op==-43){//+
                        num = left + right;
                    }
                    else if(op==-45){//-
                        num = left - right;
                    }
                    
                    charCount[index++] = num;
                }
            }
            free(nums_1);
            free(nums_2);
        }
        return charCount;
    }
    return NULL;
}

//因为存在连续数字字符的情况,比如 "109-23-9",所以先把字符串转为数组
int* diffWaysToCompute(char* input, int* returnSize) {
    //计算运算符的个数 用来计算要需要分配的内存空间,比如比如 "109-23-9",有两个运算符,需要分配的内存空间是5个
    int operatorCount = 0;
    for(int i = 0; i < strlen(input); i++){
        if (input[i]==42 || input[i]==43 || input[i]==45){
            operatorCount++;
        }
    }
    
    int *inputToIntArray = (int*)malloc(sizeof(int) * (2*operatorCount +1));//分配数组空间
    int index = 0;
    int data = 0;
    for(int i = 0; i < strlen(input); i++){
        char dataChar = input[i];
             //42 * , 43 + , 45 -,因为输入中不含负数,为了区分运算符和数字,把运算符用负数表示,
        if (dataChar == 42 || dataChar == 43 || dataChar == 45){
            inputToIntArray[index++] = data;
            inputToIntArray[index++] = -dataChar;
            data = 0;
            continue;
        }
        data = data *10 + (dataChar - 48);
    }
    inputToIntArray[index] = data;

    int* charCount = diffWaysToCompute1(inputToIntArray, 0, index);
    free(inputToIntArray);
  
    *returnSize = charCount[0] -1;
    int *ffcharCount =  charCount +1;
    return ffcharCount;
}

去掉if的代码也可以


int*  diffWaysToCompute1(int* str,int min,int max) {
    
    if(min == max){
        int *charCount = (int*)malloc(sizeof(int) * 2);
        charCount[0] = 2;
        charCount[1] = str[min];
        return charCount;
    }
    
 
    int *charCount = NULL;//
        for (int mid = min; mid < max; mid +=2) {
            int *nums_1 = diffWaysToCompute1(str,min,mid);
            int *nums_2 =  diffWaysToCompute1(str,mid+2,max);
            
            int nums_1_count = nums_1[0] - 1;
            int nums_2_count = nums_2[0] - 1;
            int index = 1;
            if (charCount == NULL) {
                charCount =(int*)malloc(sizeof(int) * (nums_1_count * nums_2_count +1));
                 charCount[0] = nums_1_count * nums_2_count + 1;
            }
            else{
                charCount=(int*)realloc(charCount,sizeof(int)*(charCount[0]+ nums_1_count * nums_2_count));
                index = charCount[0];
                charCount[0] = nums_1_count * nums_2_count + charCount[0];
                
            }
        
            int op = str[mid + 1];
            for (int i= 1; i <= nums_1_count; i++) {
                int left = nums_1[i];
                for (int j= 1; j <= nums_2_count; j++) {
                    int right = nums_2[j];
                    int num = 0;
                    if (op==-42) {//*
                        num = left * right;
                    }
                    else if(op==-43){//+
                        num = left + right;
                    }
                    else if(op==-45){//-
                        num = left - right;
                    }
                    
                    charCount[index++] = num;
                }
            }
            free(nums_1);
            free(nums_2);
        }
        return charCount;

}

//因为存在连续数字字符的情况,比如 "109-23-9",所以先把字符串转为数组
int* diffWaysToCompute(char* input, int* returnSize) {
    //计算运算符的个数 用来计算要需要分配的内存空间,比如比如 "109-23-9",有两个运算符,需要分配的内存空间是5个
    int operatorCount = 0;
    for(int i = 0; i < strlen(input); i++){
        if (input[i]==42 || input[i]==43 || input[i]==45){
            operatorCount++;
        }
    }
    
    int *inputToIntArray = (int*)malloc(sizeof(int) * (2*operatorCount +1));//分配数组空间
    int index = 0;
    int data = 0;
    for(int i = 0; i < strlen(input); i++){
        char dataChar = input[i];
             //42 * , 43 + , 45 -,因为输入中不含负数,为了区分运算符和数字,把运算符用负数表示,
        if (dataChar == 42 || dataChar == 43 || dataChar == 45){
            inputToIntArray[index++] = data;
            inputToIntArray[index++] = -dataChar;
            data = 0;
            continue;
        }
        data = data *10 + (dataChar - 48);
    }
    inputToIntArray[index] = data;

    int* charCount = diffWaysToCompute1(inputToIntArray, 0, index);
    free(inputToIntArray);
  
    *returnSize = charCount[0] -1;
    int *ffcharCount =  charCount +1;
    return ffcharCount;
}

Review

####文章1 : 谷歌发布新款手机、平板电报以及智能家居控制助手 Google has launched the latest versions of its Pixel smartphone, as well as a new tablet computer and a smart home controller. 谷歌已经推出其最新款的Pixel智能手机,还有新款平板电脑和智能家居控制助手。

The new products were announced during an event Tuesday in New York. 星期二,谷歌在纽约的发布会中发布了这些新产品。

Google launched the first Pixel smartphone two years ago as part of a major new effort to develop hardware. 作为硬件开发的重大举措之一,谷歌于两年前发布了第一款pixel智能手机。

The company is better known for building software to power internet search technology and the Android smartphone operating system. 该公司因开发了支持网络搜索技术的软件和安卓智能手机操作系统而闻名。

Over the past two years, Google has sold about 7 million Pixel phones, the technology research company IDC estimates. 据科技研究公司IDC估计,在过去的两年时间,谷歌已经售出约七百万台Pixel智能手机。

Those sales are a very small part of the estimated 3.6 billion phones sold by all manufacturers during that same two-year period. 这些销量只占同期所有制造商估计销售36亿台手机中的小部分。

Apple, for example, sold about 388 million iPhones during the two years. 例如,苹果公司这两年时间里售出了约3.66亿台IPhone手机。

Google’s new smartphones - the Pixel 3 and Pixel 3 XL - appear aimed at providing iPhone-like features at a lower price. 谷歌的新款智能手机Pixel 3和Pixel 3 XL似乎是旨在以更低价格提供类似iPhone的手机功能。

The Pixel 3 will be available October 18 in the United States for $799. 智能手机Pixel将会于10月18日在美国开售,售价仅为799美元。

The larger Pixel 3 XL will cost $899. 尺寸更大的Pixel 3 XL智能手机售价为899美元。

This compares to the iPhone Xs, which sells for $999, and the iPhone Xs Max, priced at $1099. 相比之下,IPhoneXs的售价为999美元,IPhone Xs Max的售价为1099美元。

Both new Pixels are also being released in 12 other countries, including Japan, Singapore and India. 这两款新的Pixel系列手机将会在其他的12个国家销售,包括日本,新加坡和印度。

During Tuesday’s launch event, Google officials demonstrated new features and improvements to the Pixel 3. 在周二的发布会上,谷歌官方展示Pixel 3的新功能和改进之处。

At times, they made direct comparisons to iPhones. 他们有时还直接将其与iPhone比较。

Google promises better camera performance in its Pixel 3 devices. 谷歌承诺在其Pixel 3设备中提供更好的相机功能。

A new tool is designed to use machine learning software to produce better low-light and close-up shots. 这款新设备旨在用人工智能软件来提供更好的低光和特写镜头。

The tool works by combining many shots quickly taken one after the other. 该设备通过多个镜头一个接一个快速组合起来以实现拍摄效果。

The camera also uses machine learning to examine the many photos it takes in an effort to find and suggest the best ones 该相机同样利用人工智能软件来检查拍摄的多张照片,可以找出或推荐拍得最好的一张。

Pixel 3 phones were also built with two camera lenses in front, which Google demonstrated as a helpful tool when taking selfies with large groups. 智能手机Pixel 3同样配备了两个前置摄像头,并且谷歌证明了其是拍摄大型团体照的有用工具。

The phone is also able to answer itself if the user cannot or does not want to pick up. 如果用户无法或不想接听电话时,这款手机同样可以自动应答。

If a call comes in, the user can touch the screen to have the phone answer and ask who is calling. 如果有电话打入,用户可以触碰屏幕让手机自动应答是谁打来的电话。

The answer from the person placing the call is then put into a text message and shared with the user in real time. 拨打电话者的回应会转换成文字信息,实现与用户的实时共享。

Google Product Manager Liza Ma said the feature puts the user in complete control of the phone. 谷歌产品经理丽莎·玛称,这一功能可以让用户完全控制手机。

“You can decide whether to pick up, send a quick reply, or mark the call spam. You’ll never have to talk to another telemarketer.” 她说:”你可以决定是否接听电话,发送快速回复信息,或者是将其标记为骚扰电话。你将不用和电话推销员通话。”

As with the earlier models, the new Pixels center heavily on the company’s search engine and other products. 与早前的型号一样,新款Pixel手机重点放在谷歌公司的搜索引擎和其他产品。

These include maps, Google Assistant and YouTube video service. 包括地图、谷歌助手和YouTube视频服务。

Google also introduced its new Home Hub, an internet-connected smart speaker and home controller with a small screen. 谷歌同样推出其新款其智能家居助手Home Hub,该设备是一款配备了小型屏幕的智能联网音箱以及家居智能控制设备。

The device is similar to Amazon’s Echo Show and Facebook’s new Portal. 这款设备类似于亚马逊公司的Echo Show和脸书最新的Portal。

The company said Home Hub is designed to be a central controller for many devices throughout the home, such as lights, outdoor cameras, temperature controls and televisions. 谷歌公司称,智能家居助手Home Hub旨在成为家庭众多设备的控制器,例如电灯,户外摄像机,温度控制器和电视。

Like similar devices, it can be activated by voice to play music and look up information on the internet. 与类似的设备一样,它可以通过声控来播放音乐,并且在网络寻找信息。

Home Hub will cost $149 when it goes on sale later this month in the United States, Britain and Australia. 智能家居助手Home Hub将会于本月晚些时候在美国,英国和澳大利亚开售,届时售价为149元。

This compares to the new version of Amazon’s Echo Show, which sells for $229. The cost of the Facebook Portal starts at $199. 相比之下,亚马逊公司最新款的Echo Show售价为229美元,而脸书推出的Portal售价为199美元。

Google also announced it would launch a new computer tablet later this year called the Pixel Slate. 谷歌还宣布今年晚些时候,将会发布一款名为Pixel Slate的新款平板电脑。

The company says the device will be powered by its own newly designed Chrome OS system. It appears aimed at competing with Apple’s iPad Pro. 该公司称设备将会由其自行设计的Chrome OS系统提供技术支持,其似乎旨在与苹果的Ipad Pro竞争市场。

The Slate will run Android phone apps, but Google says it offers performance closer to a desktop computer. 平板电脑Slate将会运行安卓手机软件,但是谷歌公司称这款设备可以提供接近台式电脑的性能。

It is priced at $599. An exact release date has not yet been announced. 这款平板电脑售价为599美元,其具体的发布日期还没有公布。

####文章2 :

2016 哈佛毕业演讲台的第一位中国学生何江 When I was in middle school, a poisonous spider bit my right hand.

我在上中学的时候,被一只有毒的蜘蛛咬了右手。 I ran to my mom for help, but instead of taking me to a doctor, my Mom set my hand on fire.

我去向母亲求救,但是她没有找医生,而是用火来烤我的手。 After rubbing my hand with several layers of cotton then soaked in wine, she put a chopstick into my mouth and ignited the cotton.

她用酒浸过的棉纱绕住我的手。缠了好几层之后,让我嘴里咬住一根筷子,然后点燃了棉纱。 Heat quickly penetrated the cotton and began to roast my hand.

热量快速穿透棉花,开始炙烤我的右手。 The searing pain made me want to scream but the chopstick prevented it.

这股灼痛让我想要大叫,不过我嘴里咬着的筷子让我叫不出来。 All I could do was watch my hand burn, one minute, then two minutes, until my mom put out the fire.

我当时唯一能做的就是看着我着火的手,一分钟过去了,两分钟过去了,直到母亲把火熄灭。 You see the part of China I grew up in was a rural village, and at that time, pre-industrial.

如你所见,我是在中国的一个小山村里成长的,在那个时候那里还没有实现工业化。 When I was born, my village had no cars, no telephones, no electricity, not even running water and we certainly didn’t had access to the modern medical resources.

在我出生的那个年代,我们村没车、没电话、也没电,连自来水都没有,更没法获得现代化的医疗资源。 There was no doctor my mom could bring me to see about this spider bite.

当我被蜘蛛咬伤时,根本没有我母亲可以带我去看的医生。 For those who study Biology, you may have brought the science behind my mom’s cure: heat deactivates proteins and the spider’s venom is simply a form of protein.

对于学生物学的人来说,你也许已经知道了我母亲这种治疗方式的科学依据:热量能够让蛋白质失去活性,而蜘蛛的毒液只是蛋白质组成的。 It’s cool how that folk remedy actually incorporates the base of biochemistry, isn’t it?

土办法里竟然也包含着现代生物化学的基础原理,想想也挺酷的,不是吗? But I am a Ph. D student in Biochemistry at Harvard, I now know that better, less painful and less risky treatment existed.

但是作为一个在哈佛学习生物化学的博士,我现在知道了其实有更好的、不那么痛、危险系数更小的治疗方法。 So, I can’t help but ask myself, why I did’t receive one at that time?

所以我忍不住问我自己,为什么那个时候我没有用这样的方法来治疗呢? Fifteen years have passed since that incident, I am happy to report that my hand is fine.

这件事已经过去十五年了,我很高兴地告诉你们:我的手现在恢复地很好。 But this question lingers and I continued to be troubled by the unequal distribution of scientific knowledge throughout the world.

但这个问题仍旧萦绕着我,我依旧会被世界上科学知识的分布不均衡问题所困扰。 We have learned to edit the human genome and unlock many secrets of how cancer progress.

我们已经学会编辑人类基因组,揭开了许多癌症形成的秘密。 We can manipulate neuron activity literally with the switch of light.

我们甚至可以通过光束来操控大脑神经元的活动。 Each year brings more advances in Biomedical research, exciting transformative accomplishments. 每一年生物医学领域都有巨大进步和让人兴奋的、颠覆性的伟大成就。

Yet, despite the knowledge we have amassed, we haven’t been so successful in deploying it to where it’s needed most. 然而,尽管人类已经掌握和积累了大量知识,但是我们还是没能很好地把它们运用在最需要的地方。

According to the World Bank, 12% of the world population lives on less than 2 dollars a day. 据世界银行统计,目前世界仍有12%的人一天的生活费不到2美元。

Malnutrition kills more than 3 million children annually. 每年超过三百万的儿童死于营养不良。

Three hundred million people are afflicted by Malaria globally. 全球有三亿人正受疟疾折磨。

All over the world, we constantly see these problems of poverty, illness, and lack of resources impeding the flow of scientific information. 在世界各地,我们经常看到贫穷、疾病和资源短缺这些问题,这些都阻碍了科学知识的传播。

Life-saving knowledge we take for granted in our modern world is often unavailable in the underdeveloped regions. 现代社会里那些习以为常的救生知识在不发达地区通常得不到普及。

And so, in far too many places, people are still essentially trying to cure a spider bite with fire. 于是,在世界上很多地区,人们仍然用火疗的方法来处理蜘蛛咬伤事件。

While studying at Harvard, I saw how scientific knowledge can help others in simple, yet profound ways. 在哈佛学习期间,我知道了科学知识是如何用简单的又深刻的方式帮助到其他人的。

The bird flu pandemic in the 2000s looked to my village like a spell cast by demons. 在2000年的时候,禽流感肆虐,对我们村而言,这种病就像是魔鬼的诅咒。

Our folk medicine didn’t even have half-measures to offer. 在我们的民间治疗中根本找不到解决办法。

What’s more, farmers didn’t know the difference between the common cold and flu. 更严重的是,农民们不知道普通的感冒和流感之间的区别。

They did not understand that the flu was much more lethal than common cold. 他们不明白流感比普通的感冒要致命地多。

Most were also unaware that the virus could transmit across different animal species. 而且,大部分人根本不了解流感病毒可以在不同物种间传播。

So when I realized that simple hygiene practices like separating different animal species could help to contain the spread of the disease, 所以当我了解了简单有效的卫生措施,比如隔离不同的物种可以遏制疾病的传播,

and that I could help make this knowledge available to my village. 并把这样的信息告诉我的村民。

That was my first aha moment as a bioscientist. 我的心里第一次有了作为生物科学家的成就感。

But it was more than that: it was also a vital inflection point of my own ethical development, my own self-understanding as a member of the global community. 但不仅仅如此:这是我个人道德理念的一个重要转折,也自知作为地球上一员的自我使命。

Harvard dares us to dream big, to aspire, to change the world. 哈佛教导我们敢于拥有伟大梦想,勇往直前,去改变这个世界。

Here on this Commencement Day, we are probably thinking of grand destinations and big adventures that await us. 今天在这个毕业典礼上,大家可能正幻想着等待我们的伟大理想和征途。

As for me, I am also thinking of the farmers in my village. 于我,我仍在思考着我家乡的农民们的命运。

My experience here reminds me how important it is for researchers to communicate our knowledges, to those who need it. 我的经历提醒着我,学者将知识传授给需要的人是多么重要。

Because by using the science we already have, we could probably bring my village and thousands like it into the world you and I take for granted every day. 因为通过使用我们已经掌握的科学,我们可以把我家乡的村民和成千上万类似的人带入到你我熟知的世界。

And that’s an impact every one of us can make! 这是我们每一个人都可以发挥的影响!

But the question is, will we make the effort, or not? 但问题是,我们会这样去做吗?

More than ever before, our society emphasized science and innovation, but an equally important emphasis should be on distributing the knowledge we have to those who needed. 今天的社会比以往任何一个时代都强调科学和创新的重要性。但同样重要的是,把人类已掌握的知识传播到特别需要它们的地方。

Changing the world doesn’t mean everyone has to find the next big thing. 改变世界并不意味着每个人都要找到下一个巨大突破。

It can be as simple as becoming better communicators and finding more creative ways to pass on the knowledge we have, to people like my mom and farmers in the local community. 改变世界可以很简单:成为一个更好的沟通者,用有效的方法把我们掌握的知识传递给像我母亲以及村里其他的村民。

Our society also needs to recognize that the equal distribution of knowledge is a pivotal step of human development and we will work to bring this into a reality. 我们这个社会也需要认识到知识的均衡传播是人类发展的关键环节,并应该努力让它成为现实。

And if we do that, then perhaps a teenager in rural China who is bitten by a poisonous spider will no longer have to burn his hand, but will know to seek a doctor instead. 如果我们能够做到这些,那么一个被毒蜘蛛咬伤的中国农村少年将不会忍受“火疗”,而会得到医生的专业治疗。

Thank you! 谢谢!

TIPS

如何高性能给UIView加圆角,一般用下面的方法,

  UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(r, r)];
  CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
 maskLayer.frame = rect;
  maskLayer.path = maskPath.CGPath;
  
  view.layer.mask =   maskLayer
            

也就设置 view.layer.mask,但是使用上面的方法给tableViewCell的子视图加mask的时候,如下代码,当cell的高度不是整数的时候比如30.23,tableViewCell的边缘会出现虚的情况,目前还没找到解决办法。

UIVIew *bgView = [UIVIew  new];
bgView.frame = tableViewCell.bounds;
[tableViewCell.contentView addSubview:bgView];

bgView.layer.mask =  maskLayer;

Share:

今天一个APP活了,就是计算2018剩余多少天的一个APP,本来是免费的,后来由于一个抖音公众号的推荐,下载量很大,作者把APP变为收费了,价格为3块,听说作者一天的收入应该有几万,流量真是厉害啊。

APP赚钱模式: 1 付费下载 2 免费下载 功能解锁收费 3 免费下载 免费试用 广告收费 4 免费下载,功能免费订阅试用,这个很坑,很多用户不知道怎么取消订阅或者取消很麻烦

看到别人做APP自己心里也很痒痒,也想做出自己的APP,但是没有好的点子。 目前的工作: 1 工作 2 学习算法、英语、arts 3 看书 深入理解计算机系统 csapp, sicp最好看英文 4 提升iOS技能,日志、图片压缩、混淆,检测内存泄露等