Leetcode 经验

String

遇到String 的题目,首先的想法是用指针i, j的方式做题

两种视角

Recursion:从顶端找底端

用Recursion的方式,如果会有重复的部分,新建一个Helper,存储所有见过的计算结果

EXAMPLE:62. Unique Paths

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution:
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
def helper(m,n,seen):
if (m,n) in seen: return seen[(m,n)]

if (m == 2 and n == 1) or (n==2 and m==1) or (m==1 and n ==1):
return 1

count = 0
if m-1 >= 1:
count += helper(m-1,n,seen)
seen[(m-1,n)] = count
if n-1 >=1:
temp = helper(m, n-1, seen)
seen[(m,n-1)] = temp
count += temp

return count

return helper(m,n,{})

Store in a list:从底端到顶端

如果从0/1开始,都要遍历,可以从底端到顶端

用list,存储从小到大的结果

返回值为最后的最大值

EXAMPLE:62. Unique Paths

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def uniquePaths2(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""

"""
坐标系
Finish: A[0,0]
Start: A[m-1,n-1]
"""

# 预先设定其他的都是[1],那么A[i][0], A[0][j],就不需要进行遍历
A = [[1] * n] * m

# 直接从A[1][1]开始
for i in range(1, m):
for j in range(1, n):
A[i][j] = A[i - 1][j] + A[i][j - 1]

return A[m - 1][n - 1]

Palindromic Substring

EX: LC5: Longest Palindromic Substring

1
2
3
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
  1. Select a center, like “a”, “aa”, “aaa”
  2. Spread the center
  3. Use Middle to remeber the center, and use i, j to spread it.

Establish a django project

After Django Installed

1. Activate the virtual environment

1
$ source activate myDjangoEnv

2. Init the project

1
$ django-admin startproject first_project

Default Files

Screen Shot 2017-01-28 at 12.48.16 AM.png

  • __init__.py

    This is a blank Python script that due to its special name let’s Python know that this directory can be treated as a package

  • settings.py

    This is where you will store all your project settings

  • urls.py

    This is a Python script that will store all the URL patterns for your project. Basically the different pages of your web application.

  • wsgi.py

    This is a Python script that acts as the Web Server Gateway Interface. It will later on help us deploy our web app to production

  • manage.py

    This is a Python script that we will use a lot. It will be associates with many commands as we build our web app!

3. Run server to test

1
$ python manage.py runserver

Use the given url, you can see it.

3. Start Application

1
$ python manage.py startapp first_app

Build a html page

1. Build Connections

a. Establish your html files

https://getbootstrap.com/docs/4.1/getting-started/introduction/

1
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
1
<link rel="stylesheet" href="/css/master.css">
1
<script src="MyScript.js"></script>

HTML Basics

学习笔记:

https://github.com/yukiii-zhong/Udemy_Django_Course/tree/master/HTML_LEVEL_ONE

https://github.com/yukiii-zhong/Udemy_Django_Course/tree/master/HTML_LEVEL_TWO

1. Comment

1
<!-- This is a comment -->

Shortcut: COMMAND+/

2. 基本格式

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>This is the Title!</title>
</head>
<body>
<!-- This is a comment -->

</body>
</html>

<head> Part don’t actually show in the page, it’s just the head

<body> it’s the real shown part.

3. 查看效果(In Atom)

  1. Right Click the htmlfile
  2. Select copy full path
  3. Run the copied path in the Browser

4. 查询资源

  • W3school,
  • Mozilla - More Detailed

Forking Repo

1. Fork the Repo

1.1 fork the Repo to your own.

image-20180712003555025

1.2 Clone it from your own.

1
$ git clone https://github.com/yukiii-zhong/lighthouse.git

2. Filtering Collaborator’s Commits

2.1 Group By Commit Author

a. common

1
$ git shortlog

img

b. only see number of commits

1
$ git shortlog -s -n

-s –> just show the number of commits

-n –> sort them numerically

img

c. only show specific author commits

1
$ git shortlog --author=Paul
1
2
$ git log --grep=bug
$ git log --grep bug

if you were to run git log --grep "fort", then Git will display only the commits that have the character f followed by the character o followed by r followed by t.

93. Restore IP Addresses

前提规则

  1. IP地址由32位二进制数组成,为便于使用,常以XXX.XXX.XXX.XXX形式表现,每组XXX代表小于或等于255的10进制数。所以说IP地址总共有四段,每一段可能有一位,两位或者三位,范围是[0, 255]
  2. 当只有一位时,0可以成某一段,如果有两位或三位时,像 00, 01, 001, 011, 000等都是不合法的

思路

  1. Split String according to length combinations
  2. Judge whether each split is valid
  3. Return valid res.

Git: Working with Remotes

1
$ git remote

PartA. Add A Remote Repository

1. Create A Simple Project

  1. Create a Project directory

  2. git init to turn it into a Git repository

  3. Create a README.md file

  4. Create other files

2. Hosting on GitHub

Creat the repo on GitHub

3. Manage the Repository: git remote

1
$ git remote
  • It’s possible to have links to multiple different remote repositories.
  • A shortname is the name that’s used to refer to a remote repository’s location. Typically the location is a URL, but it could be a file path on the same computer.
  • git remote add is used to add a connection to a new remote repository.
  • git remote -v is used to see the details about a connection to a remote.

PartB. Push Changes To A Remote

1. Check the commits

1
$ git log --oneline --graph --all

2. Push the commits

1
2
3
4
$ git push <remote-shortname> <branch>

# EX:
$ git push origin master

PartC: Pulling Changes From A Remote

1
$ git pull origin master

When git pull is run, the following things happen:

  • the commit(s) on the remote branch are copied to the local repository
  • the local tracking branch (origin/master) is moved to point to the most recent commit
  • the local tracking branch (origin/master) is merged into the local branch (master)

PartD: Pull vs Fetch

1
$ git fetch origin master

fetch the changes, but not merge them