[LeetCode C++实现]438. Find All Anagrams in a String

使用滑动窗口算法求解:

class Solution {

public:

vector<int> findAnagrams(string s, string p) {

vector<int> res;

unordered_map<char,int> need,window;

int left = 0,right = 0,match_cnt = 0;

//更新需要命中的全部字符

for(auto c:p) need[c] += 1;

while(right < s.size()){

char c = s[right];

right++;

if(n......

[LeetCode C++实现]567. Permutation in String

使用滑动窗口算法求解该问题答案:

class Solution {

public:

bool checkInclusion(string s1, string s2) {

unordered_map<char,int> need,window;

//构建需要匹配的字符map

for(auto c:s1) need[c] += 1;

int left = 0,right = 0,match_cnt = 0;

while(right < s2.size()){

char c = s2[right];

right++;

if(need.count(c))

{

window[c......

[LeetCode C++实现]76. Minimum Window Substring

首先使用暴力破解求解:

使用滑动窗口的解法:

class Solution {

public:

string minWindow(string s, string t) {

int ssize = s.size(),tsize = t.size();

if(ssize < tsize) return "";

unordered_map<char, int> need, window;

//初始化需要包含的字符map

for (auto c : t) need[c]++;

int left = 0, right = 0;

int match_cnt = ......

[LeetCode C++实现]9.Palindrome Number

Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward. For example, 121 is palindrome while 123 is not.

最暴力的解法,使用vector 等缓存:

class Solution {

public:

bool isPalindrome(int x) {

if(x < 0) return false;

vector<i......

[LeetCode C++实现]4. Median of Two Sorted Arrays

先不考虑log(m+n)复杂度实现,使用最直观的暴力破解方法(合并vector,排序,时间复杂度O((m+n)*log(m+n)).

class Solution {

public:

double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {

for(auto num:nums2){

nums1.push_back(num);

}

std::sort(nums1.begin(),nums1.end());

int n = nums1.size();

if(n % ......

[LeetCode C++实现]5. Longest Palindromic Substring

首先使用暴力破解方法求解:

class Solution {

public:

string longestPalindrome(string s) {

int n = s.size();

if(n < 2) return s;

int maxlen = 1,begin = 0;

for(int i = 0;i < n - 1;i++)

{

for(int j = i + 1;j < n;j++)

{

if(j - i + 1 > maxlen && isvalidPalindome(s,i,j))

{

begin = i;

maxlen = j -......

[程序员面试金典 C++实现]面试题 02.04. 分割链表

/**

* Definition for singly-linked list.

* struct ListNode {

* int val;

* ListNode *next;

* ListNode(int x) : val(x), next(NULL) {}

* };

*/

class Solution {

public:

ListNode* partition(ListNode* head, int x) {

if(head == nullptr) return nullptr;

//存储小于x元素

ListNode* less = new ListNode(-1......

[LeetCode C++实现]面试题 02.05. 链表求和

面试题02.04.分割链表,从这道题中收获到很多。

最开始我尝试的时候写的代码如下:

/**

* Definition for singly-linked list.

* struct ListNode {

* int val;

* ListNode *next;

* ListNode(int x) : val(x), next(NULL) {}

* };

*/

class Solution {

public:

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {

int carry = 0;

ListNod......

[程序员面试金典 C++实现]面试题 02.06. 回文链表

palindrome-linked-list-lcci,回文链表题目,力扣解法,首先我们使用非常直观的解题方法。

平平无奇 空间O(n)复杂度

/**

* Definition for singly-linked list.

* struct ListNode {

* int val;

* ListNode *next;

* ListNode(int x) : val(x), next(NULL) {}

* };

*/

class Solution {

public:

bool isPalindrome(ListNode* head) {

vector<int......

[程序员面试金典 C++实现]面试题 02.01. 移除重复节点

自己无脑AC的代码:

/**

* Definition for singly-linked list.

* struct ListNode {

* int val;

* ListNode *next;

* ListNode(int x) : val(x), next(NULL) {}

* };

*/

class Solution {

public:

ListNode* removeDuplicateNodes(ListNode* head) {

unordered_set<int> hash_set;

ListNode * p = head,*loc ......