![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
leetcode刷题记录
i_actor
一只学C++的菜鸡。
展开
-
leetcode刷题-go(二)
leetcode21 合并两个有序链表描述:将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。此题为简单题,没什么需要讲的,主要是为了熟悉go的代码。/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */func mergeTwoLists(list1 *ListNode, list2原创 2022-05-17 21:55:59 · 292 阅读 · 1 评论 -
leetcode刷题记录-go版本(1、20、53、70、121、136)
为了秋招,从今天开始每天至少在leetcode上刷五道题。同时为了加强go的学习,我准备后面做题先用go语言来做。由于今天是第一题,先做简单题就不写做题思路,直接写代码了。//leetcode136func singleNumber(nums []int) int { ans := nums[0] for i := 1; i < len(nums); i++{ ans ^= nums[i] } return ans}//leetcode121func maxProfit(pr原创 2022-05-16 20:20:27 · 247 阅读 · 0 评论 -
leetcode226 - Invert Binary Tree
题目:Invert a binary tree.反转二叉树,只需要顺序遍历二叉树,然后交换其左右子树就行了。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeN原创 2020-12-06 21:50:20 · 64 阅读 · 0 评论 -
leetcode617 - Merge Two Binary Trees
题目:Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then s原创 2020-12-06 21:44:03 · 60 阅读 · 0 评论 -
leetcode200 - Number of Islands(DFS)
题目:Given an m x n 2d grid map of '1’s (land) and '0’s (water), return the number of islands.An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surround原创 2020-12-06 21:05:46 · 123 阅读 · 0 评论 -
leetcode279 - Perfect Squares
题目:Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n.这道题我最开始的做法是先让n减去最大的小于n的平方数,然后统计,发现行不通,考虑了半天发现没太好的思路,就参考了一下这位老哥的思路:https://blog.csdn.net/qq_35481167/article/details/82817699他是使用原创 2020-12-02 21:35:38 · 70 阅读 · 0 评论 -
leetcode75 - Sort Colors
题目:Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.Here, we will use the integers 0, 1, and 2 to represent the color red,原创 2020-12-02 20:27:29 · 85 阅读 · 0 评论 -
leetcode131 - Palindrome Partitioning
题目:Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.大意:要求将s拆分成多个字符串,要求每个字符串都是回文字符串。我个人的做法就是暴力求解,找出所有的可能。(刚刚好能通过)class Solution {public: bool isRight(string &原创 2020-12-01 21:57:37 · 70 阅读 · 0 评论 -
leetcode103 - Binary Tree Zigzag Level Order Traversal
题目:Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).大意:将二叉树层次遍历一行正序一行倒序输出。我个人的做法就是先将二叉树层次遍历,然后把逆转奇数行的数据。/** * Definition for a bina原创 2020-12-01 21:34:52 · 71 阅读 · 0 评论 -
leetcode36 - Valid Sudoku
题目:Determine if a 9 x 9 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 contain the digits 1-9 without repetition.Each of the ni原创 2020-12-01 21:10:54 · 78 阅读 · 0 评论 -
leetcode105 - Construct Binary Tree from Preorder and Inorder Traversal
题目:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.这道题给出先序和中序 要求画出所给的唯一的二叉树。只需要顺序遍历先序数组,然后再把该元素放入到中序中,将中序数组分为左右两边就行了。/** * Definition for a binary tree node. * st原创 2020-11-29 16:20:09 · 112 阅读 · 0 评论 -
leetcode371 - Sum of Two Integers
题目:Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.这道题要求不能用加法和减法,那么就只能使用位运算来做了。class Solution {public: int add(int a, int b){ if(b == 0) return a; int sum = a ^ b; i原创 2020-11-29 15:43:13 · 60 阅读 · 0 评论 -
leetcode208 - Implement Trie (Prefix Tree)
题目:Implement a trie with insert, search, and startsWith methods.这道题好像相对比较简单,不知道为啥大家的提交准确率感觉挺低的,可能是我忽略了什么东西。TnTclass Trie {public: vector<string>trie; /** Initialize your data structure here. */ Trie() { } /** Ins原创 2020-11-28 22:04:20 · 68 阅读 · 0 评论 -
leetcode11 - Container With Most Water
题目:Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a co原创 2020-11-28 21:54:02 · 126 阅读 · 0 评论 -
leetcode 4Sum Ⅱ
题目:Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the ran原创 2020-11-28 16:03:47 · 49 阅读 · 0 评论 -
leetcode341 - Flatten Nested List Iterator
题目:Given a nested list of integers, implement an iterator to flatten it.Each element is either an integer, or a list – whose elements may also be integers or other lists.这道题要求将所给vector打平,相对简单。个人做法是先存入到vector,然后直接访问就行了。/** * // This is the interface th原创 2020-11-28 15:41:54 · 78 阅读 · 2 评论 -
leetcode62 - Unique Paths(faster than 100.00% and less than 75.54%)
题目:A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the原创 2020-11-26 15:44:27 · 61 阅读 · 0 评论 -
leetcode102 - Binary Tree Level Order Traversal
题目:Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).题目要求遍历二叉树并且按照分层保存,个人想法就是随意访问,然后按照层次来保存就行了,相对比较简单。/** * Definition for a binary tree node. * struct TreeNode { * int val; *原创 2020-11-25 22:05:52 · 89 阅读 · 0 评论 -
leetcode289 - Game of Life(faster than 100.00% and less than 85.57% )
题目:According to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.”Given a board with m by n cells, each cell has an initial state live (1) or de原创 2020-11-25 21:34:32 · 58 阅读 · 0 评论 -
leetcode328 - Odd Even Linked List
题目:Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in place. The program should run in O(1) space complexity原创 2020-11-25 10:05:19 · 93 阅读 · 0 评论 -
leetcode215 - Kth Largest Element in an Array
题目:Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.说实话这道题我还是挺迷惑的,因为题目并没有要求不能排序,所以不知道为啥还有medium的难度 。class Solution {public: int findKthLargest(vector<int原创 2020-11-25 09:10:47 · 59 阅读 · 0 评论 -
leetcode - Rotate Image
题目:You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotatio原创 2020-11-24 22:28:40 · 85 阅读 · 0 评论 -
leetcode238 - Product of Array Except Self
题目:Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].这道题本身不难,难点主要在于要求不使用除法,并且时间和空间要求O(N).主要思路就是采用一次正向扫描和一次反向扫描来求。class Solution {public:原创 2020-11-24 22:15:29 · 105 阅读 · 1 评论 -
leetcode230 - Kth Smallest Element in a BST
题目:Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.这道题就是遍历BST,取出第k小的数。只需要取出中序遍历二叉树的第k个数据就行了/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeN原创 2020-11-24 21:22:32 · 94 阅读 · 0 评论 -
leetcode78 - Subsets(faster 100%)
题目:Given an integer array nums, return all possible subsets (the power set).The solution set must not contain duplicate subsets.这道题就是排列组合,不过要求子集必须是有序的。这道题开始的时候我费了很久的时间去排序, 最后发现结果不要求和样例输出给的顺序相同,哭了。class Solution {public: bool cmp(vector<int>a原创 2020-11-24 21:04:44 · 66 阅读 · 0 评论 -
leetcode22 - Generate Parentheses
题目:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.这道题题意相对简单,就是生成正确的匹配的括号。class Solution {public: void generate(int left, int right, string str, vector<string>&parenthesis){ i原创 2020-11-24 20:40:11 · 67 阅读 · 0 评论 -
leetcode234 - Palindrome Linked List
题目:Given a singly linked list, determine if it is a palindrome.本题的难点主要是要求时间复杂度为O(n),空间复杂O(1),我个人的思路就是先统计一下链表长度,然后逆置后半部分的链表,再一一比较。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : va原创 2020-11-23 20:38:18 · 80 阅读 · 0 评论 -
leetcode141 - Linked List Cycle
题目:Given head, the head of a linked list, determine if the linked list has a cycle in it.There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to原创 2020-11-23 19:53:56 · 67 阅读 · 0 评论 -
leetcode204 - Count Primes
题目:Count the number of prime numbers less than a non-negative number, n.这道题题意很简单,就是计算小于n的素数的个数。看到题的第一眼想到的是暴力解决,果不其然超时了TnT,只能换个方法。我能想到的方法是用一个数组来存储,记录下那些不是素数的,然后就减少了很多的运算。随便最后时间复杂度也很高(只高于10%),但总归是过了。class Solution {public: int countPrimes(int n) {原创 2020-11-21 21:24:40 · 79 阅读 · 0 评论 -
leetcode160 - Intersection of Two Linked Lists
题目描述(easy):Write a program to find the node at which the intersection of two singly linked lists begins.本题就是要寻找两个链表交汇的点,方法很多,最简单直接的方法就是找出两个链表中较长的一个,遍历长的直到两条链表长度相同时再两个一起遍历,相等时就是交汇点。/** * Definition for singly-linked list. * struct ListNode { * i原创 2020-11-18 21:29:35 · 66 阅读 · 0 评论 -
leetcod326 - Power of Three
题目:Given an integer n, return true if it is a power of three. Otherwise, return false.An integer n is a power of three, if there exists an integer x such that n == 3x.这道题没啥说的,就是看n是不是3的x次方,质量不高。class Solution {public: bool isPowerOfThree(int n)原创 2020-11-18 21:18:28 · 49 阅读 · 0 评论 -
leetcode198 - House Robber
题目:You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automa原创 2020-11-18 20:55:01 · 89 阅读 · 0 评论 -
leetcode-2. Add Two Numbers(medium)
题意简单,就是将链表对应位置相加,只需要遍历两个链表,然后对应相加,再看看没加完的数据,复制到新链表中就行了。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} *原创 2020-11-16 22:25:29 · 59 阅读 · 0 评论