![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法
永不放弃EX
这个作者很懒,什么都没留下…
展开
-
【Leetcode】:51. N-Queens 问题 in JAVA
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.原创 2016-04-25 19:16:59 · 2661 阅读 · 1 评论 -
选择算法-GoLang实现
选择算法中利用了快速排序的方法,达到了线性的时间复杂度RandomizedSelect函数返回切片a的下标p到r中,第i小的数,下面是用GO语言的实现主要思路和随机快排类似,不过随机快排需要对分割出的两个子序列进行处理,而选择算法只需要对一个进行处理实现了线性的复杂度package orderimport ( "math/rand" )//RandomizedSel原创 2016-04-10 21:38:31 · 869 阅读 · 0 评论 -
最大子数组问题-GoLang实现
在一个数组A中寻找一个最大的子数组,这个问题可以用分治策略来求解。首先需要划分问题,最大的子数组只有如下3中情况:情况1:最大子序列出现在A[low]~A[mid]情况2:最大子序列出现在A[mid+1]~A[high]情况3:最大子序列包含A[mid],A[mid+1]那么,问题的计算就可以分为对两个小的子序列求解,在加上一个复杂度为n的方法,那么整个问题的复杂度为nlgn原创 2016-03-31 16:55:29 · 932 阅读 · 0 评论 -
排序算法:Insertion Sort和Merge Sort in GoLang
最近在学习算法导论,课程上讲了两种排序算法,分别是插入排序法和合并选择法下面用GO语言实现package sort//InsertionSort 插入排序 n^2func InsertionSort(arr []int) { if len(arr) < 2 { return } for j := 1; j < len(arr); j++ { key := arr原创 2016-03-29 12:20:18 · 863 阅读 · 0 评论 -
【Leetcode】:337. House Robber III 问题 in JAVA
题目:The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. Aft原创 2016-05-01 16:03:09 · 342 阅读 · 0 评论 -
二叉搜索树实现 in Go语言
用Go实现了下二叉搜索树,还是花了不少时间,在实现中使用的是单向链表,这才算是体会到了双向链表在实现中的优势package datastructureimport ( "container/list" "fmt")type BSTree struct { root *Node}type Node struct { left *Node right *Node原创 2016-04-18 20:37:28 · 454 阅读 · 0 评论 -
【Leetcode】:319. Bulb Switcher 问题 in JAVA
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning of原创 2016-04-21 10:14:19 · 386 阅读 · 0 评论 -
【Leetcode】:94. Binary Tree Inorder Traversal 问题 in JAVA
题目难度中等,就是要求不用递归的情况下进行中序遍历:中序遍历就是先访问左孩子的值,再访问父节点的值,最后访问右孩子的值。这样的话,一棵树的最最左边的子节点的值一定是第一个被访问的所以实现起来的思路就是,沿着树一直向左走,直到走不动为止,这个时候再看看当前节点是否有右节点,如果有的话对该又节点进行前面的操作。/** * Definition for a binary tree原创 2016-04-24 12:21:42 · 538 阅读 · 0 评论 -
【Leetcode】:326. Power of Three 问题 in JAVA
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly sinc原创 2016-05-09 12:17:41 · 379 阅读 · 0 评论 -
算法导论习题:10.3-4 in JAVA
算法导论课后习题10.3-4:我们往往希望双向链表的所有元素在存储器中保持紧凑,例如,在多数组表示中占用前m个下标位置。import java.util.Scanner;public class ans10_3_4 { public static void main(String[] args){ Scanner in=new Scanner(System.in); Sy原创 2016-04-17 16:41:41 · 787 阅读 · 0 评论 -
【Leetcode】:Move Zeroes问题 in JAVA
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling you原创 2016-04-14 21:19:17 · 420 阅读 · 0 评论 -
【Leetcode】:Single Number II问题 in Go语言
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without usi原创 2016-04-13 18:09:51 · 344 阅读 · 0 评论 -
【Leetcode】:Single Number III问题 in Go语言
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.For example:Given原创 2016-04-12 21:25:30 · 483 阅读 · 0 评论 -
【Leetcode】:287. Find the Duplicate Number 问题 in JAVA
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number,原创 2016-04-27 15:41:28 · 360 阅读 · 0 评论 -
【Leetcode】:Counting Bits问题 in Go语言
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.Example:For num = 5原创 2016-04-11 11:25:29 · 660 阅读 · 0 评论 -
【Leetcode】:230. Kth Smallest Element in a BST 问题 in JAVA
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up:What if t原创 2016-04-28 22:55:47 · 312 阅读 · 0 评论 -
【Leetcode】:116. Populating Next Right Pointers in Each Node 问题 in JAVA
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there i原创 2016-05-11 13:03:48 · 541 阅读 · 0 评论 -
算法导论习题:10.2-7 in Go语言
10.2-7题:给出一个时间复杂度为O(n)的非递归过程,实现对一个含n个元素的单链表的逆转。程序的主要思想就是,转变指标的指向,例如原本是1->2->3->4,现在变成1package mainimport ( "fmt")type Node struct { next *Node data int}type List struct {原创 2016-04-16 18:16:22 · 744 阅读 · 0 评论 -
【Leetcode】:171. Excel Sheet Column Number 问题 in Go语言
Related to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ...原创 2016-04-17 20:28:57 · 406 阅读 · 0 评论 -
【Leetcode】:242. Valid Anagram问题 in Go语言
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.Note:You may ass原创 2016-04-16 12:23:51 · 580 阅读 · 0 评论 -
简单的遗传算法(Genetic algorithms)-吃豆人
遗传算法简介:一直都在收听卓老板聊科技这个节目,最近播出了一起人工智能的节目,主要讲的是由霍兰提出的遗传算法,在目中详细阐述了一个有趣的小实验:吃豆人。首先简单介绍下遗传算法: 1:为了解决某个具体的问题,先随机生成若干个解决问题的实体,每个实体解决问题的方式都用“基因”来表示,也就是说,不同的实体拥有不同的基因,那么也对应着不同的解决问题的方案。 2:有了若干实体之后,接下来就是让这些实体来完原创 2016-12-05 20:52:39 · 10216 阅读 · 11 评论