
刷题
文章平均质量分 70
honeyCR
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
剑指offer-顺时针打印矩阵
题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。 例如: 我们可以借助图形来帮助思考,如下图: 我们可以将打印分为一圈一圈的, 对于左图,6X6的矩阵,最后一圈有4个数字,左上角为(2,2),6>2X2; 对于右图,5X5的矩阵,最后一圈只有一个数字,左上角也为(2,2),5>2X2; 因此我们可将cols>srartX*2且rows&...原创 2018-06-07 19:24:04 · 411 阅读 · 0 评论 -
写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。
题目: 写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。禁止使用加减乘除计算的话,我们很容易就能想到用位运算。 第一步:我们试想,对于二进制相加,不考虑进位的话,如下: 0+1=1,1+0=1;1+1=0,0+0=0; 可以观察到,这和异或的结果是一样的。第二步:我们再来考虑进位的过程,上面的过程,只有1+1会产生进位,此时我们可以想象成两个数先做...原创 2018-07-11 21:48:13 · 1370 阅读 · 0 评论 -
LeetCode-Given a number represented as an array of digits, plus one to the number.
Given a number represented as an array of digits, plus one to the number.看题意,意思为: 给定一个以数字数组表示的数字,再加一。这道题目具体的意思为: 比如给定一个digits数组{1,2,3,4},那就组成了一个数字1234,把这个数+1=1235,用数组返回1235的结果。我们需要着重考虑的是如果结果位数...原创 2018-07-11 22:08:03 · 477 阅读 · 0 评论 -
LeetCode-Given n points on a 2D plane, find the maximum number of points that lie on the same straig
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line题意为:给定二维平面上的n个点,找到位于同一直线上的最大点数。这道题给了我们一堆二维点,然后让我们求最大的共线点的个数,根据初中数学我们知道,两点确定一条直线,而且可以写成y = ax + b的形...原创 2018-07-12 20:33:24 · 1971 阅读 · 2 评论 -
LeetCode-Given a collection of intervals, merge all overlapping 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].这次题目要求我们合并区间。 解法一:我们可以先给区间集排序,排完序我们就可以开始合并了。首先把第一个...原创 2018-07-12 21:26:52 · 608 阅读 · 0 评论 -
斐波那契数列&&变形题目
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。 这个问题很简单,若是用递归可能效率会很低,重复计算太多。 因此我们使用非递归自下而上计算,代码如下:class Solution {public: int Fibonacci(int n) { int f0=0; int f1=1; ...原创 2018-08-09 10:39:41 · 1561 阅读 · 0 评论 -
调整数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。 注意:此题要求保证数的相对位置不变 方法一:类似冒泡算法,前偶后奇数就交换:class Solution {public: void reOrderArray(vector<int> &arr...原创 2018-08-11 21:58:15 · 574 阅读 · 0 评论 -
设计模式简介
简介设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。 毫无疑问,设计模式于己于他人于系统都是多赢的,设计模式使代码编制真正工程化,设计模式是软件工程的基石,如同大厦的一块块砖石一样。设计模式分为三种类型,分别是:创建型模式、结构型模式,行为型模式。设...原创 2018-08-12 17:46:48 · 165 阅读 · 0 评论 -
当fork遇见for循环,printf后会怎样?
这段时间总看见这种题,今天总结一下。题1先看代码1:#include&lt;unistd.h&gt;#include&lt;stdio.h&gt;#include&lt;sys/types.h&gt;int main(){ printf("1 2 3"); fork();}代码2:#include&lt;unistd原创 2018-08-12 22:25:22 · 1129 阅读 · 0 评论 -
从1-n整数中1出现的次数
题目: 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1、10、11、12、13因此共出现6次,但是对于后面问题他就没辙了。ACMer希望你们帮帮他,并把问题更加普遍化,可以很快的求出任意非负整数区间中1出现的次数(从1 到 n 中1出现的次数)。解法一:class Solution {public: i...原创 2018-08-20 09:19:10 · 369 阅读 · 0 评论 -
和为S的连续正数序列
题目: 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!解析:我们要找和为S的连续正数序列,...原创 2018-08-27 15:15:17 · 171 阅读 · 0 评论 -
LeetCode-find the starting and ending position&&find index if the target is found
Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of O(log n). If the target is not found in t...原创 2018-07-04 21:46:29 · 322 阅读 · 0 评论 -
求1+2+3+…+n的和
题目: 求1+2+…n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。 这个题来自剑指offer面试题64,有兴趣的同学可以看一下,下面我将参考此书总结。按照正常的思维,除了用数学公式,解决这个问题我们会用循环或者递归,下面我们讲解如何用其他方法实现循环或递归的功能。解法一:利用构造函数求解 既然不让我们使用for...原创 2018-07-10 22:06:39 · 2520 阅读 · 0 评论 -
二叉树有关面试题
判断二叉树是否对称/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution {p...原创 2018-06-22 17:43:58 · 205 阅读 · 0 评论 -
有关栈的面试题
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序 假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。class Solution {public: bool IsPopOrder(vector<int> pu...原创 2018-06-22 17:55:57 · 433 阅读 · 0 评论 -
PAT-买珠子与旧键盘问题
买珠子小红想买些珠子做一串自己喜欢的珠串。卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖。于是小红要你帮忙判断一 下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子;如果不是,那么告诉她缺了多少珠子。为方便起见,我们用[0-9]、[a-z]、[A-Z]范围内的字符来表示颜色。 例如,YrR8RrY是小红想做的珠串,那么ppRYYGrrYBR2...原创 2018-07-01 12:02:06 · 265 阅读 · 1 评论 -
LeetCode-Given a collection of numbers, return all possible 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]. 采用递归, 在num中拿出1个数字放在第一个,然后...原创 2018-07-08 19:22:41 · 354 阅读 · 0 评论 -
LeetCode-Given a string containing only digits, returning all possible valid IP address
Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example: Given”25525511135”,return[“255.255.11.135”, “255.255.111.35”]. (Order does n...原创 2018-07-08 19:37:21 · 468 阅读 · 0 评论 -
LeetCode-Given two numbers represented as strings, return multiplication of the numbers as a string
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.题意:给定两个表示为字符串的数字,将数字的乘法返回为字符串。注:数字可以任意大...原创 2018-07-16 15:03:37 · 234 阅读 · 0 评论 -
LeetCode-Sort Color
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,...原创 2018-07-16 15:11:01 · 224 阅读 · 0 评论 -
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
矩阵置空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 O...原创 2018-07-03 20:57:43 · 595 阅读 · 0 评论 -
Given a 2D board and a word, find if the word exists in the grid.
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 vertically n...原创 2018-07-03 21:16:10 · 607 阅读 · 0 评论 -
青蛙跳台阶问题
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。这个问题要和斐波那契数列区分,因为它一次也可以跳n级,因此不能用斐波那契数列计算。 我们可以用数学来解释,如下: F(n) = F(n-1)+F(n-2)+…+F(1) F(n-1) = F(n-2)+F(n-3)+…+F(1) 两个式子相减,很容易得出F(n)=2F(n-1)...原创 2018-07-10 20:20:38 · 1315 阅读 · 0 评论 -
求按从小到大的顺序的第N个丑数
题目: 把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。解释: 首先从丑数的定义我们知道,一个丑数的因子只有2,3,5,那么丑数p = 2 ^ x * 3 ^ y * 5 ^ z,换句话说一个丑数一定由另一个丑数乘以2或者乘以3或者乘以5得到,所以下一个丑数...原创 2018-08-21 10:11:37 · 1939 阅读 · 0 评论