![](https://img-blog.csdnimg.cn/20200525131027242.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
LeetCode
面试程序刷题库
优惠券已抵扣
余额抵扣
还需支付
¥29.90
¥99.00
购买须知?
本专栏为图文内容,最终完结不会低于15篇文章。
订阅专栏,享有专栏所有文章阅读权限。
本专栏为虚拟商品,基于网络商品和虚拟商品的性质和特征,专栏一经购买无正当理由不予退款,不支持升级,敬请谅解。
漠宸离若
这个作者很懒,什么都没留下…
展开
-
题242. 有效的字母异位词
Javaclass Solution { public boolean isAnagram(String s, String t) { char[] ch_s = s.toCharArray(); char[] ch_t = t.toCharArray(); Arrays.sort(ch_s); Arrays.sort(ch_t); return Arrays.equals(ch_s,ch_t); ...原创 2020-11-22 07:53:31 · 130 阅读 · 0 评论 -
题148. 排序链表
Java/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode sortList(ListNode head) { return head == null ...原创 2020-11-22 00:00:42 · 172 阅读 · 0 评论 -
题147. 对链表进行插入排序
C++/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* insertionSortList(ListNode* head) { ...原创 2020-11-20 07:56:07 · 192 阅读 · 0 评论 -
题283. 移动零
C++class Solution {public: void moveZeroes(vector<int>& nums) { //双指针法,first与i. //first 依次遍历,当nums[i]不为0则放入first位置。 //first遍历完,first<n,则剩余长度的值都为0; int first = 0; int n = nums.size(); ...原创 2020-11-19 19:55:16 · 120 阅读 · 0 评论 -
题134. 加油站
C++class Solution {public: int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { int n=gas.size(); int j=-1; int sum=0,total=0; for(int i=0;i<n;i++){ sum+=gas[i]-c...原创 2020-11-18 07:32:09 · 207 阅读 · 0 评论 -
题1030. 距离顺序排列矩阵单元格
C++class Solution {public: vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0) { vector<vector<int>>rec(R*C,vector<int>(3)); int num=0; for(int i=0;i<R;i++){ ...原创 2020-11-17 23:41:18 · 186 阅读 · 0 评论 -
题406. 根据身高重建队列
Pythonclass Solution(object): def reconstructQueue(self, people): """ :type people: List[List[int]] :rtype: List[List[int]] """ people.sort(key=lambda (h, k): (-h, k)) res = [] for p in p...原创 2020-11-16 12:42:40 · 134 阅读 · 0 评论 -
题402. 移掉K位数字
C++class Solution {public: string removeKdigits(string num, int k) { if(k==num.length()) return "0"; stack <char> s; for(int i=0;i<num.length();i++) { while(s.empty() == false &&a...原创 2020-11-15 20:03:06 · 122 阅读 · 0 评论 -
题1122. 数组的相对排序
C++class Solution {public: vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) { //常规解法,两重循环先把出现在arr中的数字输出,再排序一下,再输出剩下的数 vector<int> result; for(int i = 0; i < arr2.size...原创 2020-11-14 09:18:23 · 144 阅读 · 0 评论 -
题922. 按奇偶排序数组 II
Javaclass Solution { public int[] sortArrayByParityII(int[] A) { int i = 0; //偶数 int j = 1; //奇数 int[] arr = new int[A.length]; for (int idx = 0; idx < A.length; idx++) { if(A[idx] % 2 == 0){ ...原创 2020-11-12 20:29:23 · 113 阅读 · 0 评论 -
题514. 自由之路
C++class Solution {public: int findRotateSteps(string ring, string key) { int n = ring.size(), m = key.size(); vector<vector<int>> dp(m + 1, vector<int>(n)); for (int i = m - 1; i >= 0; --i) { ...原创 2020-11-11 20:11:01 · 457 阅读 · 0 评论 -
题31. 下一个排列
Python3class Solution: def nextPermutation(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """ firstIndex = -1 n = len(nums) def reverse(nums, i, ...原创 2020-11-10 08:04:09 · 157 阅读 · 0 评论 -
题1356. 根据数字二进制下 1 的数目排序
Python3class Solution: def sortByBits(self, arr: List[int]) -> List[int]: def _bin(x): res = 0 while x: x = x & (x - 1) # 将最末尾的1变为0 res += 1 return res ...原创 2020-11-06 06:48:04 · 200 阅读 · 0 评论 -
题127. 单词接龙
C++class Solution {public: int ladderLength(string beginWord, string endWord, vector<string>& wordList) { unordered_set<string> wordDict(wordList.begin(), wordList.end()); if (wordDict.find(endWord) == wordDi...原创 2020-11-05 06:50:13 · 199 阅读 · 0 评论 -
题941. 有效的山脉数组
C++class Solution {public: bool validMountainArray(vector<int>& A) { if(A.size () < 3) return false; int idx = 1; while(idx < A.size() - 1 && A[idx-1] < A[idx]) idx++; ..原创 2020-11-03 23:49:01 · 215 阅读 · 0 评论 -
题349. 两个数组的交集
C++class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { set<int>s1; set<int>s2; vector<int>rec; for(int i=0;i<nums1.size();i++)...原创 2020-11-02 06:52:52 · 138 阅读 · 1 评论 -
题140. 单词拆分 II
Python3class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> List[str]: wordDict = set(wordDict) l = len(s) lookup = defaultdict(list) res = [] def digui(s, start): # if s[s...原创 2020-11-02 06:53:07 · 196 阅读 · 0 评论 -
题463. 岛屿的周长
C++class Solution {public: int islandPerimeter(vector<vector<int>>& grid) { if(grid.empty()) { return 0; } if(grid[0].empty()) { return 0; } int res = 0; ...原创 2020-10-31 23:54:06 · 123 阅读 · 0 评论 -
题381. O(1) 时间插入、删除和获取随机元素 - 允许重复
C++class RandomizedCollection {public: /** Initialize your data structure here. */ RandomizedCollection() {} /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */ bool ...原创 2020-10-31 23:45:45 · 136 阅读 · 0 评论 -
题129. 求根到叶子节点数字之和
C++/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int sumNumbers(Tr...原创 2020-10-29 22:31:42 · 60 阅读 · 0 评论 -
题1207. 独一无二的出现次数
C#define MAXN 1010bool uniqueOccurrences(int* arr, int arrSize){ int count[MAXN*2]; int count_count[MAXN*2]; memset(count,0,sizeof(count)); memset(count_count,0,sizeof(count_count)); int i = 0; for(;i<arrSize;i++) { ...原创 2020-10-28 18:56:14 · 122 阅读 · 0 评论 -
题144. 二叉树的前序遍历
Java/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public List<Integer> preorderTraversal(Tree...原创 2020-10-27 06:49:52 · 60 阅读 · 0 评论 -
题1365. 有多少小于当前数字的数字
Python3class Solution: def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: cnt = collections.Counter(nums) for i in range(1, 101): cnt[i] += cnt[i - 1] return [cnt[x - 1] for x in ...原创 2020-10-26 06:46:37 · 88 阅读 · 0 评论 -
题845. 数组中的最长山脉
C++class Solution {public: int longestMountain(vector<int>& A) { int n = A.size(); if (n < 3) return 0; vector<int> left(n, 0); vector<int> right(n, 0); ...原创 2020-10-25 12:03:21 · 155 阅读 · 0 评论 -
题1024. 视频拼接
C++class Solution {public: int videoStitching(vector<vector<int>>& clips, int T) { int n = clips.size(); vector <long long> dp(T + 1, INT_MAX); dp[0] = 0; for (int i = 1; i <= T; i++)...原创 2020-10-24 10:24:31 · 5309 阅读 · 0 评论 -
题234. 回文链表
Javaclass Solution { public boolean isPalindrome(ListNode head) { if(head==null){ return true; } if(head.next==null){ return true; } ListNode pre=null; ListNode slow=head;...原创 2020-10-23 06:37:30 · 119 阅读 · 0 评论 -
题763. 划分字母区间
Javaclass Solution { public List<Integer> partitionLabels(String S) { List<Integer> res=new ArrayList<>(); int[] temp=new int[26]; for(int i=0;i<S.length();i++){ temp[S.charAt(i)-'a']=i;...原创 2020-10-22 06:37:29 · 150 阅读 · 0 评论 -
题 925. 长按键入
Python3class Solution: def isLongPressedName(self, name, typed): """ :type name: str :type typed: str :rtype: bool """ i, name_len = 0, len(name) for j in range(len(typed)): if ...原创 2020-10-21 06:26:55 · 156 阅读 · 2 评论 -
题143. 重排链表
Python3class Solution: def reorderList(self, head): """ :type head: ListNode :rtype: void Do not return anything, modify head in-place instead. """ if head == None or head.next == None: return...原创 2020-10-20 06:30:30 · 138 阅读 · 0 评论 -
题844. 比较含退格的字符串
Javaclass Solution { public boolean backspaceCompare(String S, String T) { return build(S).equals(build(T)); } public String build(String S){ Stack<Character> ans = new Stack(); for ( char c: S.toCharArray...原创 2020-10-19 06:03:43 · 142 阅读 · 0 评论 -
题19. 删除链表的倒数第N个节点
Python3# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: pre_head=L...原创 2020-10-18 06:05:36 · 85 阅读 · 0 评论 -
题52. N皇后 II
Python3class Solution: def totalNQueens(self, n): """ :type n: int :rtype: int """ row = [0 for i in range(n)] def generateBoard(m, row): board = [str() for i in range(n)] ...原创 2020-10-17 07:13:44 · 128 阅读 · 0 评论 -
题977. 有序数组的平方
Python3class Solution: def sortedSquares(self, A): """ :type A: List[int] :rtype: List[int] """ i, j = 0, len(A)-1 res = [0]*len(A) for k in range(len(A)-1, -1, -1): if abs(A[i]...原创 2020-10-16 06:50:08 · 98 阅读 · 0 评论 -
题116. 填充每个节点的下一个右侧节点指针
Python3"""# Definition for a Node.class Node: def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None): self.val = val self.left = left self.right = right self.next =...原创 2020-10-15 06:21:43 · 74 阅读 · 0 评论 -
题1002. 查找常用字符
Javaclass Solution { public List<String> commonChars(String[] A) { //res存放String[] A中重复出现的字符及次数 Map<String,Integer> res=new HashMap(); for(int i=0;i<A.length;i++) { String str=A[i]; ...原创 2020-10-14 06:13:42 · 96 阅读 · 0 评论 -
题24. 两两交换链表中的节点
C++class Solution {public: ListNode* swapPairs(ListNode* head) { if(!head || !head->next) return head; ListNode* dummy = new ListNode(-1),*pre = dummy; dummy->next = head; while(pre->next && pr...原创 2020-10-13 05:52:39 · 64 阅读 · 0 评论 -
题530. 二叉搜索树的最小绝对差
Java/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public int getMinimumDifference(TreeNode root) ...原创 2020-10-12 06:03:27 · 95 阅读 · 0 评论 -
题416. 分割等和子集
C++class Solution {public: bool canPartition(vector<int>& nums) { int sum = accumulate(nums.begin(), nums.end(), 0); if (sum % 2 != 0)return false; int half = sum / 2; int n = nums.size(); vector<bool>dp(half + 1, false...原创 2020-10-11 06:24:29 · 114 阅读 · 0 评论 -
题142. 环形链表 II
C++class Solution {public: ListNode *detectCycle(ListNode *head) { unordered_set<ListNode *> visited; while (head != nullptr) { if (visited.count(head)) { return head; } ...原创 2020-10-10 06:06:53 · 125 阅读 · 1 评论 -
题141. 环形链表
C++/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool hasCycle(ListNode *head) { if(head==NULL||...原创 2020-10-09 06:15:05 · 99 阅读 · 0 评论