[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++实现]938. Range Sum of BST

Range Sum of BST

常规解法,递归遍历二叉搜索树的每个元素,然后将符合条件的元素相加。

/**

* Definition for a binary tree node.

* struct TreeNode {

* int val;

* TreeNode *left;

* TreeNode *right;

* TreeNode() : val(0), left(nullptr), right(nullptr) {}

* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}......

[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

......

[LeetCode C实现] 167. Two Sum II - Input array is sorted

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.

Note:

Your re......