算法
文章平均质量分 55
路漫远吾求索
电子,无线局域网
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
一道面试题
一道面试题:求一棵树中任意两个结点的最远距离。思想:利用递归,在求深度的同时顺带把最远距离算出来。struct node{ vector<node*> child; vector<int> distance;}int MaxDepth(node * root, int *MaxSum){ if (root == NULL) ...原创 2019-10-31 21:25:23 · 292 阅读 · 0 评论 -
文章标题
DescriptionThere are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint原创 2017-02-21 16:23:24 · 297 阅读 · 0 评论 -
Leetcode——198. House Robber
Descriptionhttps://leetcode.com/problems/house-robber/?tab=Description You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only c原创 2017-02-21 16:57:42 · 339 阅读 · 0 评论 -
Leetcode——400. Nth Digit
题目Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...Note:n is positive and will fit within the range of a 32-bit signed integer (n < 231).Example 1:Input:3Outp原创 2017-01-23 17:24:06 · 339 阅读 · 0 评论 -
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) = 0 * B原创 2017-01-23 18:23:04 · 387 阅读 · 0 评论 -
Leetcode——313. Super Ugly Number
题目Write a program to find the nth super ugly number.Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16原创 2017-01-24 00:58:37 · 407 阅读 · 0 评论 -
Leetcode——264. Ugly Number II
题目Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ug原创 2017-01-24 18:20:52 · 521 阅读 · 0 评论 -
Leetcode——53. Maximum Subarray
DescriptionFind the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,原创 2017-02-14 13:40:56 · 346 阅读 · 0 评论 -
Leetcode——142. Linked List Cycle II
题目Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up: Can you solve it without using extra space?原题: https://原创 2017-01-26 00:13:41 · 364 阅读 · 0 评论 -
Leetcode——276.Paint Fence
DescriptionThere is a fence with n posts, each post can be painted with one of the k colors.You have to paint all the posts such that no more than two adjacent fence posts have the same color.Return th原创 2017-02-21 16:01:19 · 1176 阅读 · 0 评论 -
Leetcode——67. Add Binary
问题Given two binary strings, return their sum (also a binary string).For example, a = “11” b = “1” Return “100”.解答*class Solution {public: string addBinary(string a, string b) { int i=0;原创 2017-01-23 00:12:10 · 328 阅读 · 0 评论 -
Leetcode——385. Mini Parser
DescriptionGiven a nested list of integers represented as a string, implement a parser to deserialize it.Each element is either an integer, or a list – whose elements may also be integers or other list原创 2017-02-08 22:57:11 · 556 阅读 · 0 评论 -
vector
vectora; 不能直接用a[0]=1; 直接操作下标是不对的,两张方式: a.resize(100);先指定足够大的空间 然后a[0]=…另一种方式 a.push_back(1)…..substr 方法: 返回一个从指定位置开始,并具有指定长度的子字符串。 参数 start 必选。所需的子字符串的起始位置。字符串中第一个字符的索引为 0。 length原创 2017-02-09 16:57:39 · 339 阅读 · 0 评论 -
Leetcode——240. Search a 2D Matrix II
descriptionWrite an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted in ascending from left to right. Integers原创 2017-02-18 19:20:19 · 355 阅读 · 0 评论 -
Leetcode——241. Different Ways to Add Parentheses
descriptionGiven a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *.Example原创 2017-02-18 23:37:45 · 398 阅读 · 0 评论 -
Leetcode——453. Minimum Moves to Equal Array Elements
题目Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.Example:Input:[1,2,3]Output:3原创 2017-01-21 16:04:49 · 326 阅读 · 0 评论 -
Leetcode——326. Power of Three
题目Given an integer, write a function to determine if it is a power of three. Follow up: Could you do it without using any loop / recursion?解答Method1:循环或者递归public boolean isPowerOfThree(int n) { r原创 2017-01-21 20:27:38 · 266 阅读 · 0 评论 -
Leetcode——171. Excel Sheet Column Number
题目https://leetcode.com/problems/excel-sheet-column-number/解答其实就是26进制转化为十进制class Solution {public: int titleToNumber(string s) { int res=0; for(int i=0;i<s.length();i++) {原创 2017-01-21 23:48:40 · 269 阅读 · 0 评论 -
Leetcode——231. Power of Two
回答Given an integer, write a function to determine if it is a power of two.解答class Solution {public: bool isPowerOfTwo(int n) { int MaxPowerOfTwo=pow(2,(int)(log10(INT_MAX)/log10(2)));原创 2017-01-22 21:02:07 · 324 阅读 · 0 评论 -
Leetcode——61. Rotate List
题目Given a list, rotate the list to the right by k places, where k is non-negative.For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL.解答One Solution:/** * Definition for sin原创 2017-01-26 00:55:41 · 381 阅读 · 0 评论 -
Leetcode——75. Sort Colors
题目:Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0,原创 2017-01-26 14:38:54 · 405 阅读 · 0 评论 -
求最长连续线段长
循环int BinSearch(int a[], int n, int key){ int low = 0, high = n - 1, mid; while (low <= high) { if (a[low] == key) return low; if (a[high] == key) r原创 2017-03-25 01:33:54 · 2592 阅读 · 0 评论 -
单源最短路径——Dijkstra算法
原理解析http://blog.csdn.net/wizard_wsq/article/details/51095853小技巧总结图用邻接矩阵表示 Distance[i]表示v0到顶点i的距离 int prev[i]表示节点v0到节点i存在最短路径时,其前一个节点。Dijkstra代码//Dijkstra算法函数,求给定顶点到其余各点的最短路径//参数:邻接矩阵、出发点的下标、结果数组、路径前原创 2017-04-01 12:12:02 · 803 阅读 · 0 评论 -
八皇后问题求解
题目原文:Write an algorithm to print all ways of arranging eight queens on a chess board so that none of them share the same row, column or diagonal.译文:经典的八皇后问题,即在一个8*8的棋盘上放8个皇后,使得这8个皇后无法互相攻击( 任意2个皇后不能处于同一原创 2017-03-15 21:46:44 · 602 阅读 · 0 评论 -
链表排序
插入排序//需要注意的是传入的是二级指针,这样才会对链表内容进行修改。#pragma once#include<iostream>using namespace std;struct Node{ int data; Node *next; Node(int x) { data = x;next = NULL; }};void insert_operation(No原创 2017-04-16 00:21:35 · 620 阅读 · 0 评论 -
已知中序遍历和前序遍历,求后序遍历
已知中序遍历和前序遍历,求后序遍历思路前序遍历:根 左 右 中序遍历:左 根 右 寻找根,然后对于中序遍历,根的左半部分和前序遍历的根后面的左那部分 又重新构成一个相同的子问题,递归求解即可。Code#pragma once#include<iostream>#include<stack>#include<queue>using namespace std;typedef struct原创 2017-04-20 20:49:49 · 962 阅读 · 0 评论 -
快排-单链表实现
/************************************************************************* > File Name: main.cpp > Author: > Mail: > Created Time: 三 7/26 18:43:04 2017 *****************************原创 2017-07-26 20:28:57 · 779 阅读 · 0 评论 -
KMP算法最浅显理解——一看就明白
说明KMP算法看懂了觉得特别简单,思路很简单,看不懂之前,查各种资料,看的稀里糊涂,即使网上最简单的解释,依然看的稀里糊涂。 我花了半天时间,争取用最短的篇幅大致搞明白这玩意到底是啥。 这里不扯概念,只讲算法过程和代码理解:KMP算法求解什么类型问题字符串匹配。给你两个字符串,寻找其中一个字符串是否包含另一个字符串,如果包含,返回包含的起始位置。 如下面两个字符串:c...原创 2017-02-07 17:41:08 · 331072 阅读 · 185 评论 -
Dijkstra算法
介绍思路的https://blog.csdn.net/a_big_pig/article/details/44163903介绍怎么存路径的:https://www.cnblogs.com/smile233/p/8303673.html伪代码规范:https://www.cnblogs.com/lmystar/p/10635703.html比较详细的:https://blog.csdn.net...原创 2019-09-07 21:11:55 · 268 阅读 · 0 评论 -
二分查找
循环int BinSearch(int a[], int n, int key){ int low = 0, high = n - 1, mid; while (low <= high) { if (a[low] == key) return low; if (a[high] == key) r原创 2017-03-25 00:30:19 · 443 阅读 · 0 评论 -
Leetcode——86. Partition List
题目Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of th原创 2017-01-26 19:25:20 · 324 阅读 · 0 评论 -
Leetcode——80. Remove Duplicates from Sorted Array II
题目Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with the first five elemen原创 2017-01-26 22:59:43 · 329 阅读 · 0 评论 -
彷徨
今天看了知乎上一些搞ACM的分享,瞬间心情不好了,自己太弱了!!! 赶紧补补知识,要不然要跪要跪!!!原创 2017-01-26 23:33:01 · 429 阅读 · 0 评论 -
Leetcode——287. Find the Duplicate Number
题目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, f原创 2017-01-27 15:56:53 · 393 阅读 · 0 评论 -
大数乘法、加法、减法、除法
乘法(1)转换并反转,字符串转换为数字并将字序反转;(2)逐位相乘,结果存放在result_num[i+j]中;(3)处理进位,消除多余的0;(4)转换并反转,将计算结果转换为字符串并反转。除法就是一直减,如果被除数远远大于除数,那么运行时间特别长。应该有更好的方法。加法、减法减法思路类似加法,加法思路就是考虑进位的影响。代码#include<iostream>using namespace st原创 2017-03-28 22:13:22 · 487 阅读 · 0 评论 -
数组的连续子数组之和最大值,子数组最大乘积,二维数组子数组之和
求数组的子数组之和的最大值动态规划思想:时间复杂度为O(N),空间复杂度为O(N)和O(1)的两种解法int MaxSum(int *A, int n){ int *All = new int[n];//All[i]表示从i开始的,最大子数组之和,可能包含A[i] int *start = new int[n];//start[i]表示以i开头的最大子数组之和,包含i st原创 2017-03-31 00:07:27 · 643 阅读 · 0 评论 -
求数组中最长递增子序列,数组分割,数组循环移位,区间重合判断
求数组中最长递增子序列编程之美2.16 注意,所谓的递增子序列不一定是连续的 只要满足递增性即可。 方法:动态规划思想。 用dp[i]表示到i为止,最长的递增序列长度。(可能包含i,也可能不包含i) 那么dp[i+1]就是判断A[i+1]能否加上,即 dp[i+1]=max(dp[0~i]+A[0~i]int calculong(int *A, int n){ int *dp原创 2017-03-31 22:45:22 · 591 阅读 · 0 评论 -
图的BFS和DFS之C++实现
图的创建手动输入,并以节点数作为输入结束标志从文件读取,文件的从第二行开始,每一行结束都要有节点数作为结束 图的存储:vector< list<int> > graph;图的BFS非递归实现,借助队列void bfs(int v)//以v开始做广度优先搜索(非递归实现,借助队列){ list<int>::iterator it; visited[v] = true;原创 2017-04-01 10:26:55 · 10631 阅读 · 0 评论 -
Leetcode——258. Add Digits
题目Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit,原创 2017-01-18 00:00:22 · 315 阅读 · 0 评论 -
Leetcode——172. Factorial Trailing Zeroes
题目Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.解析class Solution {//0来自于5*2,因为2的个数总是大于等于5,所以有多少个零就看有多少个5public: int tr原创 2017-01-17 23:20:39 · 363 阅读 · 0 评论
分享