![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法
文章平均质量分 80
LearnHowToFly
这个作者很懒,什么都没留下…
展开
-
Implement Stack using Queues && Implement Queue using Stacks (LeetCode)
Implement Stack using Queues 即使用队列来实现栈 方法:创建两个队列,始终保持一个队列为空。添加数据时,添加到空队列,然后将另一个队列加入到该队列中,这样就保证了最后添加的数据处于队列的队首(即满足栈的先进后出、后进先出的要求);取数据时,从非空队列使用队列的方法中取出的数据即为所求 具体代码如下: class MyStack { //始终保持一原创 2015-07-26 17:44:29 · 512 阅读 · 0 评论 -
Two Sum (LeetCode)
题目:这个函数twosum应该返回两个数字,他们总计达目标指数,其中指数必须小于指数。请注意,你的答案返回(包括指数和指数)不为零的基础。 解法一: 解法一虽然能够解决这个问题,但是算法的时间复杂度为O(N^2),有些愚蠢 运行时间如下所示: 解法二: 解法二的时间复杂度为O(N),运行时间如下所示 解法二的思路是:循环遍历数组,每次都判断当原创 2015-07-09 22:44:36 · 318 阅读 · 0 评论 -
Count Primes(LeetCode)
Description: Count the number of prime numbers less than a non-negative number, n. 即求小于给定的正整数n的所有素数的个数 方法一:普通求解法,即从2开始判断每一个小于你的正整数k是否是素数,用k依次对2到根号k之间的所有整数取余,如果余数为0则k为非素数,否则为素数 public int count原创 2015-07-25 17:02:20 · 310 阅读 · 0 评论 -
Number of 1 Bits(LeetCode)
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 000000000原创 2015-07-25 21:29:24 · 302 阅读 · 0 评论 -
[leetcode] permutations的讨论
原题是找到一组数的全排列 Given a collection of 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]转载 2015-08-02 20:30:41 · 317 阅读 · 0 评论 -
Rotate Image (LeetCode)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). 即将一个n * n的二维数组顺时针旋转90度(旋转结果:即将第一行搬至倒数第一列,第二行搬至倒数第二列,依次搬移数组) 方法一:创建一个同等大小的二维数组,将每次搬移后的结果保存到新数组中原创 2015-08-02 22:02:31 · 414 阅读 · 0 评论 -
LeetCode问题 pow(x,n)
实现浮点类型的幂运算,函数原型为: double pow(double x, int n) 下面介绍一下解决该问题的几种方法以及要注意的地方: 1)最直观容易想到的方法就是用递归方法求n个x的乘积,注意考虑n的正负号,时间复杂度为O(n) [cpp] view plaincopy double pow(double x, int n) { if(n==0)转载 2015-08-03 21:48:13 · 362 阅读 · 0 评论