LeetCode题集
文章平均质量分 55
迷知凡
喜欢在求知过程中遇到的惊喜
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Binary Tree Paths(二叉树的路径)
Given a binary tree, return all root-to-leaf paths.(给定一棵二叉树,返回所有根节点到叶子节点的路径)For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are: [“1->2->5”, “1->3”]1.个人分析原创 2016-12-01 13:00:52 · 566 阅读 · 0 评论 -
Binary Tree Level Order Traversal II(层次遍历二叉树II)
Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).(给定一个二叉树,返回其自底向上层次遍历序列的节点值)For example: Given binary tr原创 2016-11-08 11:10:43 · 522 阅读 · 0 评论 -
Symmetric Tree(对称树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).(给定一棵二叉树,判断其是否对称)For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3原创 2016-11-09 16:44:44 · 483 阅读 · 0 评论 -
Power of Three
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?(能否不使用循环或递归实现?)1.个人分析 思路:最简单直观的做法是不断对3进行整除,如果原创 2016-10-07 12:26:27 · 424 阅读 · 0 评论 -
Power of Two
Given an integer, write a function to determine if it is a power of two.(给定一个整数,判断其是否是2的幂)1.个人分析 与判断是否是3的幂类似,普通解法就是不断地对输入的整数进行整除2,如果是2的幂则结果一定为1。2.个人解法bool isPowerOfTwo(int n) { if(n<1) re原创 2016-10-07 12:31:20 · 420 阅读 · 0 评论 -
Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.(给定一个32位整型数,判断其是否是4的幂)1.个人分析 判断一个整型数是否是4的幂,必定是从2的幂中寻找符合条件的。1,2,4,8,16,32,64都是2的幂,其中1,4,16,64为4的幂,观察这些数的二进制位发现,它们原创 2016-10-07 12:35:15 · 433 阅读 · 0 评论 -
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.(合并两个有序链表,并返回该新链表。新链表应该由最初的俩链表拼接而成)1.个人分析 与合并两个有序数组类似,只不过在这原创 2016-10-20 12:09:28 · 817 阅读 · 0 评论 -
Lowest Common Ancestor of a Binary Search Tree(二叉树最小公共祖先)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.(给定一个二分查找树,找到给定两节点的最小公共祖先)According to the definition of LCA on Wikipedia: “The lowest common ances原创 2016-10-17 12:10:06 · 379 阅读 · 0 评论 -
House Robber(窃贼的计划)
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses原创 2016-11-10 21:08:11 · 625 阅读 · 0 评论 -
Balanced Binary Tree(平衡二叉树)
Given a binary tree, determine if it is height-balanced.(给定一棵二叉树,判断该树的高度是否平衡)For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every原创 2016-11-29 18:51:27 · 812 阅读 · 0 评论 -
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 array.(给定一个非负整数num,对每个在0<=i<=num范围原创 2016-11-30 10:54:52 · 1425 阅读 · 0 评论 -
Battleships in a Board(统计战舰数)
Given an 2D board, count how many different 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原创 2016-11-07 16:39:39 · 336 阅读 · 0 评论 -
Two Sum(两数之和)
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 would have exactly one solution原创 2016-12-02 18:12:49 · 1848 阅读 · 1 评论 -
Add Two Numbers(基于链表的两数相加)
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 two numbers and return it as a linke原创 2016-12-03 14:26:15 · 799 阅读 · 0 评论 -
ZigZag Conversion(“Z”形转换)
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (字符串”PAYPALISHIRING”以给定的行数写成如下Z形模式) (you may want to display this pattern in a fixed font for better l原创 2016-12-12 16:53:25 · 2163 阅读 · 0 评论 -
Reverse Integer(反转整型数)
Reverse digits of an integer.(反转一个整型数)Example1: x = 123, return 321 Example2: x = -123, return -3211.个人分析 思路一:整型数转字符串->反转字符串->字符串转整型 思路二:数学解法,不断地进行整除和取余运算。2.个人解法 (1)int reverse(int x) { int si原创 2016-12-12 16:58:18 · 1990 阅读 · 0 评论 -
Longest Substring Without Repeating Characters(无重复最长子串)
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”, the原创 2016-12-05 15:30:26 · 637 阅读 · 0 评论 -
Number of Boomerangs(统计"boomerang"的数目)
Given n points in the plane that are all pairwise distinct, a “boomerang” is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tup原创 2016-11-28 14:50:48 · 492 阅读 · 0 评论 -
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 lin原创 2016-12-22 10:33:03 · 689 阅读 · 0 评论 -
Palindrome Number(回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(判断一个整型数是否是回文,要求不能借用额外的空间)1.个人分析 题目中的额外空间应该是指空间复杂度为O(1),所有的负数不属于回文数。2.个人解法bool isPalindrome(int x) { if(x > INT_MAX || x原创 2016-12-18 20:49:42 · 959 阅读 · 0 评论 -
3Sum(三数和)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.(给定含有n个元素的整型数组S,从数组S中找出所有的唯一三元组使得a+b+c=0成立)Not原创 2017-01-06 14:51:46 · 561 阅读 · 0 评论 -
Longest Common Prefix(最长相同前缀)
Write a function to find the longest common prefix string amongst an array of strings.(找出字符串数组中的最长相同前缀字符串)1.个人分析 思路一:采用BF法。首先取第一个字符串作为比较字符串,然后将该字符串的每个字符与其他字符串逐个进行比较,将所有相同的字符保存就是最终结果。2.个人解法string longe原创 2017-01-06 14:58:13 · 1786 阅读 · 0 评论 -
Contains Duplicate
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 element is原创 2016-09-22 11:09:54 · 1144 阅读 · 0 评论 -
Excel Sheet Column Number(Excel表列的数字)
Given a column title as appear in an Excel sheet, return its corresponding column number.(给定一个Excel表的列名,返回相应列的数字) For example: A -> 1 B -> 2 C -> 3 … Z -> 26 AA -> 27原创 2016-09-23 13:46:47 · 895 阅读 · 0 评论 -
Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constru原创 2016-09-23 13:57:25 · 518 阅读 · 0 评论 -
Same Tree(等价二叉树)
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.(给定两二叉树,判断它们是否相同。当两原创 2016-09-24 11:18:57 · 561 阅读 · 0 评论 -
Delete Node in a Linked List(删除链表中的节点)
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.(实现删除一个除尾节点外的链表节点的函数,假定只能访问被删节点)Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are give原创 2016-09-24 11:40:23 · 1500 阅读 · 0 评论 -
Move Zeroes(移动零元素)
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.(给定一个数组,将数组中值为0的元素移动到所有非0元素的后面,而且非0元素之间保持原始的相对位置)For example, given原创 2016-09-24 11:49:56 · 766 阅读 · 0 评论 -
Maximum Depth of Binary Tree(二叉树的最大深度)
Given a binary tree, find its maximum depth.(找出给定二叉树的最大深度) The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.1.个人分析 可以使用深度优先查找来得到二叉树的高度原创 2016-09-25 11:25:36 · 578 阅读 · 0 评论 -
Invert Binary Tree(反转二叉树)
Invert a binary tree.(反转一棵二叉树) 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 11.个人分析: 这道题其实在《剑指Offer》中看到过,只不过书中说的是镜像二叉树而已,但它们的本质是一样的,只要通过对根节点的左右孩子节点两两原创 2016-09-25 11:19:49 · 543 阅读 · 0 评论 -
Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.(给定一个非负整数num,重复加上它的所有数字直到结果只有一位数。)For example: Given num = 38, the process is like: 3 + 8 = 11, 1 +原创 2016-09-25 12:27:44 · 511 阅读 · 0 评论 -
Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.(给定一个大小为n的数组,找出majority element,majority element是指在数组中出现超过n/2次的元素) You may原创 2016-09-22 11:34:36 · 431 阅读 · 0 评论 -
First Unique Character in a String(字符串中的第一个唯一字符)
Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.(给定一个字符串,找出第一个非重复的字符并返回下标,如果不存在则返回-1)Examples: s = “leetcode” return 0. s = “lovele原创 2016-09-22 11:18:17 · 3041 阅读 · 2 评论 -
Valid Anagram(有效变位词)
Given two strings s and t, write a function to determine if t is an anagram of s.(给定两个字符串s和t,判断字符串t是否是s的变位词)For example, s = “anagram”, t = “nagaram”, return true. s = “rat”, t = “car”, return false.原创 2016-09-23 13:32:54 · 910 阅读 · 0 评论 -
Intersection of Two Arrays II(两数组的交叉元素II)
Given two arrays, write a function to compute their intersection.(给定两个数组,找出它们的交叉元素) Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Note: Each element in the result should appe原创 2016-09-20 15:23:48 · 407 阅读 · 0 评论 -
Roman to Integer(罗马数字转换为整数)
Given a roman numeral, convert it to an integer.(给定一个罗马数字,将其转换成整数) Input is guaranteed to be within the range from 1 to 3999.(输入范围在1-3999内)1.分析 这道题本质上应该与将字符串转化为整型一样,可以将atoi中实现的方法应用到这里。思路: 罗马数字的七个基本字原创 2016-09-20 14:49:42 · 757 阅读 · 0 评论 -
Reverse Linked List(反转链表)
Reverse a singly linked list.(反转一个单链表)Hint: A linked list can be reversed either iteratively or recursively. Could you implement both?(反转链表可以通过迭代或递归实现)1.个人解法(迭代)ListNode* reverseList(ListNode* head)原创 2016-09-20 14:27:12 · 871 阅读 · 0 评论 -
Intersection of Two Arrays(两数组的交叉元素)
Given two arrays, write a function to compute their intersection.(给定两个数组,计算它们的交叉元素)Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].Note: ● Each element in the result must be unique.原创 2016-09-26 11:40:06 · 798 阅读 · 0 评论 -
Happy Number
Write an algorithm to determine if a number is “happy”.(判断一个数字是否是”happy”)A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum o原创 2016-10-12 11:12:44 · 357 阅读 · 0 评论 -
Number of 1 Bits(二进制中1的个数)
Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight).(给定一个无符号整型数,实现一个函数返回该整数的二进制中1的个数)For example, the 32-bit integer ’11’ has bi原创 2016-10-12 11:25:00 · 656 阅读 · 0 评论
分享