
leetCode 试题分析与解答
针对 leetCode 上的题目,给出每道题的编程思路与具体的代码实现。
liyuanbhu
这个作者很懒,什么都没留下…
展开
-
Leetcode 第 794 题(Valid Tic-Tac-Toe State)
Leetcode 第 794 题(Valid Tic-Tac-Toe State) A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to reach this board position during the course of a valid ...原创 2018-03-17 19:23:21 · 1229 阅读 · 0 评论 -
LeetCode 第 73 题 (Set Matrix Zeroes)
LeetCode 第 73 题 (Set Matrix Zeroes) Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up: Did you use extra space? A straight forward sol原创 2016-07-13 19:29:32 · 2661 阅读 · 0 评论 -
LeetCode 第 367 题 (Valid Perfect Square)
LeetCode 第 367 题 (Valid Perfect Square) Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function su原创 2016-07-12 20:29:35 · 2547 阅读 · 0 评论 -
LeetCode 第 372 题 (Super Pow)
LeetCode 第 72 题 (Super Pow) Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array. Example1: a = 2原创 2016-07-12 21:02:17 · 3151 阅读 · 0 评论 -
LeetCode 第 19 题 (Remove Nth Node From End of List)
LeetCode 第 19 题 (Remove Nth Node From End of List) Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2.原创 2016-07-07 22:33:50 · 2225 阅读 · 0 评论 -
LeetCode 第 292 题 (Nim Game)
LeetCode 第 292 题 (Nim Game) You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who remov原创 2016-07-05 13:58:13 · 1710 阅读 · 0 评论 -
LeetCode 第 67 题 (Add Binary)
LeetCode 第 67 题 (Add Binary) Given two binary strings, return their sum (also a binary string). For example, a = “11” b = “1” Return “100”.两个字符串,计算加法。这道题主要是考察对字符串操作的掌握情况。另外,加法要从低位算起,但是原创 2016-07-03 13:21:30 · 3079 阅读 · 0 评论 -
LeetCode 第 204 题 (Count Primes)
LeetCode 第 204 题 (Count Primes) Description: Count the number of prime numbers less than a non-negative number, n.计算小于 N 的素数的个数。这道题目比较简单。但是想提高计算效率与需要费点脑筋。判断一个数字 nn 是不是素数的简单方法是 用 nn 去除 2,3,4,…,n−1原创 2016-07-01 12:37:52 · 2129 阅读 · 0 评论 -
LeetCode 第 371 题 (Sum of Two Integers)
LeetCode 第 371 题 (Sum of Two Integers) Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example: Given a = 1 and b = 2, return 3.不用加减法计算两个整数的和。这道原创 2016-07-01 19:56:30 · 5884 阅读 · 0 评论 -
LeetCode 第 263 题 (Ugly Number)
LeetCode 第 263 题 (Ugly Number) 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 ar原创 2016-07-02 09:08:41 · 2668 阅读 · 0 评论 -
LeetCode 第 155 题 (Min Stack)
LeetCode 第 155 题 (Min Stack) Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the element o原创 2016-05-03 14:47:18 · 3442 阅读 · 0 评论 -
LeetCode 第 344 题(Reverse String)
LeetCode 第 344 题(Reverse String) Write a function that takes a string as input and returns the string reversed. Example: Given s = “hello”, return “olleh”. 这道题非常简单。用 C++ 来写的话主要是考察对 string 类型原创 2016-05-01 19:29:06 · 3089 阅读 · 0 评论 -
LeetCode 第 345 题(Reverse Vowels of a String)
LeetCode 第 345 题(Reverse Vowels of a String) Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = “hello”, return “holle”. Example 2:原创 2016-05-01 19:28:13 · 2988 阅读 · 0 评论 -
LeetCode 第 14 题(Longest Common Prefix)
LeetCode 第 14 题(Longest Common Prefix) Write a function to find the longest common prefix string amongst an array of strings. 这道题比较简单,主要是要考虑一些特殊情况,比如这个 vector 为空如何处理。下面是我的代码。class Solution {public:原创 2016-05-01 19:26:45 · 1840 阅读 · 0 评论 -
LeetCode 第 21 题 (Merge Two Sorted Lists)
LeetCode 第 21 题 (Merge Two Sorted Lists) 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 first two lists.合并两个排好序的链表,要求合并之原创 2016-05-01 19:24:32 · 1757 阅读 · 0 评论 -
LeetCode 第 66 题 (Plus One)
LeetCode 第 66 题 (Plus One) Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the l原创 2016-05-01 19:21:22 · 1757 阅读 · 0 评论 -
LeetCode 第 65 题(Valid Number)
LeetCode 第 65 题(Valid Number) Validate if a given string is numeric. Some examples: “0” => true ” 0.1 ” => true “abc” => false “1 a” => false “2e10” => true Note: It is inten原创 2016-05-01 19:18:00 · 1683 阅读 · 0 评论 -
LeetCode 第 69 题 (Sqrt(x))
LeetCode 第 69 题 (Sqrt(x)) Implement int sqrt(int x). Compute and return the square root of x.求 x 的平方根。我们知道 x√\sqrt{x} 是单调增函数, 1≤x√≤x1 \leq \sqrt{x} \leq x。所以可以用二分查找法来计算。int mySqrt(int x){ if原创 2016-04-23 18:02:51 · 2897 阅读 · 0 评论 -
LeetCode 第 50 题 (Pow(x, n))
LeetCode 第 50 题 (Pow(x, n)) Implement pow(x, n).这个题目非常简短,求 xnx^n。其中nn 为整数。 最简单的想法就是用一个循环,将 xx 自乘nn 次。按照这个思路有下面的代码。class Solution {public: double myPow(double x, int n) { int sign原创 2016-04-23 17:42:59 · 3014 阅读 · 0 评论 -
LeetCode 第 343 题 (Integer Break)
LeetCode 第 343 题 (Integer Break) Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can ge原创 2016-04-20 09:48:03 · 7724 阅读 · 8 评论 -
LeetCode 第 125 题 (Valid Palindrome)
LeetCode 第 125 题 (Valid Palindrome) Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, “A man, a plan, a canal: Panama”原创 2016-04-19 12:48:55 · 1327 阅读 · 0 评论 -
LeetCode 第 326 题 (Power of Three)
LeetCode 第 326 题 (Power of Three) Given an integer, write a function to determine if it is a power of three. Follow up: Could you do it without using any loop / recursion?这道题的常规的思路是不停的除以 3,如果能原创 2016-04-19 09:41:15 · 1247 阅读 · 0 评论 -
LeetCode 第 338 题 (Counting Bits)
LeetCode 第 338 题 (Counting Bits) Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an原创 2016-04-19 09:30:21 · 2162 阅读 · 0 评论 -
LeetCode 第 37 题 (Sudoku Solver)
LeetCode 第 37 题 (Sudoku Solver) Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character ‘.’. You may assume that there will be only on...原创 2016-04-19 09:24:47 · 1825 阅读 · 0 评论 -
LeetCode 第 190 题 (Reverse Bits)
LeetCode 第 190 题 (Reverse Bits) Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192原创 2016-04-18 21:31:15 · 2924 阅读 · 0 评论 -
LeetCode 第 191 题 (Number of 1 Bits)
LeetCode 第 191 题 (Number of 1 Bits) 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原创 2016-04-18 20:46:54 · 1921 阅读 · 0 评论 -
LeetCode 第 231 题 (Power of Two)
LeetCode 第 231 题 (Power of Two) Given an integer, write a function to determine if it is a power of two.这个题目有个特别简单的解法。当然,能够独自想出这个方法可不简单。这种方法可以作为一个知识点记住就好了。class Solution {public: bool isPowerOf原创 2016-04-18 20:39:07 · 1979 阅读 · 0 评论 -
LeetCode 第 342 题(Power of Four)
LeetCode 第 342 题 Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Co原创 2016-04-18 13:07:51 · 5592 阅读 · 0 评论 -
LeetCode 第 9 题(Palindrome Number)
LeetCode 第 9 题 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting t原创 2016-04-18 13:19:21 · 1153 阅读 · 0 评论 -
LeetCode 第 7 题(Reverse Integer)
LeetCode 第 7 题题目是这样的: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Here are some good questions to ask before coding. Bonus points for you原创 2016-04-17 19:20:36 · 2586 阅读 · 0 评论 -
LeetCode 第 3 题(Longest Substring Without Repeating Characters)
LeetCode 第三题 Given a string, find the length of the longest substring without repeating characters. Examples: Given “abcabcbb”, the answer is “abc”, which the length is 3. Given “bbbbb”,原创 2016-04-16 12:07:19 · 3322 阅读 · 0 评论 -
Leetcode 第 2 题(Add Two Numbers)
Leetcode 第二题详解题目如下: Question You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the原创 2016-04-14 14:43:57 · 2969 阅读 · 0 评论 -
Leetcode 第 1 题(Two Sum)
Leetcode 第一题昨天去逛了逛这个网站,感觉挺有意思。准备有空的时候就做一两道。第一道题目是这样的: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input wou原创 2016-04-03 13:58:58 · 5832 阅读 · 2 评论