刷题笔记
记录下自己刷题过程,分享收获。
岳麓吹雪
现在努力,以后就可以休息
展开
-
LeetCode 64:最小路径和
题目思路当前点值为grid[i][j],则到达该点最小路径总和:dis[i][j] = min(dis[i][j-1],dis[i-1][j]) + grid[i][j]第一列最小路径总和为:dis[i][j] = dis[i][j-1] + grid[i][j]第一行最小路径总和为:dis[i][j] = dis[i-1][j] + grid[i][j]。从左上到右下遍历...原创 2018-08-23 22:07:20 · 374 阅读 · 0 评论 -
LeetCode 349,350:两个数组的交集
题目思路leetcode把该题放在了二分查找类别底下,被误导了,一开始思路是从nums1中找nums2的值,由于两个数组存在重复数,实际使用直接遍历查找十分简单。首先将两个数组排序,各自从nums1和nums2的第一个元素开始,如果相同,将该元素放入输出数组;如果nums1元素大,nums2向后找;如果nums2元素大,nums1向后找,直到有一个数组被遍历。其中将相同元素数组...原创 2018-08-22 11:43:22 · 851 阅读 · 2 评论 -
LeetCode 35,704:搜索插入位置和二分查找
LeetCode 35:搜索插入位置 LeetCode 704:二分查找 两题都是最基本的二分查找问题,两题一起来做。题目LeetCode 35:搜索插入位置 LeetCode 704:二分查找 思路两题都是最基本的二分法问题,区别是不存在时的返回方式。704比较容易,只需要返回-1,而35需要返回插入位置,两者最后的特殊情况处理不同。代码LeetC...原创 2018-08-21 21:13:00 · 436 阅读 · 0 评论 -
LeetCode 542:01矩阵
题目思路方法1,笨方法,首先想到的是,把0的位置先找出来存起来,然后对非零位置找离最近0的距离,这样算复杂度很高。方法2,找邻域,确定与0的距离。第一次循环,遍历所有点,如果当前点为0,跳过;否则,找其邻域4个点是否有0,如果有,跳过;如果没有,将该点的值+1;第二次循环,如果当前点小于等于1,跳过;否则,找其邻域4个点是否有1,如果有,跳过;如果没有,将该点的值+1;...原创 2018-08-20 22:44:27 · 2264 阅读 · 0 评论 -
LeetCode 338:比特位计数
题目思路考点就是统计1的个数,直接想到的就是n&(n-1),需要遍历所有的数字进行统计,复杂度较高。在看了别人的做法以后,改了版本2。规律为: 1.首先考虑数字表示所需的二进制位数,每两个数所需位数相同,比如0和1需要1位,2和3需要2位……每一对数包含一个奇数一个偶数,奇数中包含的1的个数比偶数多1。 2.对于偶数和它右移1位的数字包含1的个数相同。公式:re...原创 2018-08-20 09:39:27 · 1161 阅读 · 0 评论 -
LeetCode 821:字符的最短距离
题目思路首先找出字符C所在位置,然后遍历字符串S,计算和字符C位置的最短距离。代码class Solution {public: vector<int> shortestToChar(string S, char C) { int n = S.size(); vector<int> temp; ...原创 2018-08-19 19:05:56 · 442 阅读 · 0 评论 -
LeetCode 867:转置矩阵
题目思路重新定义一个容器,将行列互换存储即可。代码class Solution {public: vector<vector<int>> transpose(vector<vector<int>>& A) { vector<vector<int>> AT; ...原创 2018-08-19 09:35:46 · 545 阅读 · 0 评论 -
LeetCode 171:Excel表列序号
题目思路26进制与10进制转换问题,不过这个26进制都是大于0的,没有0,这样就更简单了,不需要考虑首位为0的问题。代码class Solution {public: int titleToNumber(string s) { int n = s.size(); int temp=0; for(int i=0;i&l...原创 2018-08-18 21:22:58 · 362 阅读 · 0 评论 -
LeetCode 292:Nim游戏
题目思路乍一看还是很难的,以为要用动态规划,熟悉了游戏规则和题目要求之后,统计规律就可以做了。下面来分情况分析下。如果石头小于4块,可以直接都拿走,必胜: 如果石头为4块,无论拿1块还是3块,剩余石头数都少于4块,必输; 如果石头为5-7块,可以拿走1-3块,使得剩余石头数等于4块,必胜; 如果石头为8块,无论拿1块还是3块,都会是5-7块,对方可以将石头数变成4块,...原创 2018-08-18 21:10:25 · 206 阅读 · 0 评论 -
LeetCode 617:合并二叉树
题目思路初始想法是遍历两棵树,把对应节点位置相加。使用递归方式遍历树最为容易,在遍历过程中,对t1和t2的对应节点值相加。如果某个节点位置t1不存在节点,则直接返回该位置的t2节点;反之,如果t2不存在节点,则返回t1节点。代码/** * Definition for a binary tree node. * struct TreeNode { * int ...原创 2018-08-18 17:33:16 · 294 阅读 · 0 评论 -
LeetCode 371:两数之和
题目思路不使用+和-操作,也就是要求使用位运算进行加法运算。推荐阅读:https://blog.csdn.net/zhongjiekangping/article/details/6855864只考虑一位二进制数情况,加法有四种情况: 0+0=0 0+1=1 1+0=1 1+1=0(进位) 各位情况实际就是^异或操作,只有在1+1时进位,也就是&与操作,而...原创 2018-08-18 09:44:53 · 263 阅读 · 0 评论 -
LeetCode 6:Z字形变换
题目思路定义numRows大小的vector容器来存Z字形排列的字符。比对输入输出,忽略空格,可以发现规律,拿出2*numRows-1个字符来看如下图,就是一种迂回读取。先按行升序读取到对应行,然后再反过来降序读取到对应行,一直这样重复即可。 代码class Solution {public: string convert(string s, int numRo...原创 2018-08-16 17:50:58 · 237 阅读 · 0 评论 -
LeetCode 559,589,590:N叉树的前序遍历、先序遍历、最大深度
题目 思路N叉树的结构如下:class Node {public: int val; vector&amp;lt;Node*&amp;gt; children; Node() {} Node(int _val, vector&amp;lt;Node*&amp;gt; _children) { val = _val; children = ...原创 2018-08-15 10:20:41 · 2544 阅读 · 1 评论 -
LeetCode 226:翻转二叉树
题目思路对树的问题采用递归的方式比较容易。从变换形式可以看到,该题实际就是将左右子树交换位置,题目没有告诉是完全二叉树,因此可能存在某个节点只有左子树或右子树中的一个,此时也要进行交换。代码/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeN...原创 2018-08-15 09:38:18 · 555 阅读 · 0 评论 -
LeetCode 657:判断路线成圈
题目思路可以转换为数学问题,从坐标系原点出发,向四个方向运动,判断最终坐标是否为(0,0)。代码class Solution {public: bool judgeCircle(string moves) { int n=moves.size(); int hor=0,ver=0; for(int i=0;i&l...原创 2018-06-29 16:04:10 · 277 阅读 · 0 评论 -
LeetCode 3:无重复字符的最长子串
题目思路设置一个变量start用于记录不重复区间的起始位置。对第i个字符,检测从start开始到i-1的字符中是否存与第i个字符相同的,不相同则长度计数+1,否则,将start置为i,进行下一轮检测,直到遍历完所有字符。代码class Solution {public: int lengthOfLongestSubstring(string s) { ...原创 2018-06-29 16:50:18 · 220 阅读 · 0 评论 -
LeetCode:Add Digits(各位相加)
题目思路一开始想的比较简单,递归求每次的各位数字之和,直到和小于10。然后考虑了O(1)复杂度下的方法,在循环求和时,每次都将求和结果拆成当前位和进位两个数,这样可以保证始终求和都是个位数。代码递归:class Solution {public: int addDigits(int num) { while(num>9) ...原创 2018-06-29 11:42:42 · 365 阅读 · 0 评论 -
刷题笔记:牛客华为2016机试——简单错误记录
题目开发一个简单错误记录功能小模块,能够记录出错的代码所在的文件名称和行号。 处理: 1.记录最多8条错误记录,对相同的错误记录(即文件名称和行号完全匹配)只记录一条,错误计数增加;(文件所在的目录不同,文件名和行号相同也要合并) 2.超过16个字符的文件名称,只记录文件的最后有效16个字符;(如果文件名不同,而只是文件名的后16个字符和行号相同,也不要合并) 3.输入的文件可能带路...原创 2018-06-09 21:42:03 · 802 阅读 · 0 评论 -
刷题笔记:牛客华为2016机试——最高分是多少
题目老师想知道从某某同学当中,分数最高的是多少,现在请你编程模拟老师的询问。当然,老师有时候需要更新某位同学的成绩. 输入描述:输入包括多组测试数据。每组输入第一行是两个正整数N和M(0 < N <= 30000,0 < M < 5000),分别代表学生的数目和操作的数目。学生ID编号从1编到N。第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代...原创 2018-06-09 21:49:24 · 1687 阅读 · 0 评论 -
刷题笔记:牛客2017校招真题在线编程——统计字符
题目题目描述 给定一个英文字符串,请写一段代码找出这个字符串中首先出现三次的那个英文字符。 输入描述:输入数据一个字符串,包括字母,数字等。输出描述:输出首先出现三次的那个英文字符示例1 输入Have you ever gone shopping and输出e思路开辟两个数组,分别记录26个字母大小写出现的次数,搜索字符串,出现3次就输出。...原创 2018-06-08 21:51:13 · 300 阅读 · 0 评论 -
刷题笔记:牛客腾讯笔试题——有趣的数字
题目小Q今天在上厕所时想到了这个问题:有n个数,两两组成二元组,差最小的有多少对呢?差最大呢?输入描述: 输入包含多组测试数据。 对于每组测试数据: N - 本组测试数据有n个数 a1,a2...an - 需要计算的数据 保证: 1&lt;=N&lt;=100000,0&lt;=ai&lt;=INT_MAX.输出描述:对于每组数据,输出两个数,第一个数表示差最小的对数...原创 2018-06-08 21:26:11 · 1200 阅读 · 2 评论 -
刷题笔记:牛客网易笔试题——被3整除
题目小Q得到一个神奇的数列: 1, 12,123,…12345678910,1234567891011…。 并且小Q对于能否被3整除这个性质很感兴趣。 小Q现在希望你能帮他计算一下从数列的第l个到第r个(包含端点)有多少个数可以被3整除。输入描述: 输入包括两个整数l和r(1 <= l <= r <= 1e9), 表示要求解的区间两端。输出描述: ...原创 2018-06-04 23:23:39 · 1184 阅读 · 0 评论 -
LeetCode:Missing Number(缺失数字)
题目Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.Example 1:Input: [3,0,1]Output: 2Example 2:Input: [9,6,4,2,3,5,7,0,1...原创 2018-06-02 19:42:00 · 503 阅读 · 0 评论 -
LeetCode:Reverse Bits(颠倒二进制位)
题目Reverse bits of a given 32 bits unsigned integer.Example:Input: 43261596Output: 964176192Explanation: 43261596 represented in binary as 00000010100101000001111010011100, return...原创 2018-06-01 21:28:01 · 784 阅读 · 0 评论 -
LeetCode:Pascal's Triangle(杨辉三角)
题目Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.In Pascal’s triangle, each number is the sum of the two numbers directly above it.Example:Input: 5Out...原创 2018-06-02 11:06:17 · 286 阅读 · 0 评论 -
LeetCode: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 day i.If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the sto...原创 2018-05-24 22:09:49 · 233 阅读 · 0 评论 -
LeetCode:Shuffle an Array(打乱数组)
题目Shuffle a set of numbers without duplicates.Example:// Init an array with set 1, 2, and 3.int[] nums = {1,2,3};Solution solution = new Solution(nums);// Shuffle the array [1,2,3] and retur...原创 2018-05-21 20:53:34 · 948 阅读 · 1 评论 -
刷题笔记:2017校招真题在线编程——数串
题目题目描述 设有n个正整数,将他们连接成一排,组成一个最大的多位整数。 如:n=3时,3个整数13,312,343,连成的最大整数为34331213。 如:n=4时,4个整数7,13,4,246连接成的最大整数为7424613。输入描述: 有多组测试样例,每组测试样例包含两行,第一行为一个整数N(N<=100),第二行包含N个数(每个数不超过1000,...原创 2018-05-12 22:52:28 · 355 阅读 · 0 评论 -
LeetCode:Add Two Numbers(两数相加)
题目You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and retu...原创 2018-05-18 15:44:37 · 274 阅读 · 0 评论 -
LeetCode:Symmetric Tree(对称二叉树)
题目Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ ...原创 2018-05-18 16:09:35 · 227 阅读 · 0 评论 -
LeetCode:Number of 1 Bits(位1的个数)
题目Write a function that takes an unsigned integer and returns the number of ‘1’ bits it has (also known as the Hamming weight).Example 1:Input: 11Output: 3Explanation: Integer 11 has binary re...原创 2018-05-18 16:29:46 · 327 阅读 · 0 评论 -
LeetCode:Validate Binary Search Tree(验证二叉搜索树)
题目Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node’s key. ...原创 2018-05-16 22:11:49 · 431 阅读 · 0 评论 -
LeetCode:Maximum Depth of Binary Tree(二叉树的最大深度)
题目Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.Note: A leaf is a node with no children.E原创 2018-05-09 21:07:48 · 371 阅读 · 0 评论 -
LeetCode:Linked List Cycle(环形链表)
题目Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?思路用快慢指针法,快指针每次移动2步,慢指针每次移动1步。如果有环,快指针会先进入环中,等slow进入环中就变成了追及问题。每次追一步,一定会相遇,相遇就说明有环。否则,快指针会原创 2018-05-06 22:21:33 · 332 阅读 · 0 评论 -
LeetCode:Palindrome Linked List(回文链表)
题目Given a singly linked list, determine if it is a palindrome.Follow up: Could you do it in O(n) time and O(1) space?思路将链表中的内容取出存到vector中,对vector做回文检测,实现可以容易很多,但效率低。代码/** * Definition for singly-link原创 2018-05-06 21:30:09 · 286 阅读 · 0 评论 -
LeetCode:First Bad Version(第一个错误的版本)
题目You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on...原创 2018-05-06 16:15:12 · 373 阅读 · 0 评论 -
LeetCode: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...原创 2018-05-06 15:21:47 · 686 阅读 · 0 评论 -
LeetCode:Merge Two Sorted Lists(合并两个有序链表)
题目Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.Example:Input: 1->2->4, 1->3->4Output:...原创 2018-05-06 11:16:36 · 1036 阅读 · 0 评论 -
LeetCode:Palindrome Number(回文数)
题目Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.Example 1:Input: 121Output: trueExample 2:Input: -121Output: false...原创 2018-05-05 21:45:10 · 197 阅读 · 0 评论 -
LeetCode:Valid Anagram(有效的字母异位词)
题目Given two strings s and t, write a function to determine if t is an anagram of s.For example, s = “anagram”, t = “nagaram”, return true. s = “rat”, t = “car”, return false.Note: You may ass...原创 2018-05-03 21:51:52 · 207 阅读 · 0 评论