![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法
文章平均质量分 77
PointbreakLALALA
这个作者很懒,什么都没留下…
展开
-
LeetCode 题解(1):Valid Palindrome
题目:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is n原创 2013-12-08 15:22:23 · 881 阅读 · 0 评论 -
LeetCode 题解(2):Evaluate Reverse Polish Notation
题目:Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -,*, /. Each operand may be an integer or another expression.Some examples: ["2", "原创 2013-12-08 17:16:42 · 1193 阅读 · 0 评论 -
LeetCode 题解(3):Merge Intervals
题目:Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].题解:/** * Definition for an interval. * stru原创 2013-12-09 14:39:55 · 638 阅读 · 0 评论 -
LinkedIn 面试题 (2013.12.09):部分排序数组找值
题目:整数数组,部分排序,如 [9, 11, 1, 3, 6, 7], 给定某个query,如果query存在数组,返回index,否则返回-1。题解:class Solution {public: int FindInteger(int* array, const int query, const int start, const int end) { int原创 2013-12-10 09:24:21 · 1274 阅读 · 0 评论 -
换邮票 算法 动态规划
#include #include #include #include using namespace std;/// /// Facilitates dispensing stamps for a postage stamp machine./// class StampDispenser{public: /// /// Initializes a n原创 2013-12-04 14:42:02 · 1651 阅读 · 0 评论 -
QuickSort C++
//// main.cpp// Algorithm_rehersal//// Created by FENGLINAN on 13-12-2.// Copyright (c) 2013年 FENGLINAN. All rights reserved.//#include#include #include #include #include #include #include #include原创 2013-12-03 14:20:22 · 770 阅读 · 0 评论 -
LeetCode 题解(5):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 inte原创 2014-01-23 09:24:02 · 720 阅读 · 0 评论 -
LeetCode 题解(6):Spiral Matrix
题目:Given a matrix of m x n elements (m rows,n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]原创 2014-01-25 04:34:08 · 554 阅读 · 0 评论 -
LeetCode 题解(7):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 const原创 2014-01-25 16:07:15 · 599 阅读 · 0 评论 -
某公司面试题:连续子数组的最大和
题目:输入一个整型数组,元素有整有负,如{1,-2,3,10,-4,7,2,-5}。求连续子数组和的最大值,时间复杂度O(n)。题解:遍历数组元素并求和,用变量maximum记录出现过的和的最大值,用curMaximum记录当前和的最大值,当curMaximum小于0时,curMaximum+下一个元素 maximum,修改maximum为curMaximum。class原创 2014-01-29 04:21:47 · 552 阅读 · 0 评论 -
题解:把数组排成最小的数
题目摘自《剑指Offer》题目:输入一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最小的一个。例如输入数组{32, 321},则输出这两个能排成的最小数字32132。思路:1. 对数组进行排序,理应处在高位的数排序后应位于数组的前端,处在低位的数排序后位于数组的后端。设数组中两个数为a和b,若ab b,但ab = 101 2. 这里的陷阱是原创 2014-01-29 14:25:27 · 392 阅读 · 0 评论 -
Google经典面试题:求从1到n的n个整数中,字符“1”出现的个数
题目:输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数。例如输入12,从1到12这些整数中包含1的数字有1,10,11和12, 1一共出现了5次。题解:算法:以整数“n = 13254”为例,个位出现1的次数=n/10^1×10^0 + (若个位 = 0, 则 0;个位 > 1, 则1; 若个位 = 1,低0位+1); =》 13254/10 + 1原创 2014-01-29 06:47:56 · 773 阅读 · 0 评论 -
Google Mock Interview:有序的循环列表插入节点
题目:给定一个有序的循环链表,在其中插入一个值,保持该循环链表依然有序。题解:需要考虑的情况有:1. 初始链表为空链表,创建新节点,current->next = current,返回current。2. 若插入值介于最大值和最小值之间,即 current->value next->value,找到current并插在其后。3. 若插入值比最大值大,或比最小值小,找到转原创 2014-02-06 08:06:04 · 579 阅读 · 0 评论 -
Dijkstra Shortest Path
#include #include #include #include #include #include using namespace std;typedef int vertex_t;typedef double weight_t;struct edge { vertex_t target; weight_t weight; edge( vertex_t v原创 2014-02-07 05:27:48 · 792 阅读 · 0 评论 -
LinkedIn 面试题:字符串的全排列(Permutation)和组合(Combination)
题目:输入一个字符串,如"abc",打印该字符串所有可能的排列,如"abc","acb","bac","bca","cab","cba"。思路:从字符串的第一个字符开始,如"a",分别和原字符串的各位字符做交换,如aa = abc, ab = bac, a c = cba。递归。解法:class Solution {public: void stringPermutatio原创 2014-01-28 05:48:15 · 2410 阅读 · 0 评论 -
LeetCode 题解(10):Word Search
题目:Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or verti原创 2014-02-07 10:26:16 · 583 阅读 · 0 评论 -
Cracking the Code Interview (1): 跟踪数组的中位数
题目:随机生成一些数字,并保存到一个(可扩展的)数组中,如何跟踪数组的中位数?题解:用multiset分别实现最大堆和最小堆,最大堆保存数组的前半部分,最小堆保存数组的后半部分,每次添加新数字时和堆顶元素比较。同时保证两个堆的元素个数之差不超过1.//// main.cpp// Combination//// Created by on 14-2-3.// Co原创 2014-02-04 14:40:43 · 706 阅读 · 0 评论 -
《Programming Interviews Exposed》 中的一道递归题:Telephone number to words
题目:电话键盘上通常一个数字会对应几个字母,如2->"ABC",3->“DEF”,等等,给定一个电话号码,输出所有可能的字母组合。如"866-2665"的一种字母组合是"TOOCOOL",要求输出全部。解法:与求前排列的递归类似。#include #include #include using namespace std;class Solution {public原创 2014-02-10 04:48:14 · 507 阅读 · 0 评论 -
排序算法粗略总结
以整数数组递增排序为例:1. 选择排序 selection sort从第一个元素开始,在全数组里寻找最小的数,和第一个元素交换;前进至第二个元素,在第二个元素为起始的余下数组中寻找最小的数,和第二个元素交换;可以递归实现:SelectionSortRecursion( data, start ){ if( start { swap原创 2014-02-10 09:46:23 · 564 阅读 · 0 评论 -
经典汉诺塔问题
#include #include #include #include using namespace std;class Tower {public: stack tower; int index; Tower(int index_in):index(index_in){} void addElement(int e) { if(!tower.empty())原创 2014-02-24 09:34:19 · 469 阅读 · 0 评论 -
LeetCode 题解(14):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原创 2014-02-24 13:47:26 · 796 阅读 · 0 评论 -
LeetCode 题解(16): Simplify Path
题目:Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"Corner Cases:Did you consider the case where pa原创 2014-02-24 17:07:27 · 679 阅读 · 0 评论 -
LeetCode 题解(17): 4Sum
题目:Given an array S of n integers, are there elementsa, b, c, and d in S such that a +b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:El原创 2014-02-25 05:46:44 · 657 阅读 · 0 评论 -
LeetCode 题解(18): String to Integer (atoi)
题目:Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible inp原创 2014-02-25 14:02:18 · 550 阅读 · 0 评论 -
LeetCode 题解(19): Scramble String
题目:Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 ="great": great / \原创 2014-02-26 09:30:02 · 636 阅读 · 0 评论 -
LeetCode 题解(20): Balanced Binary Tree
题目:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofevery node never原创 2014-02-27 06:16:02 · 640 阅读 · 0 评论 -
LeetCode 题解(22): Clone Graph
题目:Clone an undirected graph. Each node in the graph contains alabel and a list of its neighbors.题解:按BFS遍历 Old Graph,创建Map保存Old Graph到New Graph的节点的映射,每当从队列中弹出一个元素并处理其neighbor时,先在map中寻找是否已存在该邻居原创 2014-02-27 08:44:00 · 634 阅读 · 0 评论 -
LeetCode 题解(11):Triangle
题目:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4],原创 2014-02-15 06:57:33 · 616 阅读 · 0 评论 -
LeetCode 题解(21): Set Matrix Zeros
题目:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A straight forward solution using原创 2014-02-27 07:07:44 · 890 阅读 · 0 评论 -
LeetCode 题解(23): Single Number II
题目:Given an array of integers, every element appearsthree times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it witho原创 2014-02-27 09:20:46 · 679 阅读 · 0 评论 -
LeetCode 题解(12):Maximum Depth of Binary Tree
题目:Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.题解:基础中的基础。/** * Definition原创 2014-02-15 07:09:02 · 506 阅读 · 0 评论 -
LeetCode 题解(13):Regular Expression Matching
题目:Implement regular expression matching with support for'.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire in原创 2014-02-15 17:00:41 · 672 阅读 · 0 评论 -
LeetCode 题解(28): 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.题解:题目说非负数便不用考虑正负号的问题了。写的有点长,主要是先原创 2014-02-28 08:52:00 · 626 阅读 · 0 评论 -
LeetCode 题解(32): Binary Tree Zigzag Level Order Traversal
题目:Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given bi原创 2014-02-28 18:03:59 · 649 阅读 · 0 评论 -
LeetCode 题解(25): Remove Nth Node From End of List
题目:Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end原创 2014-02-28 04:51:18 · 460 阅读 · 0 评论 -
LeetCode 题解(30): Rotate Image
题目:You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?题解:如此简单的一道题竟然搞了好多错误。一个是offset要注意,一个是内循环次数要注意。原创 2014-02-28 14:28:42 · 633 阅读 · 0 评论 -
LeetCode 题解(33): Binary Tree Inorder Traversal
题目:Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Note: Recursive solution i原创 2014-02-28 18:12:14 · 661 阅读 · 0 评论 -
LeetCode 题解(34): Insert Interval
题目:Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.原创 2014-03-01 03:51:26 · 713 阅读 · 0 评论 -
LeetCode 题解(26): Add Two Numbers
题目:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as原创 2014-02-28 05:16:04 · 622 阅读 · 0 评论 -
LeetCode 题解(35): Interleaving String
题目:Given s1, s2, s3, find whethers3 is formed by the interleaving of s1 and s2.For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc",原创 2014-03-01 05:17:54 · 628 阅读 · 0 评论