原创 leetcode 217: Contains Duplicate

class Solution { public: bool containsDuplicate(vector<int>& nums) { unordered_set<int> set; int n=

原创 leetcode 216: Combination Sum III

class Solution { public: vector<vector<int>> combinationSum3(int k, int n) { vector<vector<int> > res;

原创 leetcode 229: Majority Element

Extended from Majority Element. Now there are at most two majority element, so I need four variables to implement the m

原创 leetcode 233: Number of Digit One

This problem is a little hard to understand. You can write some numbers and calculate the number of Digit Ones by hand

原创 leetcode 214: Shortest Palindrome

Use KMP algorithm. First, get the reverse of the string s called new_s, and add s, a special character ('#') and new_s

原创 leetcode 3: Longest Substring Without Repeating Characters

Use a window to get a substring. The end of the window keeps forward and once it meets a character that is already in t

原创 leetcode 109: Convert Sorted List to Binary Search Tree

Use the fast slow method to find the mid point of the list and make it the root. Then do it recursively on both sides.

原创 leetcode 6: ZigZag Conversion

In one line, the first character is s[i], the second is s[i+2*numRows-2-2*i] and the third is s[i+2*numRows-2]. Just be

原创 leetcode 5: Longest Palindrome Substring

Use DP. dp[i][j] means whether the substring i to j is a palindrome. class Solution { public: string longestPalind

原创 leetcode 1: Two Sum

Use a unordered map to save all visited number. While scan the array, find out whether target-nums[i] is already visite

原创 leetcode 8: Sting to Integer (atoi)

class Solution { public: int myAtoi(string str) { int len=str.length(); int i=0; while(i<le

原创 leetcode 9: Palindrome Number

Reverse the number and compare it with the original one. class Solution { public: bool isPalindrome(int x) {

原创 leetcode 321: Create Maximum Number

The algorithm is quite difficult, you need to think step by step to come up with the idea. I learned the algorithm here

原创 leetcode 241: Different Ways to Add Parentheses

Learn from https://leetcode.com/discuss/48488/c-4ms-recursive-%26-dp-solution-with-brief-explanation. Recursion method,

原创 leetcode 237: Delete Node in a Linked List

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(i