![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
Brett-Xu
Stay hungry! Stay foolish!
展开
-
[leetcode] Two Sum
Question: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, and you may not us...原创 2018-10-20 16:02:18 · 73 阅读 · 0 评论 -
[leetcode] Reverse Integer
Question:Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example 2:Input: -123Output: -321Example 3:Input: 120Output: 21Note:Assume...原创 2018-10-20 16:32:50 · 62 阅读 · 0 评论 -
[leecode] Palindrome Number
Question:Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.Example 1:Input: 121Output: trueExample 2:Input: -121Output:...原创 2018-10-20 16:52:11 · 80 阅读 · 0 评论 -
[leetcode]Climbing Stairs
Question: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?Note: Given n will be...原创 2018-10-20 16:57:51 · 79 阅读 · 0 评论 -
[leetcode]Pascal's triangle 2
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.Note that the row index starts from 0.分析:杨辉三角要求第k行(k从0开始)的值,杨辉三角每一行的首尾的值都为1,每一行值的个数依次加一。且第k行(k>=2)...原创 2018-10-29 09:31:16 · 64 阅读 · 0 评论 -
[leetcode]Roman to Integer
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.Symbol ValueI 1V 5X 10L 50C 100D ...原创 2018-10-27 11:35:47 · 66 阅读 · 0 评论 -
[leetcode]Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string "".Example 1:Input: ["flower","flow","flight"]Output:...原创 2018-10-27 11:46:12 · 67 阅读 · 0 评论 -
[leetcode]Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.An input string is valid if:Open brackets must be closed by the same type of b...原创 2018-10-27 12:03:14 · 65 阅读 · 0 评论 -
[leetcode]Remove Element
Given an array nums and a value val, 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 by modifying the input arra...原创 2018-11-03 10:35:24 · 58 阅读 · 0 评论 -
[leetcode]Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1if needle is not part of haystack.Example 1:Input: haystack = "hello", needle = "ll"Output: 2Clarification:What should we...原创 2018-11-03 10:52:24 · 73 阅读 · 0 评论 -
[leetcode]Plus One
Given a non-empty array of digits representing a non-negative integer, plus one to the integer.The digits are stored such that the most significant digit is at the head of the list, and each element...原创 2018-11-06 10:39:42 · 70 阅读 · 0 评论 -
[leetcode]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.Example:Input: 1->2->4, 1->3->4Output: 1-...原创 2018-11-02 10:02:49 · 56 阅读 · 0 评论 -
[leetcode]Remove Duplicated from Sorted Array
Given a sorted array nums, 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 by modifyi...原创 2018-11-02 10:17:36 · 75 阅读 · 0 评论 -
[leetcode]Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array....原创 2018-11-04 10:57:38 · 70 阅读 · 0 评论 -
[leetcode]Count and Say
The count-and-say sequence is the sequence of integers with the first five terms as following:1. 12. 113. 214. 12115. 1112211 is read off as "one 1" or 11.11 is read off ...原创 2018-11-04 11:28:30 · 77 阅读 · 0 评论 -
[leetcode]Add Binary
Given two binary strings, return their sum (also a binary string).The input strings are both non-empty and contains only characters 1 or 0.Example 1:Input: a = "11", b = "1"Output: "100"分析:...原创 2018-11-10 10:28:21 · 88 阅读 · 0 评论 -
[leetcode]Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x, where x is guaranteed to be a non-negative integer.Since the return type is an integer, the decimal digits are truncated and only...原创 2018-11-10 10:39:44 · 95 阅读 · 0 评论 -
[leetcode]83. Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.Example 1:Input: 1->1->2Output: 1->2分析:删除排好序的链表中重复的项。若链表为空或者只有一个元素,直接返回;否则定义一个结点,从头开始遍历,依次比...原创 2018-11-13 10:14:25 · 87 阅读 · 0 评论 -
[leetcode]Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.Example:Input: [-2,1,-3,4,-1,2,1,-5,4],Output: 6Explanati...原创 2018-11-05 19:24:01 · 76 阅读 · 0 评论 -
[leetcode]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 defin...原创 2018-11-05 19:48:25 · 73 阅读 · 0 评论 -
[leetcode]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?Note: Given n will be a positive...原创 2018-11-11 10:22:13 · 63 阅读 · 0 评论 -
[leetcode]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 i...原创 2018-11-08 10:05:21 · 80 阅读 · 0 评论 -
[leetcode]Contains Duplicate2
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k....原创 2018-11-08 10:30:51 · 85 阅读 · 0 评论 -
[leetcode]Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n-1, find the one that is missing from the array.Example 1:Input: [3,0,1]Output: 2分析:找出数组中0~n-1缺失的数字。可以对数组排序,如果排好序的两个数字之...原创 2018-11-12 10:18:12 · 125 阅读 · 0 评论 -
[leetcode]121. Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock),...原创 2018-11-16 09:53:42 · 125 阅读 · 0 评论 -
[leetcode]122. Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy on...原创 2018-11-16 09:59:01 · 97 阅读 · 0 评论 -
[leetcode]326. Power of Three
Given an integer, write a function to determine if it is a power of three.Example 1:Input: 27Output: true分析:判断一个数是否为3的幂。可以依次/3,直到为1或者%3余数不为0。只要出现余数不为0,则为false,若最后余数为0,则为true,特别的是1为true,小于等于0...原创 2018-11-21 10:09:36 · 61 阅读 · 0 评论 -
[leetcode]342. Power of Four
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.分析:和判断一个数是否为3的幂类似,可以用循环/4判断余数或者利用对数来判断,代...原创 2018-11-21 10:13:28 · 82 阅读 · 0 评论 -
[leetcode]141. Linked List Cycle
Given a linked list, determine if it has a cycle in it.分析:判断一个链表是否存在环。设置快慢指针,慢指针一次走一步,快指针一次走两步,如果最后快指针能够追上慢指针,则该链表存在环。/** * Definition for singly-linked list. * struct ListNode { * int v...原创 2018-11-15 09:59:27 · 67 阅读 · 0 评论 -
[leetcode]88. Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:The number of elements initialized in nums1 and nums2 are m and nrespectively. You may assume that...原创 2018-11-15 10:07:04 · 56 阅读 · 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?分析:判断一个链表是否为环形链表,且不要使用额外空间。判断环形链表通常利用快慢指针来做,快指针一次走两步,慢指针一次走一步,若存在环,则快指针定会与慢指针相遇,/*...原创 2018-11-22 09:16:51 · 68 阅读 · 0 评论 -
[leetcode]125. Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.Note: For the purpose of this problem, we define empty string as valid palindrome.Examp...原创 2018-11-17 10:00:50 · 88 阅读 · 0 评论 -
[leetcode]349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.Example 1:Input: nums1 = [1,2,2,1], nums2 = [2,2]Output: [2]Note:Each element in the result must be unique. The result can b...原创 2018-11-24 10:12:11 · 94 阅读 · 0 评论 -
[leetcode]350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.Example 1:Input: nums1 = [1,2,2,1], nums2 = [2,2]Output: [2,2]Note:Each element in the result should appear as many times as ...原创 2018-11-24 10:20:57 · 86 阅读 · 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.Example:Input: [0,1,0,3,12]Output: [1,3,12,0,0]Note:You m...原创 2018-11-18 10:54:29 · 90 阅读 · 0 评论 -
[leetcode]136. Single Number
Given a non-empty 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 us...原创 2018-11-20 10:33:24 · 62 阅读 · 0 评论 -
[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 on top of the stack. top() -- Ge...原创 2018-11-25 18:53:29 · 74 阅读 · 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 ↘ ...原创 2018-11-25 19:04:15 · 59 阅读 · 0 评论 -
[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 ⌋ times.You may assume that the array is non-empty and the majority element alwa...原创 2018-12-04 10:39:40 · 112 阅读 · 0 评论 -
[leetcode]171. Excel Sheet Column Number
Given a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> ...原创 2018-12-04 10:51:40 · 86 阅读 · 0 评论