Valid Sudoku

  • 数独整体能够满足的情况是比较复杂.参考:编程之美 关于数独问题的讨论
  • 这道题的解法可能简单一点,因为对输入进行来限制.只是去判断是否是一个数独
    数独的形式相比较来说也是较简单的
  • 对于输入的复合列表,列表中的元素相对较稀疏,没有出现的数字用字符"."来代替
  • python 的zip()函数 a=list[list[]]
    for row in a 可以读取a 的行元素
    for col in zip(a)可以读取a的列元素
    zip(
    list[list[]]) 实现和矩阵一样的转置功能,不过目标是一个列表.
    还有zip生成的一个类是迭代器的,使用.next()来返回一个元组tuple()
  • python 的set()函数 会返回一个一个无重复元素的set的元素.
  • 注意区分python中的range方法,和元素枚举方法的差别
    例如:for i in range(0,3,6) 和 for i in(0,3,6)是完全两种概念.
  • C++中注意在for()中 ++i 与 i++的区别.i++先引用再自增;++i 是先自增再引用.在for 循环中可能还区别不大,但是在while()判断语句对最后的计算结果有很大的影响.

String to Integer(atoi)

  • 正则表达式:
    用来处理字符串的方法
    正则表达式是一种"表示法"
    ""一般是跟在需要重复元素的后面 
    转义序列:"",".","|","
    " 这些都是元字符,利用这些将元字符和字母表中的字符区别开来.
    注意反选和制表的差别,反选:[^a-z]; 制表'[1]',表示定位在行首的意义.
  • 表达式中的group函数,用来提取分组截获的字符串
  • 如果字符串中全是数字,则只要一个int(string),就可以将字符串转换成数字int .

Count and Say

  • 刚开始题目的理解有点问题.循环递减的大方向没有错.我认为是两个字符串的变换,但是别人代码中的循环递归看似简单,实则非常难.对于temp的交换,只是止步于能看懂 但是自己却是写不出来的.
  • 存在的小问题是python 及C++等语言的基础结构不了解,例如字符串如何表示,如何计数和表示字符串中的子字符串.
  • 自己的代码还有改进空间,这个思路方向再试试 count and say
  • 在python中 str()- is a keyword是程序内置的关键字.例如str(12)=='12'

Longest Word (lintcode)

  • 知识点:
    1.python中字符串也是可以实现 str.pop()操作
    2.C++中,对于vector ans {}; 整个vector可以进行 ans.size()

    对于其中的每个元素也可以实现 ans[i].size()
  • 实现方法有两种,分别是遍历一次和遍历两次

implemet strStr()

  • 注意的是,利用两次循环0(n^2)来实现的话,注意空字符串的判断
  • 采用暴力的模式匹配也可以
  • 最好的是采用 KMP算法。(这个算法还不是很理解)特别是next array 和nextval 在代码中的实现

sort letter by case

  • 需要注意的是,字符串利用循环要时刻检测循环数是否 out of range
  • 能不用一些像 erase isupper 函数 就尽量不用。 自己实现???

compare strings

  • python 字符的ascii 码 要利用 ord('a')=101
  • python 中没有自增 ++ 取而代之的是 +=1 ,这个很容易出错。

Longest Common Prefix

  • 解题思路:
    1.找到字符串中最短的子字符串,并保存长度
    2.随机抽取一个子字符串从头开始和所有的子字符串进行比对,所有子字符串都有的字符 进行保存
  • python 有个好用的set集合,循环最小长度,添加所有子字符串的i,set集合以后,如果大于两个元素,则元素不是common的 比较好用

convert sorted array to high balance binary tree

  • 这一题的 divide and conquer 的思想很重要
  • 与树的问题都会与递归有联系

binary tree inorder traversal

121 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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Note that you cannot sell a stock before you buy one.
"""

Example1

Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.

Example 2

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
import sys
class Solution:
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
# need robust
profit=0
min_price=sys.maxsize # 最小值的初始化,初始化为int的最大值
for i in prices:
if i<min_price:
min_price=i # 找到当前数值最小的价格
while i-min_price>profit:
profit=i-min_price #profit 用来保存每个数值与当前最小值的差值
return profit

  1. a-z ↩︎

LeetCode 笔记的更多相关文章

  1. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  2. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  3. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  4. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  5. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  6. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  7. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  8. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  9. Leetcode 笔记 35 - Valid Soduko

    题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...

  10. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

随机推荐

  1. C#汽车租赁系统 完整版

      Truck.cs类 //卡车类 public class Truck : Vehicle1 { //重载 public int Load { get; set; } //构造函数 public T ...

  2. Codeforces Round #192 (Div. 2) (330B) B.Road Construction

    题意: 要在N个城市之间修建道路,使得任意两个城市都可以到达,而且不超过两条路,还有,有些城市之间是不能修建道路的. 思路: 要将N个城市全部相连,刚开始以为是最小生成树的问题,其实就是一道简单的题目 ...

  3. Java性能权威指南读书笔记--之二

    新生代填满时,垃圾收集器会暂停所有的应用线程,回收新生代空间.这种操作被称为Minor GC. 老年代被填满时,垃圾收集器会暂停所有应用线程,对其进行回收,接着对堆空间进行整理.这个过程被称为Full ...

  4. scrapyd schedule.json setting 传入多个值

    使用案例: import requests adder='http://127.0.0.1:6800' data = { 'project':'v1', 'version':'12379', 'set ...

  5. 天气预报APP(2)

    之前实现了能够罗列可以罗列出全国所有的省.市.县,然后就是查询全国任意城市的天气信息.查询天气信息使用的是和风天气的api,这个api获得的天气信息是JSON格式的. 使用GSON库解析JSON数据的 ...

  6. Oracle、MySQL和Sqlserver的事务管理、分页和别名的区别

    1.在mysql中事务默认是自动提交的,只有设置autocommit为0的时候,才用自己commit(commit--rollback回滚) 2.但是在oracle中必须自己commit;不然就只能结 ...

  7. 不可错过的几款GitHub开源项目

    工作之余或者周末感觉无聊?不知道干什么?想继续提高技术,但是不知道做什么的同学,看过来,不妨利用闲暇时间来撸几个 GitHub 上还不错的开源项目,本文推荐的开源项目比较适合新手.及对MVP设计模式不 ...

  8. 【openmp】for循环的break问题

    问题描述:在用openmp并行化处理for循环的时候,便无法在for循环中用break语句,那么我们如何实现这样的机制呢?在stackoverflow上看到一个不错的回答总结一下. volatile ...

  9. Spring aop注解失效

    问题 在spring 中使用 @Transactional . @Cacheable 或 自定义 AOP 注解时,对象内部方法中调用该对象的其他使用aop机制的方法会失效. @Transactiona ...

  10. 记一次mysql数据库失而复得过程

    背景: 由于是自己买的vps搭建的博客,用的是军哥的一键lnmp源码编译安装的,文章也就几篇,对备份并不太重视,想着等服务器快到期的时候备份一下不就行了. 后来在该服务器上测试lnmp分别编译编译安装 ...