【【leetcode】Next Permutation(middle)】教程文章相关的互联网学习教程文章

Leetcode刷题记(15) —加1【图】

题目要求:给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。你可以假设除了整数 0 之外,这个整数不会以零开头。题解:该题较简单,只需要注意进位就行,;还有重要的一点是当第一位为9时原数组需要添加一个元素: 原文:https://www.cnblogs.com/wangjm63/p/11518921.html

leetcode第一刷_Rotate Image

这个题该怎么说呢,旋转又要求inplace,一般就是要找到某种规律了,这个还是很明显的,画一下原来的,再画一下旋转之后的,看看原来的跑到什么位置了。牵扯到四个位置按顺时针方向互换一下位置,发现只要做三次交换就可以实现,好神奇。最后需要注意调整到什么时候结束,如果n是偶数的话,到n/2就是最里层了,不需要继续旋转。奇数其实也是,正好少了最里面的那个只有一个数的层。class Solution { public:void rotate(vector<vect...

LeetCode - 17. Letter Combinations of a Phone Number

17. Letter Combinations of a Phone Number Problem‘s Link ---------------------------------------------------------------------------- Mean: 给你一个数字串,输出其在手机九宫格键盘上的所有可能组合.analyse:使用进位思想来枚举所有组合即可.Time complexity: O(N) view code/** * ----------------------------------------------------------------- * Copyright (c) 2016 crazyacking.All rights reserved. * ------...

LeetCode - Remove Duplicates from Sorted Array【代码】

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.For example,Given input array A = [1,1,2],Your function should return length = 2, and A is now [1,2].解法很简单,边界条件A为null或者长度为0,返回0;否则,用variable size来维护题目要求的数...

Leetcode 827. 最大人工岛(连通块)【代码】

827. 最大人工岛 难度困难83 在二维地图上, 0代表海洋, 1代表陆地,我们最多只能将一格 0 海洋变成 1变成陆地。 进行填海之后,地图上最大的岛屿面积是多少?(上、下、左、右四个方向相连的 1 可形成岛屿) 示例 1: 输入: [[1, 0], [0, 1]] 输出: 3 解释: 将一格0变成1,最终连通两个小岛得到面积为 3 的岛屿。示例 2: 输入: [[1, 1], [1, 0]] 输出: 4 解释: 将一格0变成1,岛屿的面积扩大为 4。示例 3: 输入: [[1, 1], [1, 1]]...

[LeetCode]112 Path Sum【代码】

https://oj.leetcode.com/problems/path-sum/http://blog.csdn.net/linhuanmars/article/details/23654413/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; ...

LeetCode 527. Word Abbreviation【代码】

Greedy先生成abbreviation,如果有重复,对所有重复的prefix再添加一个字符,直到没有重复为止。class Solution { public:vector<string> wordsAbbreviation(vector<string>& dict) {int n=dict.size();vector<string> res(n);vector<int> prefix(n,0); //prefix[i] = to what index is the prefixfor (int i=0;i<n;++i){res[i] = abbrev(dict[i],prefix[i]);}for (int i=0;i<n;++i){ while (true){vector<int> dupes;for (int j=i...

[LeetCode] 1189. Maximum Number of Balloons【代码】【图】

Given a string text, you want to use the characters of text to form as many instances of the word "balloon" as possible.You can use each character in text at most once. Return the maximum number of instances that can be formed.Example 1:Input: text = "nlaebolko" Output: 1 Example 2:Input: text = "loonbalxballpoon" Output: 2 Example 3:Input: text = "leetcode" Output: 0Constraints:1 <= text.length <...

LeetCode Minimum Size Subarray Sum【代码】

原题链接在这里:https://leetcode.com/problems/minimum-size-subarray-sum/ 维护一个window, 当sum<s时一直移动window 的 right index, 同时更新sum.当sum >= s后一直移动window left index, 同时更新sum 和 res.最后返回时看res 是否 被更新过,若被更新过就返回 res, 没有更新过说明没有符合要求的window, 返回0.Note: right的初始值是0, 所以每次更新sum 后 right 是指向了下个没有加进sum里的数值. 外循环while中的第二部分检...

【LeetCode】7. Reverse Integer【代码】

题目Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321简单题,需要考虑两个问题,1)如果输入的末尾是0怎么办?2)如果倒过来的数字溢出了怎么办?class Solution { public:int reverse(int x) {int flag = 1;unsigned res = 0;;if (x < 0){flag = -1;x = flag * x;}while (x > 0){if (res > (INT_MAX - x%10)/10)return0;res = res*10 + x%10;x = x/10;}return flag * (int)res;} }; ...

[Leetcode Weekly Contest]184【代码】

链接:LeetCode[Leetcode]5380. 数组中的字符串匹配给你一个字符串数组 words ,数组中的每个字符串都可以看作是一个单词。请你按 任意 顺序返回 words 中是其他单词的子字符串的所有单词。 如果你可以删除\(words[j]\)最左侧和/或最右侧的若干字符得到\(word[i]\),那么字符串\(words[i]\) 就是\(words[j]\)的一个子字符串。直接暴力就可以了。class Solution:def stringMatching(self, words: List[str]) -> List[str]:res = []f...

[LeetCode] 547. Friend Circles【代码】

https://leetcode.com/problems/friend-circlespublicclass Solution {int[] id;int[] weight;publicint findCircleNum(int[][] M) {if (M == null || M.length == 0 || M[0].length == 0) {return 0;}initUnionFind(M.length);Set<Integer> set = new HashSet<>();for (int i = 0; i < M.length; i++) {for (int j = 0; j < M[0].length; j++) {if (i == j) {continue;}if (M[i][j] == 1) {union(i, j);}}}for (int i = 0; i < id...

19.3.4 [LeetCode 102] Binary Tree Level Order Traversal【代码】

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] ] 1class Solution {2public:3 vector<vector<int>> levelOrder(TreeNode* root) {4 queue<TreeNode*>q, p;5 vector<vector<int>>ans;...

【Leetcode】Container With Most Water【代码】【图】

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. 1class Solution {2public:3int maxArea(vector<int> &height) {4int i = 0, j = height.size()...

【LeetCode/力扣】#1720 - 解码异或后的数组【代码】

1 题目描述 题目链接:https://leetcode-cn.com/problems/decode-xored-array/未知 整数数组 arr 由 n 个非负整数组成。 经编码后变为长度为 n - 1 的另一个整数数组 encoded ,其中 encoded[i] = arr[i] XOR arr[i + 1] 。例如,arr = [1,0,2,1] 经编码后得到 encoded = [1,2,3] 。 给你编码后的数组 encoded 和原数组 arr 的第一个元素 first(arr[0])。 请解码返回原数组 arr 。可以证明答案存在并且是唯一的。示例 1: 输入:...