u012560212的博客

私信 关注
hiptonese
码龄7年

一枚微电子码农,主攻机器学习,深度学习。目前在学习TensorFlow,欢迎赐教。

  • 484,436
    被访问量
  • 121
    原创文章
  • 18,515
    作者排名
  • 32
    粉丝数量
  • 于 2013-10-24 加入CSDN
获得成就
  • 获得104次点赞
  • 内容获得57次评论
  • 获得97次收藏
荣誉勋章
兴趣领域
  • #人工智能
    #图像处理#机器学习#Python#算法#PyTorch#视觉/OpenCV#数据分析#深度学习#神经网络#TensorFlow
TA的专栏
  • python
    4篇
  • 数据结构
    1篇
  • 最近
  • 文章
  • 资源
  • 问答
  • 课程
  • 帖子
  • 收藏
  • 关注/订阅

Python多线程的简易使用

python的多线程简易使用import multiprocessing as mpdef show(ctx, proc): ‘’‘多线程执行的主体’‘’ for i in ctx: print('ctx: %d, proc: %d' %(i, proc))def mul_show(ctx, proc_num): ‘’‘show 函数多线程执行’‘’ ...
原创
75阅读
0评论
0点赞
发布博客于 2 年前

python 生成器 yield

迭代器在说生成器之前,我们先讲讲迭代器: 迭代器包括:list, set, dict等,一个比较明显的例子是都能用for in 的形式遍历;def iter(n): i = 0 l = [] while i <n: l.append(i) i+=1 return lfor i in iter(3): pri...
原创
191阅读
0评论
0点赞
发布博客于 3 年前

三球排序问题

题目:有三种颜色的球,白色(0表示),红色(1表示),蓝色(2表示)。 给出序列[2,1,0,1,2,0,0,1,2],请按照白色,红色,蓝色给这个序列重新排序。 得到的结果为[0,0,0,1,1,1,2,2,2]。算法的时间复杂度为O(N)。思路:1 使用三个指针 begin, end, current。begin指向头部,end指向尾部,current指向头部。2 指针的运...
原创
1531阅读
0评论
0点赞
发布博客于 3 年前

C++ priority_queue (优先级队列)的使用

priority_queue的内部实现是堆,本问介绍了如何利用优先级队列实现大顶堆,小顶堆,以及自定义数据类型的排列。#include <iostream>#include <queue>using namespace std;struct Node{ int val; int priority; Node(int val, int ...
原创
483阅读
0评论
0点赞
发布博客于 3 年前

python 的多线程以及锁定

下面的代码主要是使用一个类对外部数据(myList)进行修改,由于两个线程同时对myList修改,在不加锁的情况下,可能导致数据被不安全修改。 读者可尝试将“开启锁”和“释放锁”注释,重运行,看效果。#!/usr/local/bin/python# coding: utf-8import threadingimport timeclass ChangeVal(threading.
原创
225阅读
0评论
0点赞
发布博客于 3 年前

python list.append()是没有返回值的

今天写leetcode的时候,被这个问题卡了好久。主要是平时没注意。d = dict()d[0] = [0]# 我们希望往d[0]添加一个元素d[0] = d[0].append(1)# d = {0:None} 因为 list.append() 方法是没有返回值的# 正确做法为d[0].append(1)# d= {0, [0,1]}
原创
2680阅读
0评论
2点赞
发布博客于 3 年前

C++ 字符串的查找,替换和子串

int main() { string s = "12+13"; // 查找 int index = s.find("+"); // 替换, (起始位置, 子串长度, 替换后的值,这里替换为空,相当于删除) string ss = s.replace(index, 1, ""); // 子串 string sub = s.substr(0,
原创
3115阅读
0评论
0点赞
发布博客于 3 年前

C++ string 和 int, float double long数据类型的转换

int main() { int val = 10; // int -> string string s = to_string(val); // str -> int int i = atoi(s.c_str()); // str -> float double f = atof(s.c_str()); // str -> long
原创
253阅读
0评论
0点赞
发布博客于 3 年前

C++ 成员函数后面加 const

c++ 成员函数后面的const, 表示传入该成员中的隐藏指针 “this” 是const类型的。这表示在该成员中不能修改其他成员变量。class Test{public: int val; void set(int i) const { this->val = i; //error: 不能用const类型的this修改其他成员;
原创
323阅读
0评论
0点赞
发布博客于 3 年前

C++ 中使用for循环和迭代器遍历容器

之前一直看的是第四版的《C++ Primer》,里面貌似只介绍了用迭代器遍历容器(可能是受当时版本所限),这里记录下如何用for循环来遍历容器(也可遍历普通数组)class Solution{ // C++ 中默认的成员函数类型是private, 从java转过来的程序员需要注意;public: void traverse_for(vectorint> vec) {
原创
5073阅读
0评论
1点赞
发布博客于 3 年前

快速排序的C++ 实现

class Solution {public: // 入口程序 void quickSort(vector<int> &vec) { int L = 0; int R = vec.size()-1; int *pVec = &vec[0]; QuitSort(pVec, L, R); } int get
原创
141阅读
0评论
0点赞
发布博客于 4 年前

C++ 链表的归并排序

void disp_nodeList(ListNode *p) { while (p!=NULL) { disp(p->val); p = p->next; }}class Solution {public: ListNode* sortList(ListNode* head) { if (head==NULL || hea
原创
676阅读
0评论
0点赞
发布博客于 4 年前

C++ 指针数组 & 指向数组的指针

参考自《C++ Primer》template <typename T>void disp(T i) { cout<<i<<endl;}int main() { int i = 0; int j = 1; int arr[2][2] = {{1,2},{3,4}}; // 长度为2的指针数组 int *p[2]; p[0] = &i;
原创
1611阅读
0评论
0点赞
发布博客于 4 年前

C++ const 指针 & 指向const对象的指针

参考自 《C++ Primer》int main() { int i = 0; int j = 1; // const 指针,指针内容不可修改 int *const p = &i; p = &j; // error, 由于p被定义为const指针,因此其指向的物理地址是不可修改的 *p =10; // right, 通过const指针可以修改其所指对象
原创
208阅读
0评论
0点赞
发布博客于 4 年前

C++ 中引用(reference)和指针的区别

参考自《C++ Primer》众所周知,引用和指针都允许用户简介的访问对象,本文谈谈这两者的区别: * 1 引用创建之时必须初始化,且不可修改(即不可让引用指向其他对象,只能通过引用修改其原本的对象); * 2 对引用赋值,可以修改引用指向的对象; 对指针赋值,修改指针所指向的地址int main() { int i = 1; // 定义引用 int &r = i;
原创
243阅读
0评论
0点赞
发布博客于 4 年前

C++ 指针的两种操作,通过指针赋值 & 对指针赋值

// 打印函数template <typename T>void disp(T i) { cout<<i<<endl;}int main() { int i = 1; int *p = &i; *p = 2; // 通过指针赋值 disp(i); int j = 10; // 对指针赋值,将指针p指向j p = &j;
原创
44727阅读
1评论
3点赞
发布博客于 4 年前

python DataFrame 取差集

需求:给定一个dataframe和一个list,list中存放的是dataframe中某一列的元素,删除dataframe中与list元素重复的行(即取差集)。 在网上搜了一圈,好像没看到DataFrame中取差集的方式,所以自己写了一个。方法比较繁琐,如果有更简便的方式,请留言。import pandas as pddata = [[1,2,3],[2,3,4],[3,4,5],[4,5,6]
原创
20080阅读
8评论
2点赞
发布博客于 4 年前

使用Tensorflow的slim库进行迁移学习

由于slim库不是tf的核心库,因此需要到github下载相关代码,这里假设我的工作目录为:/home/hiptonese/MigrationLearning1 下载代码:https://github.com/tensorflow/models2 将下载好的代码放到工作目录下3 下载你所需要的模型的checkpoint文件(该文件存放了模型预训练的变量值),这里列出了各个常用模型的ckpt文件
原创
3165阅读
0评论
0点赞
发布博客于 4 年前

Tensorflow 在训练中的内存溢出以及线程安全问题

tensorflow
原创
2657阅读
0评论
0点赞
发布博客于 4 年前

Ubuntu 中anaconda为jupyter添加kernel

之前安装tensorflow的时候利用anaconda 创建了环境tensorflow(python=3.5),但是启动环境:source activate tensorflowjupyter notebook执行了上述命令后,jupyter启动的内核任然是anaconda默认的内核(python 3.6),该内核中不存在我已经安装好的tensorflow。现在的问题是,如何往jupyter中添加
原创
1773阅读
0评论
2点赞
发布博客于 4 年前

java 实现的simhash中文指纹项目

源码地址 https://github.com/yangydeng/SimhashForChineseimhashForChinese 中文的simhash指纹生成工具。 功能包括:利用simhash算法产生文本对应的指纹,可用指纹对照的方式去重;加入高频词过滤机制;可自行设置特殊词性的权重;可自行设置停用词/停用词性;感谢hanlp开源项目提供的分词以及词性标注功能;http
原创
2254阅读
0评论
0点赞
发布博客于 4 年前

java 日期规整化

之前在项目中写了一个用于规整时间格式,在Github上开源相关部分。地址:https://github.com/yangydeng/StringTransferUtils/blob/master/src/main/java/Utils/DateTimeUtil.java代码效果:public static void main(String[] args) { /
原创
393阅读
0评论
0点赞
发布博客于 4 年前

Java 遍历利用entry遍历Map时类型转换的问题

public class test { public static void main(String[] args) { Map<Integer,String> map = new HashMap<>(); for (int i=0;i<5;i++) { map.put(i,Integer.toString(i)); }
原创
1717阅读
0评论
0点赞
发布博客于 4 年前

机器学习中,如何利用训练集&测试集来判断 方差(varience)& 偏差(bias)

内容基于NG的课程varience and bias tardeoff假设我们需要对图片中是否是“猫”做出判断,并且数据集D给出的标签是完全正确的。现在将数据集切分成训练集trainingSet 和 测试集testingSet,训练出了多个模型,并得出以下结果: 模型 训练集的准确率 测试集的准确率 结论 A 1% 1.2% low bias & low varienc
原创
1590阅读
0评论
0点赞
发布博客于 4 年前

Python numpy 中 keepdims 的含义

keepdims主要用于保持矩阵的二维特性import numpy as npa = np.array([[1,2],[3,4]])# 按行相加,并且保持其二维特性print(np.sum(a, axis=1, keepdims=True))# 按行相加,不保持其二维特性print(np.sum(a, axis=1))输出array([[3], [7]])array([3, 7])
原创
22295阅读
4评论
26点赞
发布博客于 4 年前

Ubuntu 16.04 安装 tensorflow

1 下载Anaconda清华的镜像,我选的anaconda3最新的版本2 安装Anacodacd 到下载文件的路径,执行bash Anan[tab]3 创建新的环境conda create -n tensorflow python=3.5.2这里特别说明一下,我看到网上有些教程直接用 python=3.5,如果直接用py3.5下载,我这里默认下载了3.5.4版本,后续无法找到tensorflow的下
原创
360阅读
0评论
0点赞
发布博客于 4 年前

ubuntu 下sbt镜像配置

sbt默认采用国外的镜像,这里使用阿里的镜像:cd ~/.sbt/gedit repositories 复制下面内容到文件中:[repositories]localaliyun-nexus: http://maven.aliyun.com/nexus/content/groups/public/ typesafe: http://repo.typesafe.com/typesafe/ivy
原创
702阅读
0评论
0点赞
发布博客于 4 年前

Java8 的日期排序

public class testSortDate { public void runApp() { String s1 = "2017-08-10 22:18:22"; String s2 = "2018-08-10 22:18:22"; String s3 = "2017-08-10 22:19:22"; List<Stri
原创
11822阅读
0评论
0点赞
发布博客于 4 年前

java8 的 map 排序

java8 的map 排序public class test { public static void main(String args[]) { List<Map<String,String>> mpList = new ArrayList<>(); Map<String,String> mp ; mp = new HashMap<>();
原创
410阅读
0评论
0点赞
发布博客于 4 年前

Java 拼接字符的两个方法

1. 用StringUtils.join()publict class test{ List<String> strList = new ArraryList<String>(); strList.add("abc"); strList.add(" def"); String str = ""; str = StringUtils.join(strList,"
原创
714阅读
0评论
0点赞
发布博客于 4 年前

Java 的三目运算

public class test {public static void main(String args[]) {int a =1;int b =2;boolean flag;flag = a>b;System.out.println(flag);}} 关键句: flag = a>b; 若满足,则flag=true;
原创
207阅读
0评论
0点赞
发布博客于 4 年前

JSON 和 JSON数组

JSON是一种key-value的数据结构eg. {"id":"1","name":"Mike"}JSON数组是多个JSON的集合eg. {"member":[                  {"name","Mike"},{"name","Tom"},{"name","Jerry"}]      }多个JSON对象放在[...]之中,相互之间用逗号隔开。
原创
142阅读
0评论
0点赞
发布博客于 4 年前

linux的环境变量

今天梳理了一下linux的环境变量:一. 适用于linux所有用户的1. /etc/profile   该文件在用户每次登陆时会重新加载一次2. /etc/bash.bashrc  该文件在用户每次打开shell的时候会被重新加载一次,网上说不易放太多环境变量,会影响开启速率3. /etc/environment  该文件与上述不同的地方是,environment是针对
原创
256阅读
0评论
0点赞
发布博客于 4 年前

spark 报错:py4j.protocol.Py4JJavaError

环境:ubuntu12.04  spark 2.1.0博主使用pyspark启动spark,按照教程运行如下代码:lines = sc.textFile('README.md')lines.conut()spark报错,py4j.protocol.Py4JJavaError原来spark默认是从hdfs上都文件的,博主的文件存放在本地路径下,因此需要改为:lines = sc.textF
原创
20393阅读
1评论
4点赞
发布博客于 4 年前

java 通过map的value返回其对应的key (遍历map)

java的map是常用的数据结构,在使用中一般使用key去访问value,但是有时候也会碰到需要用value的值来找到对应的key的情况。由于map的key是唯一的,所以用key->value不会产生歧义,但是value的值可能会重复,因此通过value来返回key的话会可能会匹配到多个key值,这里我们只考虑使用迭代的方法返回第一个匹配的key值。import java.util.Has
原创
1358阅读
0评论
0点赞
发布博客于 4 年前

java 记录字符串中空格的下标

import java.util.ArrayList;import java.util.List;/** * Created by Yangyang Deng on 17-7-19. */public class test { public static void main(String args[]) { String str = "aa b bd e cc
原创
1607阅读
0评论
0点赞
发布博客于 4 年前

python使用类存储数据

今天review别人的代码,发现一个利用class+dic存储数据的方式:假设我们需要存储一些人的基本信息,sex,age,phone。class info: sex=None age=None phone=Noneif __name__ == "__main__": # 这里注意info()中的括号不能省略,否则不会产生新的对象,后面的赋值会覆盖前面的
原创
2820阅读
0评论
0点赞
发布博客于 4 年前

利用IntelliJ 创建一个Maven项目

创建Maven项目:File->new->project->maven (左侧)创建好后,我们可以在pom.xml中添加项目依赖的jar包,假设我们要添加的jar包为 json-lib:1.首先到这个网站:https://mvnrepository.com/2.然后在搜索栏中输入:json-lib;3.从弹出的结果选择你需要的版本,不知道该选哪个版本的可以选择热度较高的
原创
209阅读
0评论
0点赞
发布博客于 4 年前

mybatis 中 $ 与 #的区别

技术上,${value} 会在预编译之前就替换掉sql中的内容,而#{value}在预编译之后相当于一个占位符。1. 直观上,$(value)的用法相当于从字符串的层面就替换了sql语句的内容:select * from MyTable where $(value)="Mike";当我们把value="name"传入时,上述的sql将变为:select * from MyTab
原创
228阅读
0评论
0点赞
发布博客于 4 年前

intelliJ idea运行新的test功能时,报错:class not found "....." empty test suite

这时候有可能是intelliJ idea的加载有问题,可以尝试到工程的根目录下删除.idea文件(由于.idea是隐藏文件,查看是用ls -a),然后再重新加载项目,intelliJ会重新生成新的.idea文件,此时有可能解决标题错误。
原创
30264阅读
13评论
10点赞
发布博客于 4 年前

使用 MySQLdb 从数据库拉回数据,并将数据存入 python的DataFrame中

MySQLdb是python用于连接数据库的三方库,DataFrame是用于存数据的类型,每个DataFrame相当于一张二维的数据表。# -*- coding: utf-8 -*-#这是一个使用Python连接数据库并把数据放入DataFrame的方法import MySQLdbimport pandas as pdfrom pandas import DataFramedb
原创
3125阅读
0评论
3点赞
发布博客于 4 年前

mysql 子表查询;去重查询

假设 表名:tb1        列:id,name,age1. 子表查询:    (1)得到年龄大于16的结果: select * from tb1 where age>16;      (2) 在上述的基础上,得到idselect * from ( select * from tb1 where age>16) as tmp;        注意后面的 as tmp一定
原创
1365阅读
0评论
1点赞
发布博客于 4 年前

python 变量的拷贝 copy

假设有如下代码:A = 10tmpA = Aprint id(A),id(tmpA)可以看到,id(A)与id(tmpA)相同。这意味着执行 tmpA = A的时候,电脑并未给A赋予一块新的地址,而是让tmpA指向A的存储地址。可以通过下面的方法得到A的拷贝:import copyA = 10tmpA = copy.copy(A)print id(A),id(tmpA
原创
531阅读
0评论
0点赞
发布博客于 4 年前

Python 字典(dictionary)的排序

# 假设有字典dic = {'A':4,'B':3,'C':2,'D':1}#现在要对dic 按照value的值从到大排序L = sorted(dic.items(),key=lambda item:item[1])print L#[('D', 1), ('C', 2), ('B', 3), ('A', 4)]#返回一个列表,其中每个元素包含了原本dic中的key & value
原创
4363阅读
0评论
0点赞
发布博客于 4 年前

Python 子字符串查询以及如何简单规避转义字符的麻烦

1. 子字符串查询:#假设我们有如下stringstring = 'hello world bye world 2017' #我们需要查找如下子串是否存在于string中sub_str = 'rld 20'print(sub_str in string)2. 如何简单规避转义字符问题:#假设有如下string# he said: ' hello '#在字符串中出现了
原创
1803阅读
0评论
0点赞
发布博客于 4 年前

leetcode | Path Sum II

题目:将返回一棵二叉树从根到叶节点所经过的值之和等于Sum的路径。Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22,
原创
190阅读
0评论
0点赞
发布博客于 4 年前

使用TensorFlow编写损失函数 交叉熵(cross entrophy)| 均方误差(MSE)

一. 交叉熵,用于分类问题:4分类问题,数据的批大小为2,所以此处标签可以用 2*4的矩阵表示,特别注意:输出层为softmax层。y_:数据的正确标签,用one hot码表示y1:第一批输入softmax层的激活值y2:第二批输入softmax层的结果output1:第一批的最终输出结果output2:~cross_entropy1:使用y1的交叉熵cross
原创
7209阅读
0评论
0点赞
发布博客于 4 年前

已知二叉树的先序遍历(preorder)和中序遍历(inorder) 或 中序和后序(postorder),还原该二叉树

# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = None# preorder 先序# inorder 中序
原创
1285阅读
0评论
0点赞
发布博客于 4 年前

leetcode | 按层访问一棵二叉树 | Python

题目:Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree [3,9,20,null,null,15,7], 3 /
原创
702阅读
0评论
0点赞
发布博客于 4 年前

TensorFlow 保存和导入计算图中的部分节点

#保存计算图中的节点import tensorflow as tffrom tensorflow.python.framework import graph_utilv1 = tf.Variable(tf.constant(1.0,shape=[1]),name='v1')v2 = tf.Variable(tf.constant(2.0,shape=[1]),name='v2')res
原创
3994阅读
0评论
1点赞
发布博客于 4 年前

树的先序,中序,后序遍历 | Python

一直觉得遍历先序,中序,后序遍历一棵树,总会记混。后来才发现用代码来记忆,然后反推遍历的结果更加靠谱。class Solution(object): def inorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """
原创
3160阅读
0评论
1点赞
发布博客于 4 年前

Tensorflow 保存和加载模型

import tensorflow as tfdef save_mode(): v1 = tf.Variable(tf.constant(1.0,shape=[1]),name='v1') v2 = tf.Variable(tf.constant(2.0,shape=[1]),name='v2') res = tf.add(v1,v2,name='add_res')
原创
428阅读
0评论
0点赞
发布博客于 4 年前

Tensorflow 的变量管理

Tensorflow有一套完善的变量管理机制,能够让开发者在使用时条理清晰。1. Variable & get_variable:下面两个对v的定义是等价的。import tensorflow as tfv = tf.Variable(tf.constant(1.0,shape=[1]),name='v')v = tf.get_variable('v',shape=[1],init
原创
307阅读
0评论
0点赞
发布博客于 4 年前

当元素种类远远小于数组长度的排序 | Python

题目:假设一个数组nums由三种元素组成 1,2,3;并且nums的长度N远远大于3,对nums进行排序。解析:与一般的排序不同,这里我们已知元素的种类,并且数组的长度N>>3,所以:在数组头尾使用两个指针,将左边的“3”与右边的“1”互换;将左边的“2”与右边的“1”互换;将左边的“3”与右边的“2”互换;经过三轮交换,可以完成排序,时间复杂度 O(3N);而
原创
310阅读
0评论
0点赞
发布博客于 4 年前

在原数组中删除重复的元素 | Python

在原数组中删除重复元素:假设 nums = [1,1,1,2,2,3,3,3,3,4]; 经过删除后,得到的结果为:nums=[1, 2, 3, 4]数组在做增删时,需要借助index,操作起来比较麻烦(相对链表而言更难),个人觉得不适合用for 的方式控制指针运动,而是应当在循环体中控制指针更为容易。可以先定义一个死循环,然后在循环体内部定义跳出条件。def dropDuplicat
原创
4290阅读
2评论
0点赞
发布博客于 4 年前

Tensorflow 在损失函数中加入正则项(Normalization)

L1和L2正则:举例说明,假设有一个数组 nums=[1,2,3,4]L1 = a*(|1|+|2|+|3|+|4|)L2 = a*(1^2+2^2+3^2+4^2)/2其中a是系数,用于平衡正则项与经验损失函数的权重关系,即:C = loss+a*Normalization。这里说明一下,利用L1经过训练后,会让权重得到稀疏结,即权重中的一部分项为0,这种作用相当于对原始数据
原创
10012阅读
2评论
1点赞
发布博客于 4 年前

Tensorflow 学习速率的设置|学习速率的指数下降

随着学习的进行,深度学习的学习速率逐步下降  为什么比  固定的学习速率 得到的结果更加准确?如上图所示,曲线代表损失值,小球一开始位于(1)处,假设学习速率设置为 △ v,那么根据梯度下降,损失值将在(1)  (2)之间来回移动,无法到达最小值(3)处。要想到达(3),只能降低学习速率。下面介绍 学习速率指数下降 公式:公式中,learning_rate: 当
原创
20007阅读
1评论
3点赞
发布博客于 4 年前

Tensorflow 搭建简单神经网络 | Python

本案例为回归问题,采用了一层隐层,隐层和输出层的激活函数都为 relu,损失函数MSE。import tensorflow as tffrom numpy.random import RandomStatew1 = tf.Variable(tf.truncated_normal([2,3],seed=1))w2 = tf.Variable(tf.truncated_normal([3,1
原创
2487阅读
0评论
0点赞
发布博客于 4 年前

Tensorflow 简单矩阵乘法举例

这里先说一下 tf.Variable 和 tf.placeholder的区别,两者都可以盛放变量,但在启动会话前,tf.Variable必须先被赋值和初始化,而tf.placeholder是需要在启动会话的时候传入值的;从直观上讲,tf.placeholder更加接近函数的形参。import tensorflow as tfw1 = tf.Variable(tf.truncated_norm
原创
3240阅读
0评论
0点赞
发布博客于 4 年前

Python list.pop() | list.remove() 用法详解

list.pop(index) 用于从列表中删除元素,该语句还能返回被删除的元素。注意:index表示该元素的下标。a = [0,1,2,3,4,5]print a.pop() # 输出5,这个语句相当于 print a.pop(-1)print a # [0,1,2,3,4]print a.pop(0) # 0print a # [1,2,3,4]print a.pop(3)
原创
3158阅读
0评论
2点赞
发布博客于 4 年前

在不使用额外空间的前提下,将数组的偶数放到数组的奇数前面 | Python

题目:在不使用额外空间的前提下,将数组的偶数放到数组的奇数前面思路:使用两个指针,ptr_odd:奇数指针,ptr_even:偶数指针;ptr_odd从前往后运动,ptr_even从后往前运动,每次交换两者的数据,当两者相遇,则退出循环。def fun(nums): ptr_odd = 0 ptr_even = len(nums)-1 while
原创
597阅读
0评论
0点赞
发布博客于 4 年前

用二分法查找循环递增序列 | Python

问题:使用二分法在循环递增序列中查找指定元素,返回其下标;若不存在,则返回-1.要求时间复杂度为:O(logN)。循环递增序列的定义:       nums = [7,8,9,10,1,2,3,4,5,6]       即,一个序列被分为两个子序列,每个子序列都是递增的,并且如果在指定位置(如“10”,“1”之间)切断重组,则可以变为递增序列。思路:注意:由于时间复
原创
1282阅读
0评论
1点赞
发布博客于 4 年前

Python 字符串排序 | 字符串翻转

1. 排序string = "orange"char_list = sorted(string) #已排序,以字符存在列表中sorted_string = "".join(char_list) #将所有字符连起来print sorted_string 2. 翻转string = "abcde"print string[::-1]
原创
1716阅读
0评论
0点赞
发布博客于 4 年前

leetcode | 图片的旋转(顺时针90°) | Python

题目:You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).class Solution(object): def rotate(self, matrix): """ :type matri
原创
1004阅读
0评论
0点赞
发布博客于 4 年前

leetcode | Permutations | 利用深度优先(DFS)的方法排列组合列表

Given a collection of distinct numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1
原创
913阅读
0评论
0点赞
发布博客于 4 年前

leetcode | Combination Sum & Combination Sum 2

1. Combination Sum:Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repea
原创
211阅读
0评论
0点赞
发布博客于 4 年前

python | append( ) & extend( )的区别

a = [1,2,3]b = [4,5]a.append(b)#a = [1,2,3,[4,5]]a = [1,2,3]b = [4,5]a.append(b)#a = [1,2,3,4,5]
原创
194阅读
0评论
0点赞
发布博客于 4 年前

leetcode | 最大装水量问题 | python

问题如下:Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i
原创
1885阅读
0评论
0点赞
发布博客于 4 年前

使用Python类的 __init__( ) 函数定义成员变量

class Test(): def __init__(self,s,i): self.s = s self.i = i def excute(self): self.s+=" world" self.i+=1 print self.s print
原创
4581阅读
0评论
0点赞
发布博客于 4 年前

leetcode | 用拟牛顿法求一个数的平方根 | Python

class Solution(object): def mySqrt(self, x): """ :type x: int :rtype: int """ k = x while k**2>x: k = (k+x/k)/2 return k解出来的结果k为
原创
578阅读
0评论
0点赞
发布博客于 4 年前

leetcode 树的最小深度 | map( ) 的用法

求一棵树的最小深度:需要注意的是,如果一棵树退化为链表,即一棵树及其所有子树都仅有 左/右 子树,则应当算其对应链表的最大长度。该树的最小深度为5思路:利用深度优先的方式,递归的遍历所有结点,返回“较小的深度+1”,但如果较小的深度为0,则返回“较大的深度+1”。# Definition for a binary tree node.# class TreeNode(ob
原创
558阅读
0评论
0点赞
发布博客于 4 年前

判断一棵二叉树是否为另一棵二叉树的子树,Python实现

假定s是大的树,判断t是否为s的子树:# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass So
原创
1533阅读
0评论
0点赞
发布博客于 4 年前

求二叉树的所有结点之和,Python实现

问题:求二叉树所有结点之和。思路:遍历所有结点,将当前结点的值加入最终的结果,再继续遍历其左右结点。# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None
原创
2336阅读
0评论
1点赞
发布博客于 4 年前

leetcode 计算二叉树中出现最多的元素,Python实现

题目:Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST.Assume a BST is defined as follows:The left subtree of
原创
444阅读
0评论
0点赞
发布博客于 4 年前

leetcode 找到树中距离最大的两个结点,Python实现

题目:Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may
原创
3986阅读
0评论
0点赞
发布博客于 4 年前

leetcode 将已排序的 数组/链表 转换为二叉搜索树(BST),Python实现

思路:不论是数组还是链表,递归地找到他的root(即序列的中点),并返回。1. 将数组转换为二叉树:# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None#
原创
2264阅读
0评论
0点赞
发布博客于 4 年前

python 利用dictionary的get( ) 方法做计数统计

假设统计 s="aabbccc"中,每个字符出现的次数:结果应当为:"a":2, "b":2, "c":3s = "aabbccc"dic = {}for ch in s: dic[ch]=1+dic.get(ch,0)print dic
原创
6861阅读
0评论
2点赞
发布博客于 4 年前

Tensorflow 构建一个多层cnn网络,附注释。

# -*- coding: utf-8 -*-"""Created on Mon May 22 12:28:13 2017@author: Yangyang Deng@Email: yangydeng@163.com本文件为 《Tensorflow官方文档 v-1.2》中,构建一个多层神经网络案例,附注释。"""import tensorflow as tf import n
原创
2722阅读
0评论
0点赞
发布博客于 4 年前

leetcode | Delete Duplicate Emails | 删除表中重复项

题目:Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.+----+------------------+| Id | Email |+-
原创
453阅读
0评论
1点赞
发布博客于 4 年前

leetcode SQL Employees Earning More Than Their Managers

题目如下:The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.+----+-------+--------+-----------+| Id | Name |
原创
204阅读
0评论
0点赞
发布博客于 4 年前

python 自带函数 max min的灵活用法,enumerate函数

1. max min函数的灵活用法,主要是对max min 函数中 key的灵活定义:eg. 1: arr = ["abc","abcd","abcde"] 找到 arr中长度最短的字符串:arr = ["abc","abcd","abcde"]res = min(arr,key = lambda ele:len(ele)) #等同于: res = min(arr,key=len)
原创
2930阅读
0评论
0点赞
发布博客于 4 年前

leetcode 用fast-slow 指针的方法判断链表是否为回文链表。

题目:Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?思路:找到链表的中点,并从中间切断链表,将后半部分的链表翻转,最后再依次比较。Python 实现class Solutio
原创
301阅读
0评论
0点赞
发布博客于 4 年前

Leetcode 求买股票的最大盈利问题

1. 只允许买卖一次股票,求最大收益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 on
原创
1373阅读
0评论
0点赞
发布博客于 4 年前

Python 插入法实现链表排序

class Solution(object): def insertionSortList(self, head): cur = dummy = ListNode(-1) while head: while cur.next and cur.next.val<head.val: #找到插入点 cur
原创
1854阅读
0评论
0点赞
发布博客于 4 年前

leetcode: Reorder List 的Python实现

题目:Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reord
原创
405阅读
0评论
0点赞
发布博客于 4 年前

python 链表的归并排序(附数组的归并排序作为对照)

1. 链表的归并排序:class Solution(object): def merge(self, left, right): head = tail = ListNode(-1) while left and right: if left.val<right.val: tail.next =
原创
550阅读
0评论
0点赞
发布博客于 4 年前

leetcode Rotate list (链表旋转)的python实现

题目如下:题目解释:给定一个链表,将链表末尾的k个结点移动到最前面。思路:采用 fast-slow 指针的方法,令fast指针先移动k步,步长为1。然后两个指针同时移动,当fast指针到达最末尾时,将fast指向head,slow指向None,则完成旋转。注:题目中的k有可能大于链表总长度,因此需要对k取模。class Solution(object):
原创
1068阅读
0评论
0点赞
发布博客于 4 年前

找出字符串的最长回文 python实现

在leetcode的solution中发现的一个非常容易理解的寻找最长回文的方法,题目如下:Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.class Solution(object):
原创
2749阅读
0评论
1点赞
发布博客于 4 年前

产生树的镜像,判断一棵树是否为对称二叉树

代码均为Python:1. 产生树的镜像:class Solution: def Mirror(self,root): if(root): root.left,root.right = root.right,root.left self.Mirror(root.left) self.Mirror
原创
686阅读
0评论
0点赞
发布博客于 4 年前

python 通过函数无返回的修改数据结构,在函数外部,数据结构也可被修改

class TreeNode: def __init__(self,x): self.val = x self.left = None self.right = Nonedef change(root): root.left,root.right = root.right,root.leftif __name__=="__ma
原创
543阅读
0评论
0点赞
发布博客于 4 年前

python 得到树的深度、判断树是否为平衡树

0. 树中结点的结构: class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None 1. 树的深度: def TreeDepth(self,pRoot): if(not p
原创
2487阅读
0评论
0点赞
发布博客于 4 年前

连续组数组的最大和

在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如: [1,-2,3,10,-4,7,2,-5],连续子向量的最大和为18(从第2个开始,到第6个为止,下标从0开始)。class Solution: def FindGreatestSumOfSubArray
原创
135阅读
0评论
0点赞
发布博客于 4 年前

Python 实现通过指针实现链表翻转,链表奇偶下标交换,链表的冒泡排序

python 有关链表的操作问题
原创
582阅读
0评论
0点赞
发布博客于 4 年前

快速排序、堆排序、归并排序的python实现

1. 快速排序:class quick_sort: def __init__(self,arr): self.arr = arr def partitionIt(self,left,right): key = arr[left] while(left<right): while(leftkey):
原创
394阅读
0评论
0点赞
发布博客于 4 年前

用 Python实现链表的翻转,奇偶下标修改(利用数组,非常简单)

1. 首先是链表翻转效果: 0->1->2->3 ...... ->9     变为   9->8->7->......->0class NodeList: def __init__(self,x): self.next = None self.val = x def reverse(root): save = []
原创
1260阅读
0评论
0点赞
发布博客于 4 年前

利用递归的方法实现字符串倒序

问题: 利用递归的方法实现字符串倒序,例如: “abcd” -> "dcba"。附代码:def fun(string): if(len(string)==0 or len(string)==1): return string else: return fun(string[1:])+string[0] string = "a
原创
6186阅读
0评论
2点赞
发布博客于 4 年前

链表反转

链表反转是面试常考问题,这里做个总结:原链表头:pHead新链表头:p_next附代码: def ReverseList(pHead): p_next = None while(pHead is not None): p = pHead pHead = pHead.next
原创
998阅读
0评论
0点赞
发布博客于 4 年前

青蛙跳台问题

问题:一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法?解答:这个问题粗略一看可以用贪心算法解决,但运行时间太长,在一般的OJ都不能过关。  假设当台阶为n级,共有 f(n) 种跳法。通过观察可以得知,若 n = 1:  f(1) = 1;若 n = 2: f(2) = 2 ;若 n = 3: f(3) = 3;若 n = 4: f(
原创
512阅读
0评论
0点赞
发布博客于 4 年前

python 使用GridSearchCV 报错 ValueError: not enough values to unpack (expected 2, got 1)

今天写代码的时候,用GridSearchCV  做交叉验证,一直报错 :ValueError: not enough values to unpack (expected 2, got 1)反复调试后发现,    clf = GridSearchCV(GBR,param_grid=parameters,cv=kf,scoring=loss)clf.fit(train_x
原创
5369阅读
0评论
0点赞
发布博客于 4 年前

MNIST_data

解压后放在与代码统一路径即可使用
zip
发布资源于 4 年前