【【LeetCode】224. 基本计算器 Basic Calculator II(C++)】教程文章相关的互联网学习教程文章

[C++]LeetCode: 78 Unique Paths II【代码】

题目:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectivelyin the grid.For example,There is one obstacle in the middle of a 3x3 grid as illustrated below.[[0,0,0],[0,1,0],[0,0,0] ] The total number of unique paths is 2.Note: m and n will be at most 100.思路:这道题和Leet...

leetcode-第k个排列(Java和c++版)【代码】

第k个排列给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列。按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:"123""132""213""231""312""321"给定 n 和 k,返回第 k 个排列。说明:给定 n 的范围是 [1, 9]。给定 k 的范围是[1, n!]。示例 1:输入: n = 3, k = 3输出: "213"示例 2: 输入: n = 4, k = 9输出: "2314" 思路:观察一下: 我们取k=17。首先数组都是从0开始计数的,所以k-1=16。从16开始,先可以...

leetcode566 C++ 36ms 矩阵reshape【代码】

class Solution { public:vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {if(nums.empty() || nums[0].empty()){return {};}auto h = nums.size();auto w = nums[0].size();if(h*w != r*c){return nums;}vector<vector<int>> res(r, vector<int>(c, 0));for(int i=0;i<r;i++){for(int j=0;j<c;j++){res[i][j] = nums[(i*c + j)/w][(i*c + j)%w];}}return res;} };原文:https://www.cnblogs.com/th...

LeetCode 561. Array Partition I(easy难度c++)【代码】【图】

题目: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.Example 1: Input: [1,4,3,2]Output: 4 Explanation: n is 2, and the maximum sum of pairs is 4. Note: n is a positive integer, which is in the range of [1, 10000]. All the integers in the arra...

LeetCode107 树·二叉树的层次遍历II(C++)【代码】

题目描述:给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)例如:给定二叉树 [3,9,20,null,null,15,7], 3/ 9 20/ 15 7 返回其自底向上的层次遍历为:[[15,7],[9,20],[3] ]/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NU...

leetcode169——Majority Element (C++)【代码】【图】

Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times.You may assume that the array is non-empty and the majority element always exist in the array.个人博客:http://www.cnblogs.com/wdfwolf3/。题目比较好理解,在含有n个元素的数组中找出出现次数超过?n/2?的元素,假设数组不为空而且这个数是一定存在的。1.moore-voting算法这个算法就是为...

leetcode 78. 子集(c++)【代码】

给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。说明:解集不能包含重复的子集。示例:输入: nums = [1,2,3]输出:[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]class Solution { public:vector<vector<int>> subsets(vector<int>& nums) {vector<vector<int> > res(1);sort(nums.begin(), nums.end());for (int i = 0; i < nums.size(); ++i) {int size = res.size();for (int j = 0; j <...

LeetCode 674. Longest Continuous Increasing Subsequence最长连续递增序列 (C++/Java)【代码】

题目:Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).Example 1:Input: [1,3,5,4,7] Output: 3 Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. Even though [1,3,5,7] is also an increasing subsequence, it‘s not a continuous one where 5 and 7 are separated by 4. Example 2:Input: [2,2,2,2,2] Output: ...

LeetCode 35 Search Insert Position (C,C++,Java,Python)

Problem: Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0Solution:二分查找,当找不到时l=r+1,所以根据最后一次l和r的变动来判定应该插入的位置,如果最后一次是l=mid+...

LeetCode 62. Unique Paths不同路径 (C++/Java)【代码】【图】

题目:A robot is located at the top-left corner of a m x n grid (marked ‘Start‘ in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish‘ in the diagram below).How many possible unique paths are there?Above is a 7 x 3 grid. How many possible unique paths are there?Note: m and n will...

LeetCode 102. Binary Tree Level Order Traversal02. 二叉树的层次遍历 (C++)【代码】

题目:Given a binary tree, return the level order traversal of its nodes‘ values. (ie, from left to right, level by level).For example:Given binary tree [3,9,20,null,null,15,7], 3/ 9 20/ 15 7 return its level order traversal as:[[3],[9,20],[15,7] ]分析:给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。可以立刻想到两种方法,也就是BFS和DFS。先说BFS,BFS实际...

C++ 判断对称二叉树的递归和非递归做法 [LeetCode 101]【代码】

题目:给定一个二叉树,检查它是否是镜像对称的。例如,二叉树 [1,2,2,3,4,4,3] 是对称的。1/22/ \ /3443 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:1/22\ 33链接:https://leetcode-cn.com/problems/symmetric-tree最开始的思路是用层次遍历,每一行看看是不是回文数组,但后来发现例如样例2那样的树就会判断错误,递归也没想好怎么个调用思路。 正确的递归思路:来源:https://leetcode-cn.com/u/haventmetyo...

[Leetcode学习-c++&java]Reorder Data in Log Files(比较日志数据)【代码】

问题: 难度:easy 说明: 感觉不像是 leetcode 的 easy 题,题目实际要求其实是: 输入给出一个 String[] ,日志里面都是 空格字符隔开,只有 小写字母和 数字,空格隔开各个字符串都是 先小写字母 + 后数字, 然后 String[i] 第一个 空格前面的字符串 作为符号,将 符号后面的所有 空格以及字母字符 按照 ASCII码表排序,而将所有数字字符 的 排序认为比小写字母还要后,相当于 ASCII 码表里面,数字的码表值比 小写字母大: ...

【LeetCode】C++ :简单题 - 字符串 1370. 上升下降字符串【代码】

1370. 上升下降字符串 难度简单83 给你一个字符串 s ,请你根据下面的算法重新构造字符串: 从 s 中选出 最小 的字符,将它 接在 结果字符串的后面。从 s 剩余字符中选出 最小 的字符,且该字符比上一个添加的字符大,将它 接在 结果字符串后面。重复步骤 2 ,直到你没法从 s 中选择字符。从 s 中选出 最大 的字符,将它 接在 结果字符串的后面。从 s 剩余字符中选出 最大 的字符,且该字符比上一个添加的字符小,将它 接在 结果字...

【LeetCode】C++ :简单题 - 字符串 917. 仅仅反转字母【代码】

917. 仅仅反转字母 难度简单73 给定一个字符串 S,返回 “反转后的” 字符串,其中不是字母的字符都保留在原地,而所有字母的位置发生反转。 示例 1: 输入:"ab-cd" 输出:"dc-ba"示例 2: 输入:"a-bC-dEf-ghIj" 输出:"j-Ih-gfE-dCba"示例 3: 输入:"Test1ng-Leet=code-Q!" 输出:"Qedo1ct-eeLg=ntse-T!" 提示: S.length <= 10033 <= S[i].ASCIIcode <= 122 S 中不包含 \ or "双指针 class Solution { public:string rever...