![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leetcode
lyh_xd
一匹正在努力提高算法、编程水平的机器学习马
展开
-
leetcode 24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You may no原创 2016-04-12 10:25:36 · 286 阅读 · 0 评论 -
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.After removing the second node from the end, the linked list be原创 2016-09-01 10:57:29 · 211 阅读 · 0 评论 -
Leetcode 257. 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”]用深度优先搜索+递归处理/** * Definition f原创 2016-09-01 10:21:04 · 215 阅读 · 0 评论 -
Leetcode 234. Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time and O(1) space? 题目是求单链表是否是回文。根据在平台上测试结果知:12->34->12是回文,而不是说 12->343 ->21是回文,这个概念要搞清楚。解题思路很简单,用两个节原创 2016-09-01 00:20:57 · 271 阅读 · 0 评论 -
leetcode 202. Happy Number
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 of i原创 2016-04-08 23:08:40 · 323 阅读 · 0 评论 -
leetcode 70. Climbing Stairs
意思就是爬一个n个台阶的楼梯,每次只能爬1个或2个台阶,问有多少种方法。思路:爬n个台阶可有两种大的方案:爬 n-2 个台阶后再一次爬 2 个台阶;或者爬 n-1 个台阶后再爬 1 个台阶。所以得出规律:设方案个数为 S[n],则 S[n]=S[n-1]+S[n-2],S[1]=1,S[2]= , 这是一个斐波那契数列。原创 2016-04-01 17:26:38 · 325 阅读 · 0 评论 -
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 ’11’ has binary representation 0000000000000原创 2016-04-01 11:25:07 · 287 阅读 · 0 评论 -
leetcode 279 : Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n. For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, retu原创 2016-03-29 21:45:34 · 437 阅读 · 0 评论 -
Leetcode 230. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.Follow up: What if the BST is mod原创 2016-06-29 22:05:42 · 263 阅读 · 0 评论 -
Leetcode 328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in plac原创 2016-06-17 22:15:30 · 229 阅读 · 0 评论 -
Leetcode 62. Unique Paths
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the botto原创 2016-06-17 21:11:38 · 223 阅读 · 0 评论 -
Leetcode 337. House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “root.” Besides the root, each house has one and only one parent house. After a tour,原创 2016-06-17 10:28:59 · 233 阅读 · 0 评论 -
Leetcode 319. Bulb Switcher
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it’s off or turning off i原创 2016-06-16 22:41:06 · 212 阅读 · 0 评论 -
Leetcode 213. House Robber II
Note: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all原创 2016-06-17 11:23:18 · 196 阅读 · 0 评论 -
Leetcode 165. Compare Version Numbers
Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.You may assume that the version strings are non-empty and conta原创 2016-06-11 17:07:14 · 184 阅读 · 0 评论 -
Leetcode 278. First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the原创 2016-06-07 11:10:00 · 167 阅读 · 0 评论 -
Leetcode 38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, …1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 21 is read off as “one 2, the原创 2016-05-30 23:30:42 · 214 阅读 · 0 评论 -
Leetcode 58. Length of Last Word
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 does not exist, return 0.Note: A word is defined as原创 2016-09-01 11:39:10 · 226 阅读 · 0 评论 -
Leetcode 1. 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.Example:Given nums = [2, 7, 11, 1原创 2016-09-01 12:52:24 · 237 阅读 · 0 评论 -
Leetcode 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 that was added in原创 2016-09-02 11:24:55 · 212 阅读 · 0 评论 -
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-26 15:47:31 · 232 阅读 · 0 评论 -
Leetcode 206. Reverse Linked List
Reverse a singly linked list.原创 2016-05-26 09:23:33 · 203 阅读 · 0 评论 -
Leetcode 121. Best Time to Buy and Sell Stock
股票利益最大化问题原创 2016-05-25 17:32:38 · 214 阅读 · 0 评论 -
Leetcode 141. Linked List Cycle
Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space? 判断链表是否有环原创 2016-05-25 11:12:41 · 211 阅读 · 0 评论 -
Leetcode 237. 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 given the third node with value 3,原创 2016-05-24 20:23:10 · 193 阅读 · 0 评论 -
Leetcode Majority Element I&II
本篇讲述Leetcode里面两个关于找出出现次数最多元素的相似题目,通过多种解法,展现摩尔投票法的魅力。leetcode #169 Majority Element Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2原创 2016-03-27 16:09:29 · 570 阅读 · 0 评论 -
Leetcode 349. 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].原创 2016-05-24 15:21:43 · 260 阅读 · 0 评论 -
Leetcode 226. Invert Binary Tree
Invert a binary tree. 题目意思是将二叉树每层的左右孩子交换。原创 2016-05-24 14:30:42 · 230 阅读 · 0 评论 -
Leetcode 104. 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.原创 2016-05-24 09:04:59 · 216 阅读 · 0 评论 -
Leetcode 100. 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-05-23 21:33:49 · 201 阅读 · 0 评论 -
Leetcode 83. Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3.原创 2016-05-23 20:41:06 · 248 阅读 · 0 评论 -
leetcode 110. Balanced Binary Tree
判断一个二叉树是否是 height-balaced 的二叉树,height-balaced 二叉树的定义是:任一结点的左右子树高度差不超过1.原创 2016-04-13 00:13:24 · 253 阅读 · 0 评论 -
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?class Solution {public: bool isPowerOfThree(int n) { re原创 2016-04-09 18:14:57 · 310 阅读 · 0 评论 -
leetcode 198. 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-04-09 14:52:54 · 240 阅读 · 0 评论 -
Leetcode 7. Reverse Integer
Example1: x = 123, return 321 Example2: x = -123, return -321class Solution {public: int reverse(int x) { string s=to_string(x); for(int i=0,j=s.size()-1;i<j;++i,--j){原创 2016-09-02 12:33:04 · 217 阅读 · 0 评论 -
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.用二进制的异或、与运算代替加减class Solution {public: int getSum(int a, in原创 2016-09-02 12:10:34 · 220 阅读 · 0 评论 -
Leetcode 290. Word Pattern
Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.原创 2016-05-30 16:10:17 · 350 阅读 · 0 评论 -
Leetcode 160. Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.原创 2016-05-29 20:46:11 · 247 阅读 · 0 评论 -
Leetcode 111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node./** * Definition for a binary tree node.原创 2016-05-29 17:11:37 · 227 阅读 · 0 评论 -
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” is a palindrome. “race a car” is not a palin原创 2016-06-07 15:33:57 · 256 阅读 · 0 评论