LeetCode
文章平均质量分 52
辞穷
如果你真心想做某事,全世界都会帮你完成。
展开
-
LeetCode1104. 二叉树寻路 接近双百 还算简单和详细的题解
https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/solution/1104-er-cha-shu-xun-lu-jie-jin-shuang-ba-lmrd/解题思路首先确定层数int lg = log2(label); 获得当前label的子孩子(3 * pow(2, lg) - 1 - label)/ 2 即该层的镜像(距离该层两边界相等对称面)值/2:就是当前层的镜像/2:当前层的镜像3 * pow(2原创 2021-07-29 13:11:24 · 123 阅读 · 0 评论 -
LeetCode001两数之和
题目链接:https://leetcode-cn.com/problems/two-sum/看到题目第一想法是map,然后发现并不快,有另一种更简单的办法,在了解途中还学了一些其他的。1、用unordered_map代替map,以散列代替map内部的红黑树实现,使其可以用来处理只映射而不按key排序的需求,速度比map快很多。—《算法笔记》2、return {i, j} 是vector的列表...原创 2020-02-22 21:44:34 · 202 阅读 · 0 评论 -
171. Excel Sheet Column Number(C++)
题目: Given a column title as appear in an Excel sheet, return its corresponding column number.For example:A -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 28 解答:#include<string>//相当于是读一个26进制数,获得字符长度原创 2017-03-17 09:00:41 · 268 阅读 · 0 评论 -
409. Longest Palindrome (C++)
题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example “Aa” is no原创 2017-03-23 21:04:40 · 374 阅读 · 0 评论 -
396. Rotate Function (C++)
题目:Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a “rotation function” F on A as follow: F(k)原创 2017-03-14 13:08:10 · 308 阅读 · 0 评论 -
387. First Unique Character in a String (C++)
题目: 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原创 2017-03-22 19:32:22 · 331 阅读 · 0 评论 -
268. Missing Number (C++)
题目: Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array. 0到n中有一个数在数组中没有了,找到这个数。 For example, Given nums = [0, 1, 3] return 2.原创 2017-03-22 17:59:10 · 402 阅读 · 0 评论 -
541. Reverse String II (C++)
题目: Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of原创 2017-03-13 13:19:01 · 530 阅读 · 0 评论 -
238. Product of Array Except Self (C++)
题目: Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in原创 2017-03-22 11:19:51 · 256 阅读 · 0 评论 -
169. 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 eleme原创 2017-03-22 09:56:06 · 340 阅读 · 0 评论 -
504. Base 7(C++)
题目: Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: “202”Example 2: Input: -7 Output: “-10”Note: The input will be in range of [-1e7, 1e7].原创 2017-03-22 04:08:19 · 339 阅读 · 0 评论 -
455. Assign Cookies(C++)
题目: Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of原创 2017-03-10 20:42:40 · 222 阅读 · 0 评论 -
242. Valid Anagram(C++)
题目: Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = “anagram”, t = “nagaram”, return true. s = “rat”, t = “car”, return false. Note:原创 2017-03-22 19:01:36 · 355 阅读 · 0 评论 -
204. Count Primes(C++)
题目:Description: Count the number of prime numbers less than a non-negative number, n. 找出小于n的所有素数的个数解答:先将小于n的所有数都标记为true,从0开始,对接下来见到的每一个素数(true),使所有有这个素数因子的数都标记为false(本身不标记) 最后查找数组中为true的个数就是答案,这样可以减原创 2017-05-09 14:25:25 · 448 阅读 · 0 评论 -
263. Ugly Number(C++)
题目:Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since原创 2017-05-09 13:06:43 · 363 阅读 · 0 评论 -
202. Happy Number(C++)
题目:Write an algorithm to determine if a number is “happy”. A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares原创 2017-05-09 12:30:29 · 547 阅读 · 0 评论 -
217. Contains Duplicate Add to List(C++)
题目:Given an array of integers(整数), find if the array contains any duplicates(重复). Your function should return true if any value appears at least twice in the array, and it should return false if every原创 2017-05-09 09:56:21 · 263 阅读 · 0 评论 -
191. Number of 1 Bits(C++)
题目:Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11’ has binary representation 00000000原创 2017-05-11 19:39:05 · 411 阅读 · 0 评论 -
231. Power of Two(C++)
题目:Given an integer, write a function to determine if it is a power of two. 这道题和LeetCode上326题只是换了数字的差别,翻译和具体解析在这里都有介绍 http://blog.csdn.net/ZZLCSDN2017/article/details/71591345解答:首先计算2^30=1073741824,原创 2017-05-10 21:01:45 · 428 阅读 · 0 评论 -
326. Power of Three(C++)
题目:Given an integer, write a function to determine if it is a power of three. 给一个整数,判断它是不是3的幂数 Follow up: Could you do it without using any loop / recursion? 不用循环或递归写出这个方法解答:最简单的: 用循环/注意判断0特殊值cl原创 2017-05-10 20:48:07 · 463 阅读 · 0 评论 -
167. Two Sum II - Input array is sorted (C++)
题目 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 nu原创 2017-02-28 00:19:42 · 391 阅读 · 0 评论 -
283. Move Zeroes (C++)
题目: Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after calling原创 2017-03-11 19:09:06 · 287 阅读 · 0 评论 -
442. Find All Duplicates in an Array (C++)
题目: Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it witho原创 2017-03-11 14:24:04 · 253 阅读 · 0 评论 -
520. Detect Capital(C++)
题目: Given a word, you need to judge whether the usage of capitals in it is right or not. We define the usage of capitals in a word to be right when one of the following cases holds: All原创 2017-03-05 21:22:37 · 398 阅读 · 0 评论 -
344. Reverse String(C++)
题目: Write a function that takes a string as input and returns the string reversed. 造一个方法让输入的的字符串反转(这个不用谷歌我也可以翻译hh) Example: Given s = “hello”, return “olleh”.直接贴代码:class Solution {public:原创 2017-03-05 20:24:39 · 404 阅读 · 0 评论 -
500. Keyboard Row
题目 Given a List of words, return the words that can be typed using letters of alphabet on only one row’s of American keyboard like the image below.American keyboard Example 1: Input: [“Hello”,原创 2017-02-25 19:34:01 · 292 阅读 · 0 评论 -
448. Find All Numbers Disappeared in an Array(C++)
题目: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this arr原创 2017-03-08 10:22:02 · 251 阅读 · 0 评论 -
419. Battleships in a Board
题目 Given an 2D board, count how many battleships are in it. The battleships are represented with ‘X’s, empty slots are represented with ‘.’s. You may assume the following rules: You receive a val原创 2017-02-23 23:43:21 · 353 阅读 · 0 评论 -
136. Single Number(C++)
Given an array of integers, every element appears twice except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra原创 2017-03-02 18:51:28 · 401 阅读 · 0 评论 -
412. Fizz Buzz(C++)
LeetCode:412. Fizz Buzz(C++)原创 2017-02-26 14:23:20 · 654 阅读 · 0 评论 -
485. Max Consecutive Ones(C++)
Given a binary array, find the maximum number of consecutive 1s in this array.Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecuti原创 2017-03-02 17:04:55 · 293 阅读 · 0 评论 -
476. Number Complement (C++)
题目 Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the rang原创 2017-02-23 00:46:56 · 391 阅读 · 0 评论 -
389. Find the Difference
题目 Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at a random position. Find the letter t原创 2017-03-06 13:09:56 · 278 阅读 · 0 评论 -
371. Sum of Two Integers(C++)
题目:不用加减乘除法实现加法运算如果不能用加减乘除运算,那还能有什么运算方法呢?当然了,还有亲切的位运算。解题思路: 和平时做加减法一样,先各个位相加,然后得到的数再相加,注意:期间要考虑进位情况(有时候多次进位,比如:9999+1=10000)二进制的两个数相比较无非三种情况: 1———–1———–0 0———–1———–0 相加后结果: 1———–0———–0 不进位|||进位1原创 2017-03-05 16:40:22 · 565 阅读 · 1 评论 -
495. Teemo Attacking (C++)
题目: In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo’s attacking ascending time series towards Ashe and the poisonin原创 2017-03-11 13:41:02 · 403 阅读 · 0 评论 -
492. Construct the Rectangle (C++)
题目:长但是简单 For a web developer, it is very important to know how to design a web page’s size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whos原创 2017-03-11 12:50:09 · 312 阅读 · 0 评论 -
413. Arithmetic Slices(C++)
题目: https://leetcode.com/problems/arithmetic-slices/?tab=Description翻译: 如果数字序列由至少三个元素组成,并且任何两个连续元素之间的差相同,则该数字序列被称为算术。 例如,这些是算术序列: 1,3,5,7,9 7,7,7,7 3,-1,-5,-9 以下序列不是算术。 1,1,2,5原创 2017-03-10 20:34:10 · 286 阅读 · 0 评论 -
463. Island Perimeter (C++)
题目: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is comple原创 2017-03-07 14:05:22 · 393 阅读 · 0 评论 -
258. Add Digits (C++)
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has原创 2017-03-06 21:21:58 · 329 阅读 · 0 评论 -
496. Next Greater Element I(C++)
题目 You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1’s elements in the corresponding places of nums原创 2017-02-28 12:12:57 · 442 阅读 · 0 评论