![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode刷题C++
文章平均质量分 73
CodingBoy121
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
展开
-
trie树字典树的插入和删除
#include#includeusing namespace std;typedef struct Trie_node{ int count; // 统计单词前缀出现的次数 struct Trie_node* next[26]; // 指向各个子树的指针 bool exist; // 标记该结点处是否构原创 2016-10-11 14:18:58 · 1463 阅读 · 0 评论 -
leetcode刷题系列--442. Find All Duplicates in an Array
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements that appear twice in this array.Could you do it without extra原创 2016-11-23 15:27:40 · 396 阅读 · 0 评论 -
leetcode刷题系列 448. Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Coul原创 2016-11-23 15:11:12 · 379 阅读 · 0 评论 -
leetcode刷题--459. Repeated Substring Pattern
class Solution {public: bool repeatedSubstringPattern(string str) { vector items(26,0); int len = str.size(); int mid = len / 2; int flag = 0; for(int i =原创 2016-11-22 18:06:09 · 332 阅读 · 0 评论 -
leetcode刷题-76. Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BANC".原创 2016-11-02 17:52:31 · 528 阅读 · 0 评论 -
leetcode刷题系列-Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.Strings consists of lowercase English letters only and the length of both strings s and p will not be lar原创 2016-11-01 21:18:58 · 248 阅读 · 0 评论 -
leetcode刷题 447. Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of points (i, j, k) such that the distance between i andj equals the distance between i and k (the order of t原创 2016-11-28 20:48:42 · 318 阅读 · 0 评论 -
leetcode刷题系列:392. Is Subsequence
Given a string s and a string t, check if s is subsequence of t.You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) strin原创 2016-10-31 12:03:18 · 627 阅读 · 0 评论 -
leetcode刷题系列-389. Find the Difference
Given two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add one more letter at a random position.Find the letter that was原创 2016-10-14 10:47:36 · 216 阅读 · 0 评论 -
leetcode刷题系列--152. Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest pr原创 2016-11-23 16:55:41 · 254 阅读 · 0 评论 -
leetcode刷题系列--206. Reverse Linked List 递归和非递归 c++实现
Reverse a singly linked list.click to show more hints.Subscribe to see which companies asked this questionShow TagsShow Similar ProblemsHave you met this ques原创 2016-12-06 18:23:59 · 623 阅读 · 0 评论 -
leetcode刷题系列:不用加号实现a+b
题目描述:实现a+b,但是不用加号解析:a和b的异或运算被称作没有进位的加法运算。其实可以想象,如果1和1想加 结果为0进位为1 0和0相加 还是0 1和0相加 还有0和1相加都是一 刚好符合异或运算的规则,然后现在要解决的就是进位问题,进位可以通过与运算来解决,与运算后的结果如果左移一位,刚好是进位后的结果,此时用这两个结果再做一次上面描述的过程,那么直到进位为0的时候,就是最终的结原创 2016-12-07 15:38:50 · 3879 阅读 · 1 评论 -
一个长度为2N的数组,前面N个是数字,后面N个是字母,类似123abc,让转化为1a2b3c
#include using namespace std;#include #include void swap(int& a,int& b){ int temp = a; a = b; b = a;}void convert(vector& ele,int gap){ int len = ele.size();原创 2016-10-09 18:54:46 · 997 阅读 · 0 评论 -
八皇后问题
void backtrace(int t){ if(t > num) { sum++; for(int m = 1; m < num; m ++) { cout << x[m]; } cout <<end; } else for(int i = 1; i <= num; i ++) { x[t] =原创 2017-01-23 15:21:55 · 357 阅读 · 0 评论 -
c++实现数字的全排列
#include #include using namespace std;void recursivePrint(vector number,int length,int index){ if(index == length - 1) { for(int i = 0 ; i原创 2016-08-31 11:44:17 · 3736 阅读 · 0 评论 -
leetcode刷题-Lowest Common Ancestor of a Binary Tree
236. Lowest Common Ancestor of a Binary Tree Add to ListQuestionEditorial Solution My SubmissionsTotal Accepted: 76416Total Submissions: 261454Difficulty: MediumContr原创 2016-12-29 16:58:01 · 255 阅读 · 0 评论 -
415. Add Strings
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.Note:The length of both num1 and num2 is Both num1 and num2 contains only digits 0-9.B原创 2016-11-28 16:46:54 · 459 阅读 · 0 评论 -
LeetCode 453. Minimum Moves to Equal Array Elements
Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.Example:Input:[1,2,3]Ou原创 2016-11-28 16:05:52 · 514 阅读 · 0 评论 -
常见面试算法题整理
下面列的所有的算法题目基本都能在本博客中找到 在使用本博文的过程中,可以按照天来复习,每天一个list,完成这些 基本能应付国内互联网公司的算法问题。第一天1.给定一个链表 在链表的尾部插入一个元素2.用两个队列实现一个栈3.用两个栈实现一个队列4.求两个有序数组的中位数,可以延伸为求两个有序数组的第k大数,要求复杂度最低(使用二分的思想来做,复杂度logN)5.atoi原创 2016-12-07 16:01:01 · 540 阅读 · 0 评论 -
一个数字二进制中一的个数
int countOneByBinary(int n){ int count = 0; while(n != 0) { int tmp = n & 1; if(tmp == 1) count++; n = n >> 1; } return count;}原创 2016-12-07 15:48:19 · 599 阅读 · 0 评论 -
leetcode刷题系列-Third Maximum Number
414. Third Maximum Number QuestionEditorial Solution My SubmissionsTotal Accepted: 1209Total Submissions: 4256Difficulty: EasyContributors: ZengRed , 1337c0d3rGiven原创 2016-10-18 11:32:51 · 628 阅读 · 0 评论 -
leetcode Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence.For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], theref原创 2016-10-14 17:08:55 · 600 阅读 · 0 评论 -
404. Sum of Left Leaves - leetcode
Find the sum of all left leaves in a given binary tree.Example: 3 / \ 9 20 / \ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.原创 2016-10-14 10:32:18 · 593 阅读 · 0 评论 -
leetcode刷题系列C++-remove element
Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.原创 2016-02-19 16:57:56 · 247 阅读 · 0 评论 -
leetcode刷题系列C++-3sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c原创 2016-02-19 16:25:33 · 387 阅读 · 0 评论 -
leetcode刷题系列C++-4sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:Element原创 2016-02-19 15:43:56 · 262 阅读 · 0 评论 -
leetcode刷题系列C++-3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exact原创 2016-02-19 15:17:43 · 372 阅读 · 0 评论 -
leetcode刷题系列C++-Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3原创 2016-02-17 14:12:16 · 672 阅读 · 0 评论 -
leetcode刷题系列C++-Search in Rotated Sorted Array II
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).Subscribe to see whic原创 2016-02-01 14:54:13 · 290 阅读 · 0 评论 -
leetcode刷题系列C++-Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the原创 2016-01-29 11:15:28 · 342 阅读 · 0 评论 -
leetcode刷题系列C++-Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array retur原创 2016-01-28 17:48:40 · 588 阅读 · 0 评论 -
leetcode刷题系列C++-next permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible原创 2016-02-19 18:00:58 · 261 阅读 · 0 评论 -
leetcode刷题系列C++-valid sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A partially fille原创 2016-02-21 18:05:41 · 396 阅读 · 0 评论 -
leetcode刷题系列C++-rotate image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?Subscribe to see which companies asked this que原创 2016-02-22 11:18:26 · 539 阅读 · 0 评论 -
reverse integer
里面对于越界部分的处理很不错 很简洁 用long long来存储数据 防止了出现越界的情况。class Solution {public: int reverse(int x) { //首先判断第一位是不是符号位 然后将后面的用 long long sum = 0; while(x != 0) {原创 2016-08-09 14:17:58 · 228 阅读 · 0 评论 -
Longest Palindromic Substring 最长回文子串
这个题目有很多经典解法,嘴笨的就是N的三次方的解法,这里就不说了,下面是n的平方的解法,是从中间向两边查找的解法。class Solution {public: string longestPalindrome(string s) { int n = s.length(); if (n == 0) return ""; stri原创 2016-08-08 21:31:36 · 255 阅读 · 0 评论 -
Medium of Two Sorted Arrays
//递归的二分查找class Solution {public: double findMedianSortedArrays(vector& nums1, vector& nums2) { int m = nums1.size(); int n = nums2.size(); if ((m+n)%2 == 0)原创 2016-08-08 21:06:57 · 616 阅读 · 0 评论 -
快速排序精简版总容易理解版
#include#includeusing namespace std;void fast_sort(vector& a,int begin,int end){ if(begin < end) { int flag = a[begin]; int i = begin; int j = end; while(i < j) { while(i = flag)原创 2016-08-08 18:11:40 · 293 阅读 · 0 评论 -
leetcode刷题系列C++-single number II
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without usi原创 2016-02-22 17:29:13 · 524 阅读 · 0 评论 -
leetcode刷题系列C++-single number I
Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using ext原创 2016-02-22 16:42:40 · 455 阅读 · 0 评论