[1] Two Sum
Easy array hash-table
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
解答 本题可以采用常规的方法和基于 hashtab 的方式进行处理(题目描述中已经定义不存在重复的数据)
result := make([]int, 2)
for i := 0; i < len(nums); i++ {
var item = nums[i]
var nextItem = target - item
for j := i + 1; j < len(nums); j++ {
if nextItem == nums[j] {
result[0] = i
result[1] = j
break
}
}
}
return result
vMap := make(map[int]int)
for index, item := range nums {
if _, ok := vMap[target-item]; ok {
return []int{vMap[target-item], index}
}
vMap[item] = index
}
return []int{}
本周阅读的一遍文章是:如何给 6 岁的小孩解释计算机 Explaining Programming to 6 Years Old Kids
- Keep them engaged by asking questions and drawing
- Build upon what they already know
tips & tricks
- It was more fun than I expected! I treated this seriously and I was well prepared, and it paid off.
- Drawing pictures is the right way to go.
- Be prepared for repeating things over and over again.
- Some kids will get bored no matter what you do. Their attention span is simply too short.
- Write carefully
- During the presentation I understood that there is no point in correcting them
- The presentation took me ~25 minutes.
本周在项目实施过程中遇到前端校验cookie的一个问题,开始一直没太注意,后来反复出现了几次才定位这个问题。实际情况是:
- 应用A和应用B是同一个二级域名下的2个不同三级级域名,比如: a.xx.com, b.xx.com
- A应用写的cookie绑定的path为a.xx.com,但是B应用绑定的path为*.bb.com, 同时都设置了HttpOnly
- 当登录B应用后,cookie写入浏览器。A应用再次登录时,B应用登录成功后将cookie写入浏览器后,但是后续所有请求带的token去后端校验都会失效,认为是非法token
其中有3部分知识点
- Cookie基础:同源策略(SOP)
* Web SOP:[protocol, domain, port]
http://www.bank.com
http://www.bank.com:8080
https://www.bank.com
总结:非同源(受SOP隔离保护)
* Cookie SOP:[domain, path]
-- 仅以domain/path作为同源限制
-- 不区分端口
-- 不区分HTTP/HTTPS
- Cookie基础:Domain向上通配
* 在对Cooke读写时,以“通配”的方式判断Domain是否有效
-- 写入:
当页面为http://www.bank.com时
Set-Cookie: user1=aaa; domain=.bank.com; path=/; [有效]
Set-Cookie: user2=bbb; domain=www.bank.com; path=/; [有效]
Set-Cookie: user3=ccc; domain=.www.bank.com; path=/; [有效]
Set-Cookie: user4=ddd; domain=other.bank.com; path=/;[无效]
-- 读取:
访问http://www.bank.com
Cookie: user1=aaa; user2=bbb; user3=ccc;
访问http://user.bank.com
Cooke: user1=aaa;
- Cookie基础:Path向下通配
Set-Cookie: session=bob; domain=.bank.com; path=/;
Set-Cookie: cart=books; domain=.bank.com; path=/buy/;
http://bank.com/ => Cookie: session=bob
http://bank.com/buy/ => Cookie: session=bob; cart=books
这篇文章对公司的现状及自理提供了一个真实的案例场景,具有很强的参考价值。需要再次细读
System Design 记录作者有关架构面试的一些知识整理文章,非常有参考价值。 比如第一篇Deep Dive Into System Design 面试者系统架构方面需要考察的点:
- Examine relevancy: Does the candidate have relevant working experience? Especially for critical problems in my projects.
- Examine potential: How deep the candidate understand about the concepts in our domains, and also the painpoints.
- Examine skills: Is he/she able to bring up a qualified architecture design with given constraints by discussing together.
Resources To Learn System Design: