算法练习
messiran10
这个作者很懒,什么都没留下…
展开
-
【Leetcode】之Construct Binary Tree from Preorder and Inorder Traversal
一.问题描述Given preorder and inorder traversal of a tree, construct the binary tree.二.我的解题思路给定二叉树的前序遍历序列和中序遍历序列,要求重建二叉树。这道题可以采用递归的算法思想,每次将序列切分成左子树序列和右子树序列,然后递归生成即可。测试通过的程序如下:/** * Definition原创 2016-08-24 11:57:57 · 282 阅读 · 0 评论 -
【Leetcode】之Search Insert Position
一.问题描述Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array原创 2015-12-04 09:44:16 · 333 阅读 · 0 评论 -
【Kaggle练习赛】之Digit Recognizer
Kaggle是国外的一项数据挖掘赛事,近期阿里并没有开办赛事,所以准备先拿Kaggle的练习赛来热热身,顺便学习一下scikit-learn这个开源库的使用。Kaggle入门可以参见 http://blog.csdn.net/u012162613/article/details/41929171 一.问题描述The goal in this competition is to take an im原创 2016-01-15 11:32:11 · 745 阅读 · 0 评论 -
【Leetcode】之 Permutations
一.问题描述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], and [3,2,1].二.我的解题思路这原创 2016-01-22 10:53:30 · 372 阅读 · 0 评论 -
【Leetcode】之Search for a Range
一.问题描述Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm’s runtime complexity must be in the order of O(log n).If the target is not found in原创 2015-12-03 16:40:11 · 344 阅读 · 0 评论 -
【Leetcode】之Jump Game II
一.问题描述iven an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to原创 2016-01-21 10:53:40 · 381 阅读 · 0 评论 -
【Leetcode】之Multiply Strings
一.问题描述Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.二.我的解题思路这道题考的实际上就是正整数乘法的实现。由于题目中明确说明整数可以原创 2016-01-20 11:30:22 · 335 阅读 · 0 评论 -
【Leetcode】之Search in Rotated Sorted Array
一.问题描述Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its原创 2015-12-02 10:58:35 · 361 阅读 · 0 评论 -
【Leetcode】之Longest Valid Parentheses
一.问题描述Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring.For “(()”, the longest valid parentheses substring is “()”, whi原创 2015-12-01 09:56:18 · 303 阅读 · 0 评论 -
【Leetcode】之Next Permutation
一.问题描述Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possibl原创 2015-11-30 10:29:40 · 359 阅读 · 0 评论 -
【Leetcode】之Trapping Rain Water
一.问题描述Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1原创 2016-01-19 10:58:57 · 312 阅读 · 0 评论 -
【机器学习算法】之Apriori
一.算法介绍Apriori算法不同于以前接触过的机器学习算法,这种算法用于在数据集中寻找有趣的关系。这些关系可以有两种形式:频繁项集或者关联规则。 关于算法的详细介绍参见: http://blog.csdn.net/qustdjx/article/details/12770883 http://blog.csdn.net/yangliuy/article/details/7494983二.py原创 2016-01-18 16:18:21 · 504 阅读 · 0 评论 -
【Leetcode】之First Missing Positive
一.问题描述Given an unsorted integer array, find the first missing positive integer.For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant sp原创 2016-01-18 11:21:49 · 309 阅读 · 0 评论 -
【Leetcode】之Remove Element
一.问题描述Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn’t matter what you leave beyond the new length.二.我的原创 2015-11-20 15:15:18 · 327 阅读 · 0 评论 -
【Leetcode】之Substring with Concatenation of All Words
一.问题描述You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once a原创 2015-11-26 11:40:38 · 308 阅读 · 0 评论 -
【Leetcode】之Divide Two Integers
一.问题描述Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.二.我的解题思路题目要求不用乘法,除法,取Mod运算来求解两个整数的除法,意味着只可以用加减法以及移位运算符。为了提高时间效率,很自然的想到采用移位运算符来使除法尽快接近原创 2015-11-25 13:43:09 · 282 阅读 · 0 评论 -
【Leetcode】之Remove Duplicates from Sorted Array
一.问题描述Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this i原创 2015-11-19 20:04:51 · 299 阅读 · 0 评论 -
vs2013编译旧版本程序出错
VS2013多字节工程问题使用VS2013编译旧版VC++程序时,提示Building an MFC project for a non-Unicode character set is deprecated,微软提供了解决方案。一、错误信息[cpp] view plaincopyprint?"font-famil转载 2015-12-05 13:06:05 · 945 阅读 · 0 评论 -
【机器学习算法】之Adaboost
一.算法介绍《统计学习方法》对adaboost算法进行了相当精彩的介绍,尤其是后面证明adaboost算法是前向分布加法算法的特例,这就将adaboost算法拉入到 统计学习=模型+策略+算法这一框架中。 1.模型: 加法模型: Adaboost算法期望用一系列不同权重的基函数的和来构成最终的分类器。 2.策略 采用指数损失函数 3.算法 采用逐层逼近的算法,每次寻求一个a原创 2016-01-11 16:26:59 · 653 阅读 · 0 评论 -
堆排序算法原理
堆排序 堆排序是利用堆的性质进行的一种选择排序。下面先讨论一下堆。1.堆 堆实际上是一棵完全二叉树,其任何一非叶节点满足性质: Key[i]=Key[2i+1]&&key>=key[2i+2] 即任何一非叶节点的关键字不大于或者不小于其左右孩子节点的关键字。 堆分为大顶堆和小顶堆,满足Key[i]>=Key[2i+1]&&key>转载 2016-07-30 10:56:31 · 265 阅读 · 0 评论 -
AVL树的左右旋转
AVL树的旋转操作 图解 最详细各大教课书上讲的都是左旋与右旋,其实这样很容易理解错误,我们换一种叫法。我们称呼左旋为:逆进针旋转。我们称呼右旋为:顺进针旋转。老规矩,直接上图。如果再看不懂AVL树的旋转,我就无能为力了。。。如果图中有错误,欢迎指正。代码篇前文已完成了 二叉查找树的实现(BST)转载 2016-04-17 10:01:23 · 587 阅读 · 0 评论 -
平衡二叉树
平衡二叉树(Balanced Binary Tree)是二叉查找树的一个进化体,也是第一个引入平衡概念的二叉树。1962年,G.M. Adelson-Velsky 和 E.M. Landis发明了这棵树,所以它又叫AVL树。平衡二叉树要求对于每一个节点来说,它的左右子树的高度之差不能超过1,如果插入或者删除一个节点使得高度之差大于1,就要进行节点之间的旋转,将二叉树重新维持在一个平衡状态。这个方案转载 2016-03-25 20:18:56 · 340 阅读 · 0 评论 -
【Leetcode】之Maximum Product Subarray
一.问题描述Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest原创 2016-03-14 17:30:18 · 324 阅读 · 0 评论 -
【机器学习算法】之决策树算法的实现
为了加深对机器学习算法的理解,以及熟悉python,pandas,scikit-learn。现在自己实现一下主要的机器学习算法,程序记录如下:决策树类的实现程序:import numpy as np import pandas as pd import random as rd import re from sklearn import tree from sklearn imp原创 2016-02-29 19:53:05 · 547 阅读 · 0 评论 -
【Leetcode】之Find Minimum in Rotated Sorted Array
一.问题描述Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no duplicate exists in the array原创 2016-03-13 15:20:10 · 333 阅读 · 0 评论 -
【Kaggle练习赛】之Titanic: Machine Learning from Disaster
一.题目描述The sinking of the RMS Titanic is one of the most infamous shipwrecks in history. On April 15, 1912, during her maiden voyage, the Titanic sank after colliding with an iceberg, killing 1502 out原创 2016-01-20 17:21:21 · 1980 阅读 · 0 评论 -
【机器学习算法】之logistic回归
为了加深对机器学习算法的理解,以及熟悉python,pandas,scikit-learn。现在自己实现一下主要的机器学习算法,程序记录如下:logistic回归算法的实现程序:from numpy import *from sklearn import preprocessing from sklearn import cross_validation def load_data_set(原创 2016-03-05 15:27:08 · 780 阅读 · 0 评论 -
【机器学习算法】之朴素贝叶斯的实现
为了加深对机器学习算法的理解,以及熟悉python,pandas,scikit-learn。现在自己实现一下主要的机器学习算法,程序记录如下:决策树类的实现程序:from numpy import *def loadDataSet(): postingList=[['my', 'dog', 'has', 'flea', 'problems', 'help', 'please'],原创 2016-03-05 11:22:51 · 540 阅读 · 0 评论 -
【机器学习算法】之回归树
一.算法介绍回归树指的是用来解决回归问题的树模型,之前介绍过的决策树可以视为一种特殊的回归树。 1.模型 回归树是对数据空间进行线性切分,得到多个Rm。每个Rm代表着一块区域,这块区域的样本点的输出值设为一个常数。 2.策略 为了针对训练数据求解到一个合适的回归树,关键问题有两个:如何划分数据空间 每个数据空间的输出值设为多少。回归树模型采用的损失函数是平方误差,策略即是: 最小化3.算法原创 2016-01-13 16:57:05 · 1268 阅读 · 0 评论 -
【Leetcode】之 Combination Sum II
一.问题描述Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combinat原创 2016-01-14 22:07:40 · 293 阅读 · 0 评论 -
【机器学习算法】之K-means聚类
一.算法介绍1.模型 K-means算法并没有显式的数学模型,算法的目的是从数据集中得到k个中心点,每个中心点及其周围的点形成一个聚簇。K-means是一种无监督的学习模型。K-means的学习目标如下图所示: 2.策略 K-mean算法采用的损失函数是平方损失函数。每个簇的点距离中心的平方距离之和构成损失函数。3.算法 首先给出原始数据{x1,x2,…,xn},这些数据没有被标记的。初始化原创 2016-01-14 21:43:06 · 1074 阅读 · 0 评论 -
【Leetcode】之Combination Sum
一.问题描述Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited numbe原创 2016-01-13 11:07:11 · 339 阅读 · 0 评论 -
【机器学习算法】之线性回归分析
一.算法介绍1.模型:y=xw 线性回归建立的模型非常简单,就是简单的线性组合。 2. 策略 使用平方损失函数: 3.算法 直接改写成矩阵相乘形式,即可得到闭式解。 上述是最基本的线性回归的用法,但是在实际应用中,从最基本的线性回归中还可以引申出多种形式。局部加权线性回归: 参见博客 http://blog.csdn.net/silence1214/article/detail原创 2016-01-12 17:16:30 · 905 阅读 · 0 评论 -
【Leetcode】之Count and Say
一.问题描述The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 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原创 2016-01-11 13:22:35 · 478 阅读 · 0 评论 -
【Leetcode】之3Sum Closest
一.问题描述Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have ex原创 2015-11-03 09:18:20 · 289 阅读 · 0 评论 -
【Leetcode】之Longest Common Prefix
一.问题描述Write a function to find the longest common prefix string amongst an array of strings.二.我的解题思路这道题比较容易,只需对字符串vector从左向右进行遍历,每次将当前最长前缀字符串与遍历到的vector中的当前字符串进行比较即可。测试通过的程序如下:class Solution {public:原创 2015-11-01 10:57:53 · 260 阅读 · 0 评论 -
【Leetcode】之Reverse Nodes in k-Group
一.问题描述Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should rem原创 2015-11-18 10:28:44 · 284 阅读 · 0 评论 -
【Leetcode】之Reverse Integer
题目描述: Reverse digits of an integer.Example1: x = 123, return 321 Example2: x = -123, return -321我的解题思路: 这道题就是一套数学题,给定一个10进制整数,需要先求得个位,十位等各个位的数值,然后把整数给反转一下。这道题的考点应该是在于:int型的存储范围有限,应该如何应对有符号整数溢出的问题。查阅原创 2015-10-25 11:23:38 · 334 阅读 · 0 评论 -
【Leetcode】之Merge Two Sorted Lists
一.问题描述Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.二.我的解题思路给定两个有序的链表,要将这两个链表按序排成一个链表。链表的merge和数组的merge还是比原创 2015-11-10 10:04:13 · 284 阅读 · 0 评论 -
【Leetcode】之ZigZag Conversion
问题描述: The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H N A P L S原创 2015-10-24 10:59:49 · 368 阅读 · 0 评论