
c++
liveway6
这个作者很懒,什么都没留下…
展开
-
剑指offer:剪绳子
题目描述给你一根长度为n的绳子,请把绳子剪成m段(m、n都是整数,n>1并且m>1),每段绳子的长度记为k[0],k[1],…,k[m]。请问k[0]xk[1]x…xk[m]可能的最大乘积是多少?例如,当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三段,此时得到的最大乘积是18。解题思路找规律链接:https://www.nowcoder.com/questionT...原创 2019-10-09 00:35:51 · 549 阅读 · 0 评论 -
剑指offer:求1+2+3+...+n
题目描述求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。解题思路短路计算调用递归。class Solution {public: int Sum_Solution(int n) { int ans=n; // &&在c++中如果前面为假则后面的...原创 2019-09-15 22:30:30 · 162 阅读 · 0 评论 -
剑指offer:孩子们的游戏(圆圈中最后剩下的数)
题目描述每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0…m-1报数…这样下去…直到剩下最后一...原创 2019-09-14 12:44:05 · 187 阅读 · 0 评论 -
剑指offer:扑克牌顺子
题目描述LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张_)…他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子…LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张...原创 2019-09-14 11:34:44 · 114 阅读 · 0 评论 -
剑指offer:翻转单词顺序列
题目描述牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?解题思路暴力求解,一个...原创 2019-09-14 00:32:01 · 110 阅读 · 0 评论 -
剑指offer:左旋转字符串
题目描述汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!解题思路暴力求解class Solution {public: string Lef...原创 2019-09-13 23:53:53 · 128 阅读 · 0 评论 -
剑指offer:和为S的两个数字
题目描述输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。解题思路class Solution {public: vector<int> FindNumbersWithSum(vector<int> array,int sum) { vector<int>...原创 2019-09-13 23:25:31 · 84 阅读 · 0 评论 -
剑指offer:和为S的连续正数序列
题目描述小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!解题思路剑指offer的解法class ...原创 2019-09-13 22:43:48 · 109 阅读 · 0 评论 -
剑指offer:数组中只出现一次的数字
题目描述一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。解题思路用容器存次数。class Solution {public: void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) { if(data.size()>0){ ...原创 2019-09-13 17:19:53 · 87 阅读 · 0 评论 -
剑指offer:平衡二叉树
题目描述输入一棵二叉树,判断该二叉树是否是平衡二叉树。解题思路在二叉树的深度上求,分别求其左右子树的深度,如果深度差值小于1则为平衡二叉树。从上而下class Solution {public: int TreeDepth(TreeNode* pRoot) { if(pRoot==NULL) return 0; int leftn = T...原创 2019-09-13 17:02:23 · 103 阅读 · 0 评论 -
剑指offer:二叉树的深度
题目描述输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。解题思路递归做法/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), ri...原创 2019-09-13 16:36:28 · 112 阅读 · 0 评论 -
剑指offer:数字在排序数组中出现的次数
题目描述统计一个数字在排序数组中出现的次数。解题思路暴力求解,没用到排序的已知条件。class Solution {public: int GetNumberOfK(vector<int> data ,int k) { if(data.size()==0) return 0; map<int, int> res; ...原创 2019-09-13 16:13:21 · 87 阅读 · 0 评论 -
剑指offer:两个链表的第一个公共结点
题目描述输入两个链表,找出它们的第一个公共结点。解题思路/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { }};*/class Solution {public: // 遍历链表得到其长度 unsigned int GetL...原创 2019-09-13 15:45:38 · 106 阅读 · 0 评论 -
剑指offer:不用加减乘除做加法
题目描述写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。解题思路class Solution {public: int Add(int num1, int num2) { int sum, carry; do{ sum=num1^num2;//异或相加,不考虑进位 ...原创 2019-09-15 22:46:18 · 108 阅读 · 0 评论 -
剑指offer:把字符串转换成整数
题目描述将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能,但是string不符合数字要求时返回0),要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0。解题初始版class Solution {public: int StrToInt(string str) { if(str.length()==...原创 2019-09-15 23:35:09 · 104 阅读 · 0 评论 -
剑指offer:机器人的运动范围
题目描述地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?解题思路class Solution {...原创 2019-10-09 00:09:25 · 187 阅读 · 0 评论 -
剑指offer:矩阵中的路径
题目请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩...原创 2019-10-09 00:08:08 · 133 阅读 · 0 评论 -
剑指offer:对称的二叉树
题目描述请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。解题递归做法/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), ...原创 2019-09-22 12:34:12 · 117 阅读 · 0 评论 -
剑指offer:二叉树的下一个节点
题目描述给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。/*struct TreeLinkNode { int val; struct TreeLinkNode *left; struct TreeLinkNode *right; struct TreeLinkNode *n...原创 2019-09-22 12:15:00 · 112 阅读 · 0 评论 -
剑指offer:删除链表中重复的结点
题目描述在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5解题非递归做法/*struct ListNode { int val; struct ListNode *next; ListNode(i...原创 2019-09-22 11:33:15 · 115 阅读 · 0 评论 -
剑指offer:链表中环的入口结点
题目描述给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。解题快慢指针,fast走两步,slow走一步,当他们相遇时,fast走了一圈环,设环为n个节点,此时slow走的节点为x,则fast走x+n=2x,则有x=n。此时再令一个节点等于pHead和slow一起一次走一步,当他们相遇时,他们的节点为环的入口。/*struct ListNode { in...原创 2019-09-19 15:53:24 · 100 阅读 · 0 评论 -
剑指offer:字符流中第一个不重复的字符
题目描述请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。解题思路class Solution{public: string s; char hash[256]={0}; //Insert one char ...原创 2019-09-17 13:09:31 · 118 阅读 · 0 评论 -
剑指offer:表示数值的字符串
题目描述请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100",“5e2”,"-123",“3.1416"和”-1E-16"都表示数值。 但是"12e",“1a3.14”,“1.2.3”,"±5"和"12e+4.3"都不是。解题class Solution {public: bool isNumeric(char* string) { ...原创 2019-09-17 11:36:22 · 96 阅读 · 0 评论 -
剑指offer:正则表达式匹配
题目描述请实现一个函数用来匹配包括’.‘和’‘的正则表达式。模式中的字符’.‘表示任意一个字符,而’'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"abaca"匹配,但是与"aa.a"和"ab*a"均不匹配解题思路class Solution {public: bool match(char*...原创 2019-09-17 11:20:45 · 113 阅读 · 0 评论 -
快手笔试题
矩阵逆时针打印#include <iostream>#include <cstdio>#include <vector>#include <map>#include <string>using namespace std;void ReverseWord(string &str, int l, int r){...原创 2019-09-17 10:56:57 · 688 阅读 · 0 评论 -
剑指offer:构建乘积数组
题目描述给定一个数组A[0,1,…,n-1],请构建一个数组B[0,1,…,n-1],其中B中的元素B[i]=A[0]A[1]…*A[i-1]A[i+1]…*A[n-1]。不能使用除法。解题暴力求解class Solution {public: vector<int> multiply(const vector<int>& A) { ...原创 2019-09-16 00:16:22 · 133 阅读 · 0 评论 -
剑指offer:数组中重复的数字
题目描述在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。解题hash表求解class Solution {public: // Parameters: //...原创 2019-09-15 23:44:22 · 113 阅读 · 0 评论 -
剑指offer:数组中的逆序对
题目描述在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007解题思路暴力超时class Solution {public: int InversePairs(vector<int> data) { i...原创 2019-09-13 14:53:48 · 105 阅读 · 0 评论 -
剑指offer:第一个只出现一次的字符
题目描述在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).解题思路class Solution {public: int FirstNotRepeatingChar(string str) { map<int, int> cnt; ...原创 2019-09-13 14:29:59 · 93 阅读 · 0 评论 -
LeetCode: 387. First Unique Character in a String
051105题目Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.Examples:s = "leetcode"return 0.s = "loveleetcode",return 2.Note: Y...原创 2019-05-11 13:22:20 · 157 阅读 · 0 评论 -
LeetCode:383. Ransom Note
051104题目Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the maga...原创 2019-05-11 12:13:07 · 177 阅读 · 0 评论 -
LeetCode:345. Reverse Vowels of a String
051103题目Write a function that takes a string as input and reverse only the vowels of a string.Example 1:Input: "hello"Output: "holle"Example 2:Input: "leetcode"Output: "leotcede"我的解题思路这道题跟...原创 2019-05-11 11:09:03 · 130 阅读 · 0 评论 -
LeetCode: 344. Reverse String
051102题目Write a function that reverses a string. The input string is given as an array of characters char[].Do not allocate extra space for another array, you must do this by modifying the input ar...原创 2019-05-11 10:28:17 · 119 阅读 · 0 评论 -
LeetCode: 58. Length of Last Word
051101题目Given a string s consists of upper/lower-case alphabets and empty space characters ’ ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word ...原创 2019-05-11 09:55:04 · 121 阅读 · 0 评论 -
LeetCode:937. Reorder Log Files
051401题目You have an array of logs. Each log is a space delimited string of words.For each log, the first word in each log is an alphanumeric identifier. Then, either:Each word after the identifi...原创 2019-05-14 00:46:21 · 210 阅读 · 0 评论 -
LeetCode: 929. Unique Email Addresses
051301题目Every email consists of a local name and a domain name, separated by the @ sign.For example, in alice@leetcode.com, alice is the local name, and leetcode.com is the domain name.Besides low...原创 2019-05-13 23:59:29 · 494 阅读 · 0 评论 -
LeetCode: 38. Count and Say
0509第2题题目The count-and-say sequence is the sequence of integers with the first five terms as following:1112112111112211 is read off as “one 1” or 11.11 is read off as “two 1s...原创 2019-05-09 00:37:18 · 137 阅读 · 0 评论 -
LeetCode: 20. Valid Parentheses
0509第1题(虽然是08做的,但这会已经09了)题目Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.An input string is valid if:Open brackets must be c...原创 2019-05-09 00:10:00 · 114 阅读 · 0 评论 -
LeetCode:14. Longest Common Prefix
两年硕士超快的鸭,又要准备秋招啦!0508第一题~题目Write a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string “”.Example 1:Input: ["flower"...原创 2019-05-08 09:47:38 · 136 阅读 · 0 评论 -
LeetCode--485
485. Max Consecutive Ones题目概述Given a binary array, find the maximum number of consecutive 1s in this array.Example 1:nput: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three di原创 2017-02-26 21:40:47 · 710 阅读 · 0 评论