
LeetCode
Donald Su
公众号:"Donald Su",微:sug2010
展开
-
[LeetCode-83]-Remove Duplicates from Sorted List(删除有序list中的重复元素)
文章目录0. 题目相关1. Solution2. 代码迭代过程0. 题目相关【题目解读】给定一个有序list,从中删除重复的元素,使得每一个元素仅出现一次。【原题描述】原题链接Given a sorted linked list, delete all duplicates such that each element appear only once.Example 1:Input...原创 2019-10-06 22:54:20 · 250 阅读 · 0 评论 -
剑指offer-二进制中1的个数
除法运算效率较低,所以在编程中,尽可能地用移位来代替除法。这个题目中的class Solution {public: int NumberOf1_dead(int num) { if(num == 0) return 0; int cnt = 0; while(num >= 1) { //if(num % 2 != 0) 比较低效,用位运算 .原创 2020-09-22 22:23:15 · 658 阅读 · 0 评论 -
不用sqrt()函数,求平方根的三种方法
最近看到了这个比较有意思的题目,探究了一下。文章目录1、二分法2、牛顿法3、来自于Quake III源码的解法4、完整代码参考当然有最暴力的方法,直接遍历,(0.0, x)区间内所有的数据(也可以是 x/2),看值是否相等,但该方法太过暴力,在此不讨论。可以采用下面的 二分法、牛顿法,Quake III源码中的方法。浮点数的比较方法比较特别,不能使用平常的 x == 0的方法进行比较,下面都用到了equal比较方法,equal的代码:const double Min_value = 1e-7;.原创 2020-05-25 20:22:29 · 5639 阅读 · 0 评论 -
剑指offer-4-替换空格
问题请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。方案该问题如果采用暴力方法:从前往后遍历,如果遇到空格,开始整体数据向后移动2位,插入%20,一直到结束。采用这样的方法,时间复杂度为O(n2)O(n^2)O(n2),比较耗时间,也不是理想的方案。上面的方案,可以看出有很大的改进...原创 2020-05-04 00:08:00 · 250 阅读 · 0 评论 -
[LeetCode-202]-Happy Number-LeetCode 30天挑战赛-2
文章目录题目相关Solution题目相关【题目解读】给定一个正数,判断该数是否是快乐数。快乐数定义:将该数进行拆分,拆分后的各个数值的平方求和,求和的结果进行如下判断该数是否为1?或者该数包含在一个循环中无休止地循环如果数值是1就是快乐数,否则不是ps:上面是快乐数,在遇到<10的数时,自己以为就结束了,未进行计算,实际上还需要继续进行计算。以3为例:32=9...原创 2020-04-04 00:23:26 · 334 阅读 · 0 评论 -
[LeetCode-1342]Number of Steps to Reduce a Number to Zero
该题是easy,比较简单,我用的是常规的方法,看到了一行实现的大神。。。文章目录题目相关Solution题目相关【题目解读】给定一个数(0~10610^6106),判断该数经过减1 或 除2操作到达0所需要的步数。【原题描述】原题链接Given a non-negative integer num, return the number of steps to reduce it t...原创 2020-04-03 23:59:20 · 680 阅读 · 0 评论 -
[LeetCode-136]-Single Number-LeetCode 30天挑战赛-1
该题是LeetCode 4月组织的30天挑战赛,是第一题。该题是easy,比较简单,但效率比较高有难度,方案还有待继续提高。文章目录题目相关Solution使用map题目相关【题目解读】从一个给定的非空数组中找出只出现一次的元素。【原题描述】原题链接Given a non-empty array of integers, every element appears twice e...原创 2020-04-02 02:00:16 · 409 阅读 · 0 评论 -
【待解决】[LeetCode-101]-Symmetric Tree(判断两颗二叉树是否对称)
文章目录0. 题目相关1. Solution0. 题目相关【题目解读】给定两颗二叉树,对这两颗二叉树进行比较,判断这两棵二叉树是否对称【原题描述】原题链接Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binar...原创 2019-11-15 19:49:38 · 277 阅读 · 0 评论 -
[LeetCode-100]-Same Tree(判断两颗二叉树是否相同)
文章目录0. 题目相关1. Solution0. 题目相关【题目解读】给定两颗二叉树,对这两颗二叉树进行比较,查看这两棵二叉树是否相同【原题描述】原题链接Given two binary trees, write a function to check if they are the same or not.Two binary trees are considered the sam...原创 2019-10-21 17:51:01 · 310 阅读 · 0 评论 -
[LeetCode-88]-Merge Sorted Array(有序数组合并)
文章目录0. 题目相关1. Solution2. 其他改进方案0. 题目相关【题目解读】给定两个有序数组,对数组进行合并操作。要求合并后的数组依旧有序。【原题描述】原题链接Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:The number...原创 2019-10-13 22:16:00 · 265 阅读 · 0 评论 -
[LeetCode-70]-Climbing Stairs(爬楼梯,斐波那契数列问题)
文章目录题目相关Solution题目相关【题目解读】该题就是斐波那契数列问题,可以使用递归方法实现。【原题描述】原题链接You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ...原创 2019-09-29 20:56:49 · 237 阅读 · 0 评论 -
[LeetCode-69]-Sqrt(根号运算)
文章目录题目相关Solution题目相关【题目解读】【原题描述】原题链接Implement int sqrt(int x).Compute and return the square root of x, where x is guaranteed to be a non-negative integer.Since the return type is an integer, the...原创 2019-09-15 18:46:08 · 296 阅读 · 0 评论 -
[LeetCode-67]-Add Binary(二进制相加)
文章目录题目相关Solution题目相关【题目解读】【原题描述】原题链接Given two binary strings, return their sum (also a binary string).The input strings are both non-empty and contains only characters 1 or 0.Example 1:Input: a...原创 2019-09-01 16:57:27 · 238 阅读 · 0 评论 -
[LeetCode-66]- Plus One
文章目录题目相关Solution题目相关【题目解读】【题目】原题链接【难度】EasySolution原创 2019-08-14 22:33:40 · 233 阅读 · 0 评论 -
[LeetCode-58]- Length of Last Word(最后一个单词的长度)
文章目录题目相关Solution题目相关【题目解读】求最后一个单词的长度【题目】原题链接Given a string s consists of upper/lower-case alphabets and empty space characters ’ ', return the length of last word in the string.If the last word ...原创 2019-08-06 23:23:33 · 239 阅读 · 0 评论 -
[LeetCode-38]- Count and Say
文章目录题目相关Solution题目相关【题目解读】【题目】原题链接The count-and-say sequence is the sequence of integers with the first five terms as following:1112112111112211 is read off as “one 1” or 11.11 is read of...原创 2019-07-17 21:56:29 · 255 阅读 · 0 评论 -
[LeetCode-20]-valid parentheses (括号匹配)
文章目录题目相关Solution题目相关【题目解读】检查输入的括号字符串中的括号是否匹配【原题描述】原题链接Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.An input string is val...原创 2019-07-02 19:31:59 · 231 阅读 · 0 评论 -
[LeetCode-14]-Longest Common Prefix (最长公共前缀)
文章目录题目相关Solution题目相关【题目解读】查找一个字符串数组的公共前缀。【原题描述】原题链接Write a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string “”....原创 2019-07-02 19:38:36 · 236 阅读 · 0 评论 -
[LeetCode-13]-Roman to Integer (将罗马数字转换为整数)
文章目录题目相关SolutionLeetCode如何查看他人提交的代码题目相关【题目解读】将罗马数字转换为整数,其中「I = 1; V = 5; X = 10; L = 50; C = 100; D = 500; M = 1000」。IV、IX、XL、XC、CD、CM这几种情况需要做减法,要特殊处理。【原题描述】原题链接Roman numerals are represented by ...原创 2019-07-02 19:40:31 · 216 阅读 · 0 评论 -
[LeetCode-09]-Palindrome Number (判断是否为回文数字)
文章目录题目相关Solution转换为字符串进行判断通过整数翻转判断题目相关【题目】原题链接Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.Example 1:Input: 121Output: true...原创 2019-07-02 19:40:59 · 245 阅读 · 0 评论 -
[LeetCode-07]-整数翻转(Reverse Integer)
文章目录题目相关Solution总结题目相关【题目】原题链接Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example 2:Input: -123Output: -321Example 3:Input: 120Output: 21...原创 2019-07-02 19:41:31 · 320 阅读 · 0 评论 -
[LeetCode-03]-Longest Substring Without Repeating Characters
文章目录题目相关Solution每周完成一个ARTS:(Algorithm、Review、Tip、Share, ARTS)Algorithm: 每周至少做一个 leetcode 的算法题Review: 阅读并点评至少一篇英文技术文章Tip: 学习至少一个技术技巧Share: 分享一篇有观点和思考的技术文章题目相关【题目】原题链接Given a string, find th...原创 2019-07-02 19:42:49 · 202 阅读 · 0 评论 -
[LeetCode-02]-Add Two Numbers-性能极好
文章目录题目相关Solution1. 错误的解法2. 正确解法3. 几个用例后记每周完成一个ARTS:(Algorithm、Review、Tip、Share, ARTS)Algorithm: 每周至少做一个 leetcode 的算法题Review: 阅读并点评至少一篇英文技术文章Tip: 学习至少一个技术技巧Share: 分享一篇有观点和思考的技术文章题目相关【题目】原题链接...原创 2019-07-02 19:44:57 · 286 阅读 · 0 评论 -
[LeetCode-01]-Two Sum(求和)
文章目录题目相关Solution1. 暴力求解2. 暴力求解方法改进3. 不可行的方案后记每周完成一个ARTS:(Algorithm、Review、Tip、Share, ARTS)Algorithm: 每周至少做一个 leetcode 的算法题Review: 阅读并点评至少一篇英文技术文章Tip: 学习至少一个技术技巧Share: 分享一篇有观点和思考的技术文章题目相关【题目...原创 2019-07-02 19:47:31 · 253 阅读 · 0 评论 -
[LeetCode-35]-Search Insert Position(搜索整数所在的位置)
文章目录题目相关Solution1. 顺序遍历2. 算法改进 -- 二分查找题目相关【题目解读】从有序整数列表中搜索给定整数,如果在其中返回下标位置,如果不在,返回应该在的位置。【题目】原题链接Given a sorted array and a target value, return the index if the target is found. If not, return t...原创 2019-07-08 20:34:58 · 305 阅读 · 0 评论 -
[LeetCode-28]-Implement strStr(实现strStr函数)
文章目录题目相关Solution题目相关【题目解读】实现 strStr(string haystack, string needle) 函数,返回 needle在 haystack中第一次出现的位置,如果没有出现返回-1.【原题描述】原题链接Implement strStr().Return the index of the first occurrence of needle in ...原创 2019-06-30 08:40:35 · 261 阅读 · 0 评论 -
[LeetCode-26]-Remove Duplicates from Sorted Array(从有序数组中删除重复元素)
文章目录题目相关Solution题目相关【题目解读】从有序数组中删除重复元素,并返回数组的长度。【原题描述】原题链接Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.Do not alloc...原创 2019-06-30 08:50:58 · 217 阅读 · 0 评论 -
[LeetCode-21]-Merge Two Sorted Lists(有序列表合并)
文章目录题目相关Solution不带头结点增加头结点使用递归题目相关【题目解读】合并两个有序列表,并返回新列表。【原题描述】原题链接Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the firs...原创 2019-06-30 08:58:18 · 249 阅读 · 0 评论 -
[LeetCode-53]- Maximum Subarray(子数组最大值)
文章目录题目相关Solution题目相关【题目解读】获取数组中最大子串,并返回该最大值【题目】原题链接Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.Example...原创 2019-07-28 22:46:08 · 315 阅读 · 0 评论 -
[LeetCode-27]-Remove Element (删除指定元素)
文章目录题目相关Solution题目相关【题目解读】使用In-place(原地算法),从数组中删除指定值的所有元素,并返回删除后的数组长度。【原题描述】原题链接Given an array nums and a value val, remove all instances of that value in-place and return the new length.Do not ...原创 2019-06-03 19:31:43 · 230 阅读 · 0 评论