HelloDavid


  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

  • 搜索

Leetcode-Count and Say

发表于 2019-06-07 | 分类于 leetcode

Count and Say

leetcode关于这题的说明比较含糊:
The count-and-say sequence is the sequence of integers with the first five terms as following:

  1. 1
  2. 11
  3. 21
  4. 1211
  5. 111221

1 is read off as “one 1” or 11.
11 is read off as “two 1s” or 21.
21 is read off as “one 2, then one 1” or 1211.
Given an integer n, generate the nth term of the count-and-say sequence.

题意是n=1时输出字符串1;n=2时,数上次字符串中的数值个数,因为上次字符串有1个1,所以输出11;n=3时,由于上次字符是11,有2个1,所以输出21;n=4时,由于上次字符串是21,有1个2和1个1,所以输出1211。依次类推,写个countAndSay(n)函数返回字符串。

解题思路:
采用动态规划的方式。当n=1时,直接返回‘1’。自底向上,然后根据n=index时返回的字符串计算出n=index+1时的字符串,并保存到数组curString中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def countAndSay(self, n):
if n==1:
return '1'

count = 1
index = 1
curString = ['', '1']
while index != n:
result = ''
cs = curString[index] + '*' # 加*是为了下面计算方便
for i in range(len(cs)-1):
if cs[i]==cs[i+1]:
count += 1
else:
result += str(count) + cs[i] # 加*的目的是为了方便处理最后一个字符
count = 1
index += 1
curString.append(result)

return curString[n]

采用正则表达:
leetcode上提供的另一种解题思路,尽管运行比较慢。
用作者的解释即: (.) captures one character and \1* covers its repetitions.
其中\1代表后向引用,表示表达式中,从左往右数,第一个左括号对应的括号内的内容。以此类推,\2表示第二个,\0表示整个表达式。

1
2
3
4
5
def countAndSay(self, n):
s = '1'
for _ in range(n - 1):
s = re.sub(r'(.)\1*', lambda m: str(len(m.group(0))) + m.group(1), s)
return s

Leetcode-Climbing Stairs

发表于 2019-06-07 | 分类于 leetcode

Climbing Stairs

爬梯子问题。给定一个n级台阶,每次可以走一个台阶或者两个台阶,一共有多少种走法?
Description

解题思路:
很常见的一种递推题型,要求n级台阶的走法,即可以分解为求n-1级台阶加上n-2级台阶的走法,climbNum[n]=climbNum[n-1]+climbNum[n-2]。所以问题实质上就是求解斐波那契数列。但由于采用递推方式会产生大量重复的计算,因此使用动态规划自底向上的进行计算,其中我们使用一个数组用于保存每步产生的结果。

1
2
3
4
5
6
7
8
9
10
11
12
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
if n<=2:
return n
climbNum = [1,1,2]
for i in range(3,n+1):
climbNum.append(climbNum[i-1] + climbNum[i-2])
return climbNum[n]

Leetcode-Best Time to Buy and Sell Stock

发表于 2019-06-07 | 分类于 leetcode

Best Time to Buy and Sell Stock

买卖股票的最佳时机,返回最大收益。
Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Description

解题思路:
依次遍历整个数组,利用minprice这个变量来保存每个元素之前的最低价格,并计算出在该元素卖出时能够带来的最大收益,最后将每个元素卖出时得到的最大收益进行比较即得到整个系统的最大收益。

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
maxprofit = 0
minprice = float('inf')
for price in prices:
minprice = min(minprice, price)
profit = price - minprice
maxprofit = max(maxprofit, profit)
return maxprofit

tip:
Python中可以用如下方式表示正负无穷:float(“inf”), float(“-inf”)

Leetcode-2sum-python

发表于 2019-06-07 | 分类于 leetcode

Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
Description

1
2
3
4
5
6
7
8
9
10
11
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i in range(0, len(nums)):
for j in range(i+1, len(nums)):
if nums[i]+nums[j]==target:
return [i, j]

初始版本的思路:
依次将两个数相加,由于题目中说明只存在一对答案,因此如果存在符合条件的两个下标,就是需要的答案。
测试未通过:
leetcode要求需要满足时间复杂度。

1
2
3
4
5
6
7
8
9
10
class Solution(object):
def twoSum(self, nums, target):
if len(nums) <= 1:
return False
buff_dict = {}
for i in range(len(nums)):
if nums[i] in buff_dict:
return [buff_dict[nums[i]], i]
else:
buff_dict[target - nums[i]] = i

leetcode上提供的一种O(n)的python解法,使用字典, python字典采用hash方式。
解题思路:
建立一个字典,依次比较列表中的元素。如果字典的键中不存在该元素,就将target - nums[i]作为字典的键,并将对应的下标作为值;如果字典键中存在该元素,则说明字典中存在符合要求的对应元素,通过buff_dict[nums[i]]提取出另一个元素的下标。

VScode + LaTex + TexLive 搭建

发表于 2019-06-07 | 分类于 latex

分别下载并安装VScode, TexLive

  1. VScode下载与安装:https://code.visualstudio.com/

  2. TexLive下载(清华大学镜像):https://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/Images/

    TexLive安装教程:https://blog.csdn.net/so_geili/article/details/72636466

  3. VScode安装插件:LaTex language support, LaTex Workshop

  4. 安装完毕后开始使用,创建临时文件temp.tex, 输入以下内容:
    1
    2
    3
    4
    5
    6
    7
    8
    \documentclass[UTF8]{ctexart}
    \title{文章标题}
    \author{David}
    \date{\today}
    \begin{document}
    \maketitle
    This is the context of the article.
    \end{document}

然后按Ctrl+S 完成编译加保存功能。

  1. 查看PDF

    1539261843199

    左侧工具栏选择 TEX-View LaTex PDF-View in VScode Tab后,右侧就会出现PDF,

    1539261941744

    每次修改后,按Ctrl+S, 右边PDF会进行实时编译。

  2. 常用的Latex数学符号:http://www.mohu.org/info/symbols/symbols.htm

L1和L2正则化

发表于 2019-06-07 | 分类于 机器学习

L0范数,L1范数,L2范数

L0范数是指向量中非0元素的个数。

如果我们用L0范数来规则化一个参数矩阵W的话(正则项),就是希望W的大部分元素都是0。换句话说,让参数W是稀疏的。

L1范数是指向量中各个元素绝对值之和,也有个美称叫“稀疏规则算子”(Lasso regularization)。

阅读全文 »

Hexo Next 解决 Busuanzi 统计浏览失效

发表于 2019-06-07 | 分类于 blog

由于busuanzi(不蒜子)的网址更新,导致了使用Hexo Next主题时统计浏览数失效.

不蒜子官网:http://ibruce.info/2015/04/04/busuanzi/

解决方法:

到hexo的themes文件夹下, 进入

\themes\next\layout_third-party\analytics

打开: busuanzi-counter.swig

将src=”https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js“

修改为src=”https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js“

即可.

Hello World

发表于 2019-06-07 | 分类于 test

hexo 更新

hexo clean

hexo g -d

David

git 常用操作

发表于 2019-06-07 | 分类于 git

以下操作均在gitbash里完成。
在使用Git提交前,必须配置用户名和邮箱,这些信息会永久保存到历史记录中。
git config —global user.name “Test”
git config —global user.email Test@test.com

初始操作

  1. 生成密钥
    ssh-keygen -t rsa -C “your_email@example.com”
    这里会有设置密码的地方,指的是访问私钥的密码。
    公钥和私钥一般都会存放在C盘对应的用户名下
    生成了公钥后记得把公钥放在github的SSH设置里。

  2. git init 创建仓库
    首先在E:创建一个gittest文件夹,即我们的项目
    cd E:
    mkdir gittest
    cd gittest (!记住一定要进入这个文件)
    然后将当前目录进行初始化。
    git init

  3. git add 增加文件到暂存区
    首先在目录下创建一个文件a.txt
    内容为12345
    这个时候操作git status, 会提示Untracked files, 需要将a.txt加入暂存区 , 因此
    git add a.txt
    完成将a.txt加入到暂存区的操作。
    这个时候操作git status, 会提示changes to be commited, 需要将a.txt加入到版本库
    注: git add . 可以将所有修改的文件加入到暂存区

    阅读全文 »

因子分解机 FM和FFM

发表于 2019-06-07 | 分类于 推荐系统

因子分解机 Factorization Machine

因子分解机主要是考虑了特征之间的关联。

FM主要是为了解决数据稀疏的情况下,(而SVM无法解决稀疏问题),特征怎样组合的问题。

数据稀疏是指数据的维度很大,但是其中为0的维度很多。推荐系统是常见应用场景,原因是推荐系统中类别属性(如商品id)比较多,每一种类别属性经过onehot处理后会产生大量值为0的特征,导致样本变得稀疏,而FM就可以解决这种样本稀疏的问题。

阅读全文 »
1…345
David

David

From Zero to One

45 日志
16 分类
39 标签
© 2019 David 苏ICP备17049163号-2
由 Hexo 强力驱动
|
主题 — NexT.Gemini v5.1.4