[LeetCode C++实现]86.Partition List

86.Partition List

解法与思路:

将小于某一值x的结点加到pless所指向的结点,将大于某一直x的结点加到pbigger所指向的结点。需要注意内存泄露与将pbigger最后一个结点next置为NULL。

/**

* Definition for singly-linked list.

* struct ListNode {

* int val;

* ListNode *next;

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

* };

*/

class Solution {

public:

ListNode......

[LeetCode C++实现]189.Rotate Array

189.Rotate Array

解法一,空间复杂度O(n)

class Solution {

public:

void rotate(vector<int>& nums, int k) {

int length = nums.size();

if(k <= 0 || length <= 1)

return;

//copy vector nums

vector<int> copy_nums;

vector<int>::iterator it = copy_nums.begin();

copy_nums.insert(it,n......

[LeetCode C++实现]13. Roman to Integer

13. Roman to Integer

罗马数字转整数,写了一堆if else之后打算改成switch,等等,我刷leetcode不是想学c++吗,那用c++应该会简单不少。

解法如下:

class Solution {

public:

int romanToInt(string s)

{

map<char, int> T = { { 'I' , 1 },

{ 'V' , 5 },

{ 'X' , 10 },

{ 'L' , 50 },

{ 'C' , 100 },

{ 'D' , 500 },

{ 'M' , 1000 } };

int sum = T[s.bac......

[LeetCode C++实现]1030. Matrix Cells in Distance Order

leetcode 1030

学习c++ sort的基本用法之后,这道题还是比较easy.

Beginners guide to the std::sort() function

解法一:

class Solution {

public:

vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0) {

vector<vector<int>> res(R*C,vector<int>(3));

int num=0;

for(int i=0;i<......

[LeetCode C实现]28. Implement strStr()

Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

Input: haystack = "hello", needle = "ll"

Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"

Output: -1......

[LeetCode C实现]647. Palindromic Substrings

Given a string, your task is to count how many palindromic substrings in this string.

The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.

Example 1:

Input: "abc"

Output: 3

Explanation: Three palindr......

[LeetCode C实现]832. Flipping an Image

Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.

To flip an image horizontally means that each row of the image is reversed. For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].

To invert an image means that each ......

[LeetCode C实现]762. Prime Number of Set Bits in Binary Representation

Given two integers L and R, find the count of numbers in the range L, R having a prime number of set bits in their binary representation.

(Recall that the number of set bits an integer has is the number of 1s present when written in binary. For example, 21 written in binary is 10101 which has 3 ......

[LeetCode C实现] 821. Shortest Distance to a Character

Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string.

Example 1:

Input: S = "loveleetcode", C = 'e'

Output:

Note:

S string length is in [1, 10000].

C is a single character, and guara......

[LeetCode C 实现]653. Two Sum IV - Input is a BST

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.

Example 1:

Input:

5

/ \

3 6

/ \ \

2 4 7

Target = 9

Output: True

Example 2:

Input:

5

/ \

3 6

/ \ \

2 4 7

......