Leetcode
文章平均质量分 58
Choly_chen
这个作者很懒,什么都没留下…
展开
-
LeetCode 169. Majority Element
最近开始刷题,从easy开始。刚看到这道题的想法是用双轨制记录位置再进行遍历。代码如下:class Solution {public: int majorityElement(vector& nums) { int j=0,sum=0; sort(nums.begin(),nums.end()); while(原创 2017-07-11 11:10:04 · 161 阅读 · 0 评论 -
LeetCode 646. Maximum Length of Pair Chain
问题描述: You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c. Chain原创 2017-12-02 17:58:34 · 108 阅读 · 0 评论 -
LeetCode 买卖股票的合适时间
最近在看贪心算法及相关内容,找出了leetcode相关的专题来做,碰到了买卖股票的一系列问题,故记录以备之。一、入门一级:只能买卖一次股票,求最大利润Title: Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on da原创 2017-12-20 09:52:00 · 4921 阅读 · 1 评论 -
LeetCode Find K-th Smallest Pair Distance
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B.Example 1: Input: nums = [1,3,1] k原创 2017-12-04 16:52:06 · 309 阅读 · 0 评论 -
树 求树的最小深度、最大深度
一、二叉树的最小深度Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.求解此类问题要先明确递归状态及递归条件,考虑清楚是否有漏洞。本题有原创 2017-12-06 16:24:39 · 3582 阅读 · 0 评论 -
求二进制中1的个数
题目描述: 输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。复习了一下原码、反码、补码的转换关系及来源,看到一个博客介绍的很详细,包括原码与补码本质上取模运算后得到的结果是相同的。 https://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html本题的方法很巧妙,通过每次n-1再与n作与操作,其实是将最原创 2017-12-07 22:30:17 · 170 阅读 · 0 评论 -
hiho一下第一周 马拉车算法实现最长回文子串
题目1 : 最长回文子串时间限制:1000ms 单点时限:1000ms 内存限制:64MB描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进。这一天,他们遇到了一连串的字符串,于是小Hi就向小Ho提出了那个经典的问题:“小Ho,你能不能分别在这些字符串中找到它们每一个的最长回文子串呢?”小Ho奇...原创 2018-01-03 17:20:36 · 190 阅读 · 0 评论 -
判断输入的字符串在键盘中是否为同一行 Keyboard Row
题目描述、样例及注意事项: Given a List of words, return the words that can be typed using letters of alphabet on only one row’s of American keyboard like the image below. Example 1: Input: [“Hello”, “Alaska”, “原创 2017-12-28 22:34:52 · 363 阅读 · 0 评论 -
LeetCode 152. Maximum Product Subarray
必须要考虑到的情况: 负数,零。 两种方法:方法一: 保留最大值,最小值。当负数存在时,交换最大最小值。用了swap函数,很巧妙。 class Solution {public: int maxProduct(vector& nums) { int r = nums[0], val_max=r, val_min原创 2017-07-26 16:41:05 · 155 阅读 · 0 评论 -
(树) 101. Symmetric Tree
给定一个二叉树,判定其是否为左右对称的结构。参考答案有两种方法:1.子函数调用,递归实现 2. 运用栈的结构,递归实现。 方法一更简洁明了。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *ri原创 2017-11-14 10:14:28 · 130 阅读 · 0 评论 -
LeetCode 135. Candy
题目要求: There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one原创 2017-12-01 20:21:34 · 137 阅读 · 0 评论 -
Leetcode Minimum ASCII Delete Sum for Two Strings
Leetcode Minimum ASCII Delete Sum for Two StringsInput: s1 = “delete”, s2 = “leet” Output: 403 Explanation: Deleting “dee” from “delete” to turn the string into “let”, adds 100[d]+101[e]+101[e] to th原创 2017-11-30 12:17:11 · 176 阅读 · 0 评论 -
Leetcode 189 Rotate Array
至少用三种方法实现,参考了一些简便方法,顺带了解一下reverse,insert, resize 函数的用法。方法一代码简单但运行速度较慢。法一 说明: 例有7个数 1 2 3 4 5 6 7执行第一步后: 4 3 2 1 5 6 7执行第二步后: 4 3 2 1 7 6 5然后,整体reverse:5 6 7 1 2 3 4法二 即为栈的应用原创 2017-07-13 14:54:06 · 149 阅读 · 0 评论 -
LeetCode 26. Remove Duplicates from Sorted Array
用了两种简便方法完成。个人更prefer第二种,且方法二时间更短。用到的函数:unique,erase。 unique为对nums重排序使其在it位置前无重复元素。 用erase函数删除it位置后的重复函数,再返回nums的大小即为无重复元素的长度。class Solution {public: int removeDuplicates(vector& nums) {原创 2017-07-13 15:48:50 · 163 阅读 · 0 评论 -
LeetCode 119. Pascal's Triangle II
一道递归问题,最初程序如下,可运行时报错为指针指向未知位置:class Solution {public: vector getRow(int rowIndex) { vector Row; for(int i=0; i { for(in原创 2017-07-30 16:47:15 · 152 阅读 · 0 评论 -
LeetCode 209. Minimum Size Subarray Sum
看到这道题,第一个想法是遍历整个数组,然后返回满足 和条件 的相邻数组长度的最小值。如下所示:int count=1, i=0; int size = nums.size(); while(i { int sum=nums[i]; for(int原创 2017-07-24 20:24:41 · 157 阅读 · 0 评论 -
LeetCode 414 Third Maximum Number
采用两种方法实现。方法一是自己想到的,方法二是提交后参考别人的代码。auto i:nums表示遍历容器nums中每个值依次赋给i.class Solution {public: int thirdMax(vector& nums) { /* sort(nums.begin(), nums.end()); //从小到大分类原创 2017-08-03 17:18:21 · 198 阅读 · 0 评论 -
442. Find All Duplicates in an Array
看到这道题时,第一个想到的是用map建立键值关系,再通过判断值的大小决定返回vector,即如方法一所示。方法一:class Solution {public: vector findDuplicates(vector& nums) { // map vector res; if(num原创 2017-09-24 13:52:28 · 132 阅读 · 0 评论 -
Leetcode 532. K-diff Pairs in an Array
题目要求找出数组中差值为k的对数,要求有重复元素出现时不可重复计数。两种思路求解:1. 先将数组排序,再设定指针定位元素位置。NOTE:使用while循环排除掉重复元素。class Solution {public: int findPairs(vector& nums, int k) { if(k原创 2017-09-21 16:51:02 · 156 阅读 · 0 评论 -
(树)687. Longest Univalue Path
最近在做树结构的相关习题,因为之前随机抽题的时候发现这个模块总是出错,考虑的不周全,所以想集中做一下。Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through th原创 2017-11-10 15:18:46 · 196 阅读 · 0 评论 -
(链表) Add to List 2. Add Two Numbers
几天没做LeetCode,现在题目总数已经直逼700了....这道题为链表,本科数据结构接触过一些后,已经好久没练过了。 基本操作都快忘了。这道题是将两个链表中的数相加,当数大于10时,需向前“进位”。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNod原创 2017-11-14 15:36:30 · 187 阅读 · 0 评论 -
Trie 树(前缀树)
一、Trie 树是我一直想看却一直拖着没看的数据结构。主要应用的思想是通过构建树结构,使得前缀相同的字符串可共享前缀序列,一旦构建好树结构后,便可实现快速检索。适用场景:1)找到所有前缀相同的字符串 2)以字典序枚举数据集中的每个字符串。哈希表在上述两种场景下会有大量的哈希碰撞(hash collisions)且搜索的时间复杂度很高(最差情形下为 O(n),n为插入的键值数)。故相较于哈希表...原创 2018-04-04 19:17:37 · 251 阅读 · 0 评论