Update 面试总结.md

添加小米校招2018笔试题第一题
This commit is contained in:
孙海洲 2018-09-20 21:05:06 +08:00 committed by GitHub
parent 5f2dfcc0d9
commit b3e1afed81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,6 @@
<!-- GFM-TOC --> <!-- GFM-TOC -->
* [1. 字符串组合](#1-字符串组合) * [1. 字符串组合](#1-字符串组合)
* [2. 实现 Singleton](#2-实现-singleton) * [2. 整数组合求和](#2-整数组合求和)
* [3. 数组中重复的数字](#3-数组中重复的数字) * [3. 数组中重复的数字](#3-数组中重复的数字)
* [参考文献](#参考文献) * [参考文献](#参考文献)
@ -64,10 +64,69 @@ ret_list = list(set(result)^set(result_same))
print len(ret_list) print len(ret_list)
``` ```
# 2. 整数组合求和
# 2. 实现 Singleton ## 题目描述
小米之家是成人糖果店。里面有很多便宜,好用,好玩的产品。中秋节到了,小米之家想给米粉准备一些固定金额大礼包。对于给定的一个金额,需要判断能不能用
不同种产品(一种产品在礼包最多出现一次)组合出来这个金额。聪明的你来帮帮米家的小伙伴吧。
### 输入
```code
输入NN是正整数 N < = 200
输入N个价格p正整数 p <= 10000用单空格分割
输入金额MM是正整数M <= 100000
```
### 输出
```code
能组合出来输出1
否则输出0
```
### 样例输入
```code
6
99 199 1999 10000 39 1499
10238
```
### 样例输出
```code
1
```
## 解题思路
本题采用一种比较直接的方式进行解题,分为如下步骤:
1. 对输入np进行排序方便后面快速结束。
2. 求解给定np时候的0~n个组合的和提供`Python`提供的`itertools.permutations`来实现。
3. 如果较小的数相加已经大于目标,可以提前跳出本次循环。
```python
import itertools
n = int(raw_input())
np = [int(k) for k in raw_input().split(" ")]
np.sort()
sum_np = int(raw_input())
flag = 0
for i in range(n):
for j in itertools.permutations(np, i):
if sum(j) > sum_np:
continue;
if sum(j) == sum_np:
flag = 1;
break;
print flag
```
[单例模式](https://github.com/CyC2018/Interview-Notebook/blob/master/notes/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F.md)
# 3. 数组中重复的数字 # 3. 数组中重复的数字