LeetCode:数组
在深圳搬砖
一入IT深似海,搬起砖来特别嗨
展开
-
把数组排成最小的数
一、问题介绍输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。二、解题思路定义新的大小关系,对vector容器内的数据进行排序,按照 将a和b转为string后若 a+b<b+aa排在b之前的规则排序,如 2 21 因为 21 2 < ...原创 2019-09-16 20:11:49 · 114 阅读 · 1 评论 -
扑克牌中的顺子
一、问题描述从扑克牌中随机抽 5 张牌,判断是不是顺子,即这 5 张牌是不是连续的。 2-10 为数字本身,A 为 1,J 为 11,Q 为 12,K 为 13,而大小王可以看成任意的 数字。(大小王最多4张)二、解题思路1、首先我们应该关注的是大小王等特殊字符,由于可以当成任意的数字,所有当成0处理。2、把数组排序,再统计数组中0的个数3、统计排序之后的数组中相邻数字之间的空缺总...原创 2019-09-16 16:19:59 · 281 阅读 · 0 评论 -
调整数组顺序使奇数位于偶数前面
一、问题描述输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。二、解题思路两个指针,一个首、一个尾。然后一个向前、一个向后判断奇、偶,然后交换(类似于快排的思想)三、代码实现void reOrderArray(vector<int> &a)...原创 2019-09-16 15:47:22 · 98 阅读 · 0 评论 -
LeetCode:349. Intersection of Two Arrays
一、问题介绍Given two arrays, write a function to compute their intersection.Example 1:Input: nums1 = [1,2,2,1], nums2 = [2,2]Output: [2]Example 2:Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]O...原创 2019-09-16 12:58:19 · 73 阅读 · 0 评论 -
LeetCode:287. Find the Duplicate Number
一、问题描述Given an arraynumscontainingn+ 1 integers where each integer is between 1 andn(inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate nu...原创 2019-09-12 17:06:34 · 85 阅读 · 0 评论 -
LeetCode:217. Contains Duplicate
一、问题描述Given an array of integers, find if the array contains any duplicates.Your function should return true if any value appears at least twice in the array, and it should return false if every e...原创 2019-09-12 14:30:21 · 81 阅读 · 0 评论 -
LeetCode167. Two Sum II - Input array is sorted
一、问题描述Given an array of integers that is alreadysorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two ...原创 2019-09-12 11:49:01 · 93 阅读 · 0 评论 -
LeetCode:3 Sum问题(C++实现)
一、问题描述Given an arraynumsofnintegers, are there elementsa,b,cinnumssuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.Note:The solution set must ...原创 2019-09-11 19:18:25 · 573 阅读 · 1 评论 -
LeetCode Two Sum问题(C++实现)
问题描述:Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1]解题思路:最容易想到的就是两层遍历循环(最容易想到的,也是最差的)时间复杂度O(n*n),如何O(n)复杂度解决呢?这就要利用到map查找是线性时间的特性。代码如下:#includ...原创 2019-09-10 12:40:31 · 253 阅读 · 3 评论