前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode上面Binary Tree的题刷了4遍,目前95%以上能够Bug Free)所以还能跟得上,今天听了一下,觉得学习到最多的,就是把Traverse和Divide Conquer分开来讨论,觉得开启了一片新的天地!今天写这个博客我就尽量把两种方式都写一写吧. Outline: 二叉树的遍历 前序遍历t…
this example is from chapter 4 in <the introduction to algorithm> the main idea is all showed in the book , i think maybe realizing the algorithm is a good way to understand it. /* from : introduction to algorithm chapter four algorithm:divide and c…
package max_subarrayy;import java.lang.Math;public class max_subarrayy { private static int[] array; public static class mark{ private int lom = 100; private int him; private int value; public mark(int a,int b,int c){ lom = a;him = b;value = c; } } p…
一. 1. Lowest Common Ancestor class Solution { public: TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *A, TreeNode *B) { if (root == NULL || root == A || root == B) { return root; } TreeNode* left = lowestCommonAncestor(root->left, A, B); Tre…
动态规划三大重要概念:最优子结构,边界,状态转移公式(问题规模降低,如问题由 n 的规模降低为 n−1 或 n−2 及二者之间的关系): 0. 爬台阶 F(n)⇒F(n−1)+F(n−2) F(n−1),F(n−2) 即是 F(n) 的最优子问题: F(1)=1,F(2)=2 是问题的边界: F(n)=F(n−1)+F(n−2) 则是问题的状态转移公式: 1. 数列快速求和和矩阵快速乘法 1+2+⋯+n==(1+⋯+n2)+((n2+1)+⋯+(n2+n2))2⋅(1+⋯+n2)+n2⋅n2 也…
叉积: 在平面中我们为了度量一条直线的倾斜状态,为引入倾斜角这个概念.而通过在直角坐标系中建立tan α = k,我们实现了将几何关系和代数关系的衔接,这其实也是用计算机解决几何问题的一个核心,计算机做的是数值运算,因此你需要做的就是把几何关系用代数关系表达出来.而在空间中,为了表示一个平面相对空间直角坐标系的倾斜程度,我们利用一个垂直该平面的法向量来度量(因为这转化成了描述直线倾斜程度的问题). 定义: 而向量的运算符合如下的代数定律. 显然这个定义式我们不怎么喜欢,因为它代数化程度还是太浅,…
两个n次多项式的相加最直接的方法所需要的时间是O(n),而实现两个n次多项式的乘法的直接方法则需要O(n^2),本章讨论的快速傅里叶变换(FFT),将会将这一过程的时间复杂度降至O(nlogn).同时本章也会给出一些FFT现实应用. 多项式的两种表示形式: 通过上面的推导,我们简单总结一下得到的结论. 而接下来问题的核心是,如果优化求值和插值过程的时间复杂度,求值过程直观的来看,时间复杂度是O(n^2),而插值过程需要解线性方程组,需要的时间复杂度更高. 为了算法的优化,我们需要引入一些复变函数…
algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { margin: 0px; padding: 0px; height: auto; bottom: 0px; top: 0px; left: 0px; right: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-s…
Algorithm in Practice Author: Zhong-Liang Xiang Date: Aug. 1st, 2017 不完整, 部分排序和查询算法, 需添加. Prerequisite 生成随机整数数组,打印数组, 元素交换. #include <stdlib.h> #include <iostream> #include <time.h> using namespace std; #define MAX 10 // 数组最大长度 void init…
1.Python数据结构篇 数据结构篇主要是阅读[Problem Solving with Python]( http://interactivepython.org/courselib/static/pythonds/index.html)时写下的阅读记录,当然,也结合了部分[算法导论]( http://en.wikipedia.org/wiki/Introduction_to_Algorithms)中的内容,此外还有不少wikipedia上的内容,所以内容比较多,可能有点杂乱.这部分主要是介…
Introduction Divide-and-Conquer的三个步骤: Divide the problem into a number of subproblems that are smaller instances of the same problem. Conquer the subproblems by solving them recursively. If the subproblem sizes are small enough, however, just solve t…
的确,正如偶像Bruce Eckel所说,"Life is short, you need Python"! 如果你正在考虑学Java还是Python的话,那就别想了,选Python吧,你的人生会有更多的时间做其他有意思的事情. 研究生之前我没学python是有原因的:首先,我怕蛇,很怕很怕,而这货的logo竟然就是蛇,我因故而避之:其次,我不喜欢脚本语言,我会shell,但是写的时候不是很爽,只是在处理些文件操作或者字符串操作的时候才会想起它,听说python脚本神马的,我便又避之.…
实python非常适合初学者入门,上手很容易.我就是完全通过网上资源学了python的.最大的是3点经验:1.找一本浅显易懂,例程比较好的教程,从头到尾看下去.不要看很多本,专注于一本.把里面的例程都手打一遍,搞懂为什么.2.去找一个实际项目练手.我当时是因为要做一个网站,不得已要学python.这种条件下的效果比你平时学一门新语言要好很多.所以最好是要有真实的项目做.可以找几个同学一起做个网站之类.3.最好能找到一个已经会python的人.问他一点学习规划的建议,然后在遇到卡壳的地方找他指点.…
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1               3            3             2             1 \          …
作者:Glowin链接:https://zhuanlan.zhihu.com/p/22881223来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 原文地址:Google Interview University 原文作者:John Washam 译文出自:掘金翻译计划 (翻译不易,欢迎 Star 支持) 译者:Aleen,Newton,bobmayuze,Jaeger,sqrthree 这是? 这是我为了从 web 开发者(自学.非计算机科学学位)蜕变至 Goog…
Awesome Courses  Introduction There is a lot of hidden treasure lying within university pages scattered across the internet. This list is an attempt to bring to light those awesome courses which make their high-quality material i.e. assignments, lect…
The pattern language is organized into four design spaces.  Generally one starts at the top in the Finding Concurrency design space and works down through the other design spaces in order until a detailed design for a parallel program is obtained. Cl…
Approach #1 Brute Force Intuition    We can exhaust the search space in quadratic time by checking whether each element is the majority element.Algorithm    The brute force algorithm iterates over the array, and then iterates again for each number to…
1.单向链表 实现思路:创建Node类,包括自己的数据和指向下一个:创建Node类,包括头尾节点,实现添加.删除.输出等功能. tips:n = n.next不破坏链表结果,而n.next = n.next.next就等于是n节点的next属性变成了再下一个,即指向n+1个节点的指针丢失,但实际上n+1节点仍在,只不过从链表中去除. 具体代码: public class NodeList<Integer> { class Node<Integer> { Node<Intege…
在日常的软件开发和使用过程中,我们发现同一套系统的同一配置项在不同的客户环境中是存在各种各样的差异的.在差异较为分散时,如何较好的管理这些差异,使得维护过程能够更加安全和快速,一直在这样那样的困扰着开发者和维护者. 例如,有系统中需要配置日志的记录路径和日志文件的命名方式.默认的日志是放在C盘目录下并以Log_XXX.txt进行命名. <?xml version=""1.0"" encoding=""utf-8""?&g…
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 这题和sort list等题都比较相似,需要先用快慢指针的方法找到链表的中点,然后用recursive的方式构建左子树和右子树(用到的思想是Divide&Conquer),然后再构建好这个节点. 编程时一点要注意: (1)dummy节点的使用可以帮助找到中点的prev节点 但是d…
There are 3 possible approaches: DP, divide&conquer and greedy. And apparently, DP has O(n^2) complexity (TLE), DivideConquer can only be worse. Greedy can be O(n)! And its code is amazingly brief: class Solution { public: bool isSubsequence(string s…
Count the number of occurrences in a sorted array Given a sorted array arr[] and a number x, write a function that counts the occurrences of x in arr[]. Expected time complexity is O(Logn) Examples: Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 2 Output…
各种排序算法: #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> #include <io.h> #include <math.h> #include <time.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define MAX_LEN…
1. 问题: The prime factors of 13195 are 5, 7, 13 and 29.What is the largest prime factor of the number 600851475143 ? 2. 解法(by java in Eclipse) package com.lun.alrithmetic; /* * Q1: what's the primary factor? (1 2 3) * Q2: i & i+2ne能否遍历出所有质数 */ public…
Two methods: 1. Traverse 2. Divide & Conquer // Traverse: usually do not have return value public class Solution { public void traverse(TreeNode root) { if (root == null) return; traverse(root.left); traverse(root.right); } } // Divide & Conquer:…
面试题3 -- 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 1. 每行中的整数从左到右是排序的. 2. 每行的第一个数大于上一行的最后一个整数. public class Solution { /** * @param matrix, a list of lists of integers * @param target, an integer * @return a boolean, indicate whether matrix contains…
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a "rotation function" F on A as follow: F(k…
1 - 从strStr谈面试技巧与代码风格 必做题: 13.字符串查找 要求:如题 思路:(自写AC)双重循环,内循环读完则成功 还可以用Rabin,KMP算法等 public int strStr(String source, String target) { if (source == null || target == null) { return -1; } char[] sources = source.toCharArray(); char[] targets = target.to…
Problem Statement:  Path sum i Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \…