![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
煮茶听雪
雄关漫道真如铁,而今迈步从头越
展开
-
[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原创 2016-06-24 09:35:46 · 182 阅读 · 0 评论 -
[Leetcode]2. 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 link原创 2016-06-24 10:18:25 · 198 阅读 · 0 评论 -
[Leetcode]3. 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 answer is "原创 2016-06-24 11:22:48 · 170 阅读 · 0 评论 -
[Leetcode]6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I原创 2016-06-24 14:15:12 · 221 阅读 · 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.题目很简单,没啥好说的。/** * Definition for singly-linked list.原创 2016-06-26 17:36:12 · 175 阅读 · 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原创 2016-06-26 18:15:42 · 178 阅读 · 0 评论 -
[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. Y原创 2016-06-26 18:37:48 · 229 阅读 · 0 评论 -
[Leetcode]206. Reverse Linked List
Reverse a singly linked list./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solu原创 2016-06-27 08:36:19 · 188 阅读 · 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./** * Definition for s原创 2016-06-27 09:09:48 · 219 阅读 · 0 评论 -
[Leetcode]203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5/** * Definition for si原创 2016-06-27 09:45:49 · 240 阅读 · 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.For example, the following two linked lists:A: a1 → a2 ↘原创 2016-06-27 10:52:33 · 163 阅读 · 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原创 2016-06-27 11:35:06 · 206 阅读 · 0 评论 -
[Leetcode]82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->原创 2016-06-27 15:46:17 · 232 阅读 · 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?这个题比较简单,可以设置两根指针,一个每次移动一格,一个移动两格,若发生两根指针指向同一个节点的情况,则说明链表含有环。/** * Definition fo原创 2016-06-27 09:26:14 · 184 阅读 · 0 评论 -
[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 removes the last stone will be the原创 2016-06-06 21:53:43 · 542 阅读 · 0 评论 -
[leetcode]283. 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.For example, given nums = [0, 1, 0, 3, 12], after calling you原创 2016-06-06 22:01:23 · 215 阅读 · 0 评论 -
[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 are ugly while 14 is not ugly sinc原创 2016-06-06 22:05:35 · 288 阅读 · 0 评论 -
[Leetcode]258. Add Digits
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 on原创 2016-06-06 22:09:21 · 195 阅读 · 0 评论 -
[Leetcode]242. Valid Anagram
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:You may ass原创 2016-06-06 22:15:43 · 197 阅读 · 0 评论 -
[Leetcode]231. Power of Two
Given an integer, write a function to determine if it is a power of two.麻烦一点的话,用循环来判断。class Solution {public: bool isPowerOfTwo(int n) { if(n<=0) return false; while原创 2016-06-06 22:22:24 · 176 阅读 · 0 评论 -
[Leetcode]217. 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-06-06 22:28:06 · 158 阅读 · 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原创 2016-06-06 22:31:04 · 181 阅读 · 0 评论 -
[Leetcode]172. Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.我们对n!分解质因子,n!=2^x+3^y+5^z+...,结尾的0一定是1个2和1个5相乘得到的。很显然,这里因子5的个数小于因子2的原创 2016-06-06 22:41:43 · 191 阅读 · 0 评论 -
[Leetcode]171. Excel Sheet Column Number
Related to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ...原创 2016-06-06 22:45:00 · 181 阅读 · 0 评论 -
[Leetcode]70. Climbing Stairs
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 ways can you climb to the top?其实这是一个斐波那契数列,如果用递归方式来解决,时间会超原创 2016-06-06 22:49:29 · 170 阅读 · 0 评论 -
[Leetcode]69. Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.class Solution {public: int mySqrt(int x) { long long left, right, mid; left = 0; right = x;原创 2016-06-06 22:52:07 · 196 阅读 · 0 评论 -
[leetcode]50. Pow(x, n)
Implement pow(x, n).将x的乘积(ret)每次翻番同时将计数(cnt)翻倍,cnt的二倍超出n时,重新计算pow(x, n-cnt),这样ret * pow(x, n-cnt)即是最终的结果。n为负数时用1除以相反数的计算结果即可。测试用例中n的测试数据有INT_MIN,这里直接包了一层函数用long long表示,不然要判断是否是INT_MIN进行特殊处理。cl原创 2016-06-06 23:03:48 · 259 阅读 · 0 评论 -
[Leetcode]27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.原创 2016-06-07 10:54:50 · 127 阅读 · 0 评论 -
[Leetcode]26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with原创 2016-06-07 10:57:30 · 200 阅读 · 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?/** * Definition for singly-linked list. * struct ListNode { * int va原创 2016-06-28 20:10:17 · 196 阅读 · 0 评论 -
[Leetcode]92. Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note:Given m, n satisfy the原创 2016-06-28 22:03:35 · 225 阅读 · 0 评论 -
[leetcode]107. Binary Tree Level Order Traversal 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 tree [3,9,20,null,null,1原创 2016-07-10 17:10:36 · 163 阅读 · 0 评论 -
[Leetcode]9. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to string, note the re原创 2016-07-10 17:41:36 · 186 阅读 · 0 评论 -
[Leetcode]112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum原创 2016-07-10 19:43:57 · 196 阅读 · 0 评论 -
[Leetcode]36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A partially fille原创 2016-07-10 22:03:03 · 212 阅读 · 0 评论 -
[Leetcode]142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up:Can you solve it without using extra space?设置原创 2016-06-29 15:33:36 · 192 阅读 · 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.递归:时间复杂度O(n),空间复杂度O(logn)./**原创 2016-07-11 17:44:09 · 211 阅读 · 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原创 2016-06-29 17:11:30 · 241 阅读 · 0 评论 -
[Leetcode]61. Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL./** * Definition for singly-link原创 2016-06-29 17:53:22 · 576 阅读 · 0 评论 -
[Leetcode]20. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all va原创 2016-06-29 20:28:07 · 194 阅读 · 0 评论