leetcode

Leetcode C++《热题 Hot 100-16》448.找到所有数组中消失的数字

淺唱寂寞╮ 提交于 2020-02-02 12:20:40
Leetcode C++《热题 Hot 100-16》448.找到所有数组中消失的数字 题目 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次。 找到所有在 [1, n] 范围之间没有出现在数组中的数字。 您能在不使用额外空间且时间复杂度为O(n)的情况下完成这个任务吗? 你可以假定返回的数组不算在额外空间内。 示例: 输入: [4,3,2,7,8,2,3,1] 输出: [5,6] 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 思路 不使用额外空间,还需要时间复杂度为n 方案1: 使用额外空间n,时间复杂度为n:用map统计一下数量【或者1-n的桶】;遍历1-n,看在map里面是都存在 方案2: 不使用额外空间,使用冒泡或者快排,时间复杂度在nlogn-n^2之间 方案3: 找到一个特别巧的方法:nums作为桶,类似于桶的冲突方案,如果该桶存在,原来的元素去找自己的桶;思路来源 https://leetcode-cn.com/problems/find-all-numbers-disappeared

LeetCode Top Interview Questions 412. Fizz Buzz (Java版; Easy)

故事扮演 提交于 2020-02-02 09:39:04
welcome to my blog LeetCode Top Interview Questions 412. Fizz Buzz (Java版; Easy) 题目描述 Write a program that outputs the string representation of numbers from 1 to n. But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”. Example: n = 15, Return: [ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" ] 第一次做; 不需要取余操作, 核心: 理解a是b的因数这句话意味着什么 //a是b的因数, 说明b里面有x个a, 也就是让x个a相加便能得到b class Solution { public List < String >

Leetcode C++《热题 Hot 100-12》226.翻转二叉树

纵饮孤独 提交于 2020-02-02 08:31:13
Leetcode C++《热题 Hot 100-12》226.翻转二叉树 题目 翻转一棵二叉树。 示例: 输入: 4 / 2 7 / \ / 1 3 6 9 输出: 4 / 7 2 / \ / 9 6 3 1 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/invert-binary-tree 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 思路 递归大法好理解,递归时间复杂度虽然是n,但是空间复杂度有可能很高,最极端情况递归栈包含了O(height)个函数 递归优化的方法,一般是将其转换成迭代,我们来分析这个题目是否可以转换成迭代 将没有转换左右孩子的依次加入到队列,然后逐个处理,直到队列为空 迭代时间复杂度、空间复杂度都是O(n),棒! 代码 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public : TreeNode * invertTree ( TreeNode *

LeetCode All in One 题目讲解汇总(持续更新中...)

心不动则不痛 提交于 2020-02-02 05:59:19
A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only. We would like to convert the sentence to " Goat Latin" (a made-up language similar to Pig Latin.) The rules of Goat Latin are as follows: If a word begins with a vowel (a, e, i, o, or u), append "ma" to the end of the word. For example, the word 'apple' becomes 'applema'. If a word begins with a consonant (i.e. not a vowel), remove the first letter and append it to the end, then add "ma" . For example, the word "goat" becomes "oatgma" . Add one letter 'a' to the end of each

Leetcode C++《热题 Hot 100-14》283.移动零

喜你入骨 提交于 2020-02-02 01:19:22
Leetcode C++《热题 Hot 100-14》283.移动零 题目 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作,不能拷贝额外的数组。 尽量减少操作次数。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/move-zeroes 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 思路 是一个没做过的题目,参考冒泡swap的方法 //1 0 0 3 12 // 1 3 0 0 12 // 1 3 12 0 0 方案1(196ms,内存10MB):标记0的index-x,找到index-x之后的第一个不为0的数字进行交换, 时间复杂度n*n,比如最坏情况0 0 0 0 0 0 1 方案2(8ms,内存10MB):时间复杂度为n,统计0的个数,直接把非0的移动到最终位置(index位置的最后位置是index-该位置前面0的个数) 方案1的时间复杂度O(n)、空间复杂度O(1) 代码 class Solution { public : void moveZeroes ( vector < int > & nums ) { /*int index_0 = -1

《剑指offer》面试题32----从1到n整数中1出现的次数

不打扰是莪最后的温柔 提交于 2020-02-02 00:21:10
题目: 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数。例如输入12,从1到12这些整数中包含1的数字有1,10,11和12,1一共出现了5次。 解法一:不考虑时间效率的解法(略) ps:我感觉是个程序员都能想到这第一种解法,时间复杂度O(nlogn)。这个方法没有什么意义,但是简单易懂,去小公司足够了,这里不讲了。 解法二:分析数字规律,时间复杂度O(logn). 这是我写这篇文章的初衷。《剑指offer》洋洋洒洒写了几十行代码,然而在leetcode上大神却只用了5行!当天晚上智障,脑子全是浆糊,竟然没有看懂什么意思=。=,我一度怀疑智商受到了碾压。然而在今天睡眠比较充足,头脑比较清醒的情况下终于理顺了思路~ 其实这道题目很多地方都有讲,包括《编程之美》,但是也有20行左右的代码,没耐心了。其它的一些帖子讲的乱七八糟,这对于我这种爱简洁,爱干净,还有严重强迫症的人是不能忍的,下面强迫症患者要开始装逼了。。。 先上代码: 1 package test; 2 3 public class Question_32 { 4 public static int countDigitOne(int n) { 5 int ones = 0; 6 for (long m = 1; m <= n; m *= 10) 7 ones += (n/m + 8) / 10 * m +

[LeetCode] 最长回文子串

情到浓时终转凉″ 提交于 2020-02-01 23:57:04
最长回文子串 题目描述 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。 示例 1 输入: "babad" 输出: "bab" 注意: "aba" 也是一个有效答案。 示例 2 输入: "cbbd" 输出: "bb" 思路1 回文串的意思就是对称,而对称的东西都应该有一个对称中心,那么找回文串只要找到这个对称中心即可。 package com.longhujing.leetcode.t0005; /** * @author longhujing * @date 2020-02-01 */ public class Solution { public String longestPalindrome(String s) { if (s == null || s.length() == 0) { return s; } int start = 0, end = 0; for (int i = 0; i < s.length(); i++) { int len1 = expand(s, i, i); int len2 = expand(s, i, i + 1); int len = Math.max(len1, len2); if (len > (end - start + 1)) { start = i - (len - 1) / 2; end

LeetCode 70 & 509 & 1137

假如想象 提交于 2020-02-01 09:49:32
文章目录 版权声明 1. LeetCode 70 1.1 复杂度分析 1.2 迭代 2. LeetCode 509 2.1 复杂度分析 2.2 递归 2.3 迭代 3. LeetCode 1137 2.1 复杂度分析 2.2 迭代 References 版权声明 LeetCode 系列笔记来源于 LeetCode 题库 1 ,在个人思考的基础之上博采众长,受益匪浅;故今记此文,感怀于心,更多题解及程序,参见 Github 2 ; 该系列笔记不以盈利为目的,仅用于个人学习、课后复习及交流讨论; 如有侵权,请与本人联系(hqpan@foxmail.com),经核实后即刻删除; 本文采用 署名-非商业性使用-禁止演绎 4.0 国际 (CC BY-NC-ND 4.0) 协议发布; 1. LeetCode 70 1.1 复杂度分析 题意解析: LeetCode 70 爬楼梯问题,亦称青蛙跳台阶问题,该问题的数学模型为 Fibonacci 数列; 设 n 级台阶的跳法有 f ( n ) f(n) f ( n ) 种,则由跳台阶规则可知, f ( n ) = f ( n − 1 ) + f ( n − 2 ) f(n)=f(n-1)+f(n-2) f ( n ) = f ( n − 1 ) + f ( n − 2 ) ; 类似的实际问题还有矩形覆盖问题,给定由 2 × 8 2\times 8 2

LeetCode【520】 检测大写字母

南笙酒味 提交于 2020-02-01 07:39:36
题目: 给定一个单词,你需要判断单词的大写使用是否正确。 我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA"。 单词中所有字母都不是大写,比如"leetcode"。 如果单词不只含有一个字母,只有首字母大写, 比如 “Google”。 否则,我们定义这个单词没有正确使用大写字母。 示例 1: 输入: “USA” 输出: True 示例 2: 输入: “FlaG” 输出: False 注意: 输入是由大写和小写拉丁字母组成的非空单词。 public boolean detectCapitalUse ( String word ) { if ( word == null || word == "" ) return false ; int count = 0 ; for ( int i = 0 ; i < word . length ( ) ; i ++ ) { if ( word . charAt ( i ) <= 'Z' && word . charAt ( i ) >= 'A' ) count ++ ; } //三种情况 ,大写字母在首位、全是大写字母、无大写字母,return true; if ( count == word . length ( ) || count == 1 && word . charAt ( 0 ) >= 'A' &&

LeetCode Top Interview Questions 387. First Unique Character in a String (Java版; Easy)

此生再无相见时 提交于 2020-01-31 22:16:52
welcome to my blog LeetCode Top Interview Questions 387. First Unique Character in a String (Java版; Easy) 题目描述 Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase letters. 第一次做;bit-map记录每个字符出现的次数, 位置信息在原始字符串中; 下面的那个做法在处理位置信息时愚蠢了… class Solution { public int firstUniqChar ( String s ) { if ( s == null || s . length ( ) == 0 ) return - 1 ; // int [ ] arr = new int [ 26 ] ; for ( int i = 0 ; i < s . length ( ) ; i +