逻辑思维
涛涛酱
北邮研究僧在读
展开
-
【LeetCode 918】 Maximum Sum Circular Subarray
题目描述Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty subarray of C.Here, a circular array means the end of the array connects to the beginning of the array. (Formally, C[i] = A[i] when 0 <= i < A.原创 2020-06-09 10:32:01 · 170 阅读 · 0 评论 -
【面试题 & 智力题】给8个小球,7个质量相同,一个重一点,给你一个天平,称几次可以找到重的那一个。
智力堪忧。2次。第一次 3-3 在这六个或者不在这六个里。在的话,1 - 1,可以从三个里找到。不在的话,1-1 剩下的两个也可以找到。原创 2020-04-23 13:01:52 · 3948 阅读 · 0 评论 -
【LeetCode 190】Reverse Bits
题目描述Reverse bits of a given 32 bits unsigned integer.Example 1:Input: 00000010100101000001111010011100Output: 00111001011110000010100101000000Explanation: The input binary string 0000001010010100...原创 2020-02-15 16:37:02 · 97 阅读 · 0 评论 -
【LeetCode 122】 Best Time to Buy and Sell Stock II
题目描述Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., bu...原创 2020-02-15 16:12:41 · 75 阅读 · 0 评论 -
【LeetCode 88】 Merge Sorted Array
题目描述Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:The number of elements initialized in nums1 and nums2 are m and n respectively.You may assume t...原创 2020-02-14 22:46:44 · 73 阅读 · 0 评论 -
【LeetCode 172】 Factorial Trailing Zeroes
题目描述Given an integer n, return the number of trailing zeroes in n!.Example 1:Input: 3Output: 0Explanation: 3! = 6, no trailing zero.Example 2:Input: 5Output: 1Explanation: 5! = 120, one trai...原创 2020-02-14 22:17:05 · 82 阅读 · 0 评论 -
【LeetCode 28】 Implement strStr()
题目描述Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Example 1:Input: haystack = "hello", needle = "ll"Output: 2Example...原创 2020-02-14 21:31:56 · 76 阅读 · 0 评论 -
【LeetCode 189】 Rotate Array
题目描述Given an array, rotate the array to the right by k steps, where k is non-negative.Example 1:Input: [1,2,3,4,5,6,7] and k = 3Output: [5,6,7,1,2,3,4]Explanation:rotate 1 steps to the right: [7...原创 2020-02-14 20:42:07 · 78 阅读 · 0 评论 -
【LeetCode 204】 Count Primes
题目描述Count the number of prime numbers less than a non-negative number, n.Example:Input: 10Output: 4Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.思路筛法 O(n)。但是vector用...原创 2020-02-14 13:13:50 · 85 阅读 · 0 评论 -
【LeetCode 169】 Majority Element
题目描述Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element ...原创 2020-02-13 21:07:01 · 70 阅读 · 0 评论 -
448. Find All Numbers Disappeared in an Array
题目描述Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.C...原创 2020-02-13 19:56:33 · 104 阅读 · 0 评论 -
【LeetCode 166】 Fraction to Recurring Decimal
题目描述Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.If the fractional part is repeating, enclose the repeating part in parentheses....原创 2020-02-09 14:36:34 · 74 阅读 · 0 评论 -
【LeetCode 36】 Valid Sudoku
题目描述Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:Each row must contain the digits 1-9 without repetition.Each column must co...原创 2020-02-09 00:31:57 · 85 阅读 · 0 评论 -
【LeetCode 134】 Gas Station
题目描述There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to ...原创 2020-02-08 20:09:35 · 120 阅读 · 0 评论 -
【LeetCode 54】 Spiral Matrix
题目描述Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.Example 1:Input:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]Output: [1,2,3,6,9,8,7,4,5]...原创 2020-02-08 17:04:55 · 67 阅读 · 0 评论 -
【LeetCode 73】Set Matrix Zeroes
题目描述Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.Example 1:Input: [ [1,1,1], [1,0,1], [1,1,1]]Output: [ [1,0,1], [0,0,0], [1,0,1]]...原创 2020-02-08 16:25:32 · 81 阅读 · 0 评论 -
【LeetCode 581】Shortest Unsorted Continuous Subarray
题目描述Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.You need to f...原创 2020-02-07 15:06:55 · 106 阅读 · 0 评论 -
【LeetCode 56】 Merge Intervals
题目描述Given a collection of intervals, merge all overlapping intervals.Example 1:Input: [[1,3],[2,6],[8,10],[15,18]]Output: [[1,6],[8,10],[15,18]]Explanation: Since intervals [1,3] and [2,6] overla...原创 2020-02-01 23:37:38 · 85 阅读 · 0 评论 -
【LeetCode 55】 Jump Game
题目描述Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if...原创 2020-01-31 21:39:34 · 106 阅读 · 0 评论 -
【LeetCode 22】Generate Parentheses
题意:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[ "((()))", "(()())", "(())()", "()(())...原创 2020-01-16 16:22:47 · 71 阅读 · 0 评论 -
【LeetCode 406】Queue Reconstruction by Height
题意:Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of t...原创 2020-01-15 22:12:03 · 77 阅读 · 0 评论 -
【LeetCode 621】Task Scheduler
题意:Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks. Tasks could be done without original order. Each task co...原创 2020-01-10 20:06:50 · 111 阅读 · 0 评论 -
【LeetCode 48】Rotate Image
题目描述:You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the image in-place, which means you have to modify the input 2D matri...原创 2020-01-10 19:23:14 · 111 阅读 · 0 评论 -
【LeetCode 238】
题目描述:给一个数组,求数组的各元素乘积(不包括当前位置)。思路:用两个数组记录顺序数组乘积和逆序数组乘积。代码:class Solution {public: vector<int> productExceptSelf(vector<int>& nums) { int len = nums.size(); vec...原创 2019-05-09 09:38:14 · 72 阅读 · 0 评论 -
【剑指 offer】反转单词顺序列
题目描述:牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?思路:两次翻转。先翻...原创 2019-03-24 09:43:30 · 91 阅读 · 0 评论 -
【LeetCode Contest 142.】1093 - 1096
…周赛一道题都没出,先是晚上了半个点。然后1093读错题,mode疯狂猜想是取模,原来是众数。然后1094以为是树状数组或者并查集,原来只是一个模拟。后两道没看,比赛就过去了。过去了。去了。了。卒。1093. Statistics from a Large Sample对0 to 256的数进行采样,每个数字可能采样 0 to 10910^{9}109次,求出采样数据的最小值,最大值,平均数...原创 2019-06-24 11:14:39 · 270 阅读 · 0 评论 -
【剑指 offer】数组中出现次数超过一半的数字
题目描述:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。思路:用一个key值记录数组中的数字,然后用一个value记录该数字出现的次数,然后累加:继续遍历余下的所有数字,如果和这个数字相等,就把次数加1;如果和这个数字不等,那么就...原创 2019-04-15 20:59:24 · 74 阅读 · 0 评论 -
【剑指 offer】把数组排成最小的数
题目描述:输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。思路:定义排序方法,str1+str2 > str2 + str1,则str1 > str2。代码:class Solution {public: string to_string(...原创 2019-04-11 11:06:07 · 82 阅读 · 0 评论 -
【剑指 offer】丑数
题目描述:把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。思路:只对丑数进行操作,为了避免重复,记录上一次*2, *3,*5得到丑数的位置。代码:class Solution {public: int GetUglyNumber_Solution...原创 2019-04-10 19:34:52 · 96 阅读 · 0 评论 -
【剑指 offer】数组中只出现一次的数字
题目描述:一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。思路:如果数组中只有一个只出现一次的数字,所有数字的异或结果就是要找的数字。现在有两个只出现一次的数字,所有数字的异或结果其实就是这两个数字的异或。按照结果的第一个数字1的位置把所有数字分成两个数组。分别异或,即找到两个数字。代码:class Solution {public: ...原创 2019-03-27 16:07:17 · 81 阅读 · 0 评论 -
【剑指 offer】孩子们的游戏
题目描述:每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0…m-1报数…这样下去…直到剩下最后...原创 2019-03-26 10:11:19 · 97 阅读 · 0 评论 -
【剑指 offer】顺时针打印矩阵
题目描述:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.思路:emmm…注意不要打重喽。(陕西口音buff)代码:class Solution {public: ...原创 2019-03-17 19:35:13 · 108 阅读 · 0 评论 -
【LeetCode 324】Wiggle Sort II
题目描述:Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]…example 1:Input: nums = [1, 5, 1, 1, 6, 4]Output: One possible answer is [1, 4, 1, 5, 1, 6]....原创 2019-03-18 09:51:54 · 75 阅读 · 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; ans && (ans += Su...原创 2019-03-12 17:30:47 · 87 阅读 · 0 评论 -
【剑指 offer】和为s的两个数字
题目描述:输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。对应每个测试案例,输出两个数,小的先输出。思路:利用排序数组的规律,定义两个指针,分别指向数组的首尾,如果两个指针所指的数字大于s,则尾指针-1;如果小于s,则首指针+1;如果等于s,则输出。如果两个指针相遇,仍找不到等于s的两个数,则说明数组中不存在和为...原创 2019-03-20 21:07:56 · 84 阅读 · 0 评论 -
【剑指 offer】整数中1出现的次数
题目描述:求出113的整数中1出现的次数,并算出1001300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1、10、11、12、13因此共出现6次,但是对于后面问题他就没辙了。ACMer希望你们帮帮他,并把问题更加普遍化,可以很快的求出任意非负整数区间中1出现的次数(从1 到 n 中1出现的次数)。思路:从个位、十位、百位…各个位置上1出现的规律来解。代码:class...原创 2019-04-12 17:04:08 · 127 阅读 · 0 评论 -
【剑指 offer】连续子数组的最大和
题目描述:给一个数组,返回它的最大连续子序列的和,你会不会被他忽悠住?(子向量的长度至少是1)。例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。思路:维护一个数组dp[],dp[i]表示到i位置且包含a[i]的连续子向量的最大和。如果dp[i-1]<0,重新开始序列,否则和之前的dp值相加。先用数据来尝试解决这个问题,然后再考虑...原创 2019-04-13 11:41:53 · 78 阅读 · 0 评论 -
【剑指 offer】二叉搜索树的后序遍历序列
题目描述:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。思路:二叉搜索树(Binary Search Tree,BST)又称二叉排序树、二叉查找树。二叉排序树或者是一棵空树,或者是具有下列性质的二叉树:(1)若左子树不空,则左子树上所有结点的值均小于或等于它的根结点的值;(2)若右子树不空,则右...原创 2019-04-16 21:41:43 · 81 阅读 · 0 评论 -
【LeetCode 121】Best Time to Buy and Sell Stock
题目描述:给一个数组表示股票在第 i 天的股票价格,只允许最多一次交易,也就是一次买入和一次卖出,求能够获得的最大收益。思路:记录到当前位置的最小数字,并记录当前能够获得的最大利润。代码:class Solution {public: int maxProfit(vector<int>& prices) { if (prices.empty(...原创 2019-05-29 14:46:51 · 75 阅读 · 0 评论 -
【LeetCode 215】Kth Largest Element in an Array
题目描述:求数组中第k大的数。思路:快排思想。使用kuai分治每次将数组划分为两段,并且返回划分点的位置,如果这个位置恰好是我们要的第k大的数,那么返回这个数即可。如果返回的位置小于要找的位置,则向右边的一半数组继续寻找。如果返回的位置大于要找的位置,则向左边寻找。代码:class Solution {public: int partition(vector<int&...原创 2019-05-22 21:29:09 · 62 阅读 · 0 评论