简述

开始学习Python的Django框架之后,马上就被这种开发方式吸引了,感觉十分的快以及简洁,就把自己的学习过程记录下来吧.
主要的学习方式就是参考官方文档(我安装的是最新的1.11版本),先跟着官方的Writing your first Django app来走一遍流程,然后再看详细的文档吧.

开始

安装Django

直接在pip下安装Django模块是最快的方式了.

pip install Django

或者也可以在官网查看下载网页,从源码安装也是一种方式.下载完成解压之后,执行python setup.py install就可以安装成功.

检查是否安装成功

进入Python的终端,输入import django,没有报错的话就是已经安装成功了.

Django的基本命令

新建project

django-admin.py startproject project_name

新建app

app就是一个项目中的应用了,一个project可以有很多的app组成.
**注意:**要先进入project目录下才可以进行app的新建

cd project_name
django-admin.py startapp app_name

启动服务器

python manage.py runserver

在命令行看到以下的消息就说明服务器已经启动:

1
2
3
4
5
6
7
8
9
10
Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.Run 'python manage.py migrate' to apply them.

July 19, 2017 - 15:50:53
Django version 1.11, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

结束

第一部分的基础就到这里,之后的更新就会按照Django官方的Writing your first Django app的部分来自己实践了.

参考博客

Django 基础教程 非常推荐!!~ 很不错的中文教程

Documentation-Quick install guide 官方的安装教程

题目及理解

题目链接
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.

Example 1:

1
2
3
Input: [1,4,3,2]
Output: 4
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).

Note:

  1. n is a positive integer, which is in the range of [1, 10000].
  2. All the integers in the array will be in the range of [-10000, 10000].

理解

题目很简单,就是对每两个数进行min操作,然后全部相加得到的数要最大.直接对数组进行排序,然后按顺序两两取最小(循环+2)然后相加即可.

代码

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int arrayPairSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
int maximum = 0;
for(int i = 0; i < nums.size(); i+=2){
maximum += nums[i];
}
return maximum;
}
};

题目及理解

题目链接
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.

You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.

Example 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Input: 	
Tree 1 Tree 2
1 2
/ \ / \
3 2 1 3
/ \ \
5 4 7
Output: Merged tree:
3
/ \
4 5
/ \ \
5 4 7

**Note:** The merging process must start from the root nodes of both trees.

读题

简单题目,就是把两棵二叉树的各个结点加起来生成一棵新的树.只要遍历每个结点然后将值相加就好.

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/** 
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
TreeNode* t3;
if(t1 == NULL)
return t2;
if(t2 == NULL)
return t1;
t3 = new TreeNode(t1->val+t2->val);
t3->left = mergeTrees(t1->left,t2->left);
t3->right = mergeTrees(t1->right,t2->right);
return t3;
}
};
0%