![](https://img-blog.csdnimg.cn/20201014180756919.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode算法练习(Easy)
我们要爱学习
敲敲代码,各种玩耍
展开
-
Palindrome Number问题及解法
问题描述:Determine whether an integer is a palindrome. Do this without extra space.求解一个数是不是回文数。问题求解思路:1.负数不是回文数2.回文数正着读和反着读一样,故可将该数反转与原数比较大小我的代码如下:class Solution {public: bool i原创 2017-02-28 14:38:35 · 217 阅读 · 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, and you may not use原创 2017-02-13 17:28:39 · 843 阅读 · 0 评论 -
Reverse Integer问题及解法
问题描述:Reverse digits of an integer.示例:Example1: x = 123, return 321Example2: x = -123, return -321话不多说,代码很清楚class Solution {public: int reverse(int x) { long long rev = 0;原创 2017-02-24 09:09:03 · 356 阅读 · 0 评论 -
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原创 2017-03-08 15:28:44 · 328 阅读 · 0 评论 -
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).示例:Given binary tree [3,9,20,null,null,15,原创 2017-03-18 09:13:49 · 197 阅读 · 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.问题分析:我们根据罗马数的特点,从后往前循环字符串,若i代表的值小于i+1代表的值,sum值减小,否则就增加。详见代码:class Solution原创 2017-03-05 10:00:15 · 253 阅读 · 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.问题分析:该题主要是将两个有序的链表合并为一个有序的列表,方法很简单,直接看代码即可原创 2017-03-09 12:51:29 · 227 阅读 · 0 评论 -
Longest Common Prefix问题及解法
问题描述:Write a function to find the longest common prefix string amongst an array of strings.问题分析:我们只需要从头到尾把每个字符串共同的字符前缀找出即可。详见代码:class Solution {public: string longestCommonPrefi原创 2017-03-06 10:49:00 · 266 阅读 · 0 评论 -
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 memo原创 2017-03-10 10:52:30 · 427 阅读 · 0 评论 -
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 pla原创 2017-03-10 10:18:44 · 219 阅读 · 0 评论 -
Convert Sorted Array to Binary Search Tree问题及解法
问题描述:Given an array where elements are sorted in ascending order, convert it to a height balanced BST。问题分析:将一个有序数组转化为平衡二叉搜索树。平衡二叉树有个特点:它的左子树和右子树均为平衡二叉树。因此可以考虑利用递归的方式求解。先求根节点,再求左右子树。过原创 2017-03-20 16:13:01 · 169 阅读 · 0 评论 -
Implement strStr()问题及解法
问题描述:Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.问题分析:返回needle串在haystack串中首次出现的位置,若没有出现,则返回-1;详细过程请看我的代码原创 2017-03-10 14:49:26 · 253 阅读 · 0 评论 -
Remove Linked List Elements问题及解法
问题描述:Remove all elements from a linked list of integers that have value val.示例:Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5问题分析:给一个头结点thead连接h原创 2017-03-28 08:21:04 · 344 阅读 · 0 评论 -
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原创 2017-03-11 11:50:53 · 287 阅读 · 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 node neve原创 2017-03-21 13:02:29 · 293 阅读 · 0 评论 -
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.问题分析:求解一棵二叉树的最小深度,可采用深度遍历的方原创 2017-03-21 13:23:17 · 171 阅读 · 0 评论 -
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.示例:Given the below binary tree and sum原创 2017-03-21 15:46:31 · 219 阅读 · 0 评论 -
Isomorphic Strings问题及解法
问题描述:Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with原创 2017-03-28 15:43:14 · 282 阅读 · 0 评论 -
Reverse Linked List问题及解法
问题描述:Reverse a singly linked list.问题分析:翻转一个单链表,可以在o(n)时间内完成,只需要设置一个前结点last,在遍历过程中先保留当前结点head的下一结点指针,然后再完成当前结点head和前结点last的连接。过程详见代码:/** * Definition for singly-linked list. * struct Li原创 2017-03-28 16:33:49 · 848 阅读 · 0 评论 -
Power of Four问题及解法
问题描述:Given an integer (signed 32 bits), write a function to check whether it is a power of 4.示例:Given num = 16, return true. Given num = 5, return false.问题分析:一个数若是4的幂,那么它一定是2的幂,而且转换成二进制时原创 2017-04-07 09:08:21 · 314 阅读 · 0 评论 -
Pascal's Triangle问题及解法
问题描述:Given numRows, generate the first numRows of Pascal's triangle.示例:For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]问题分析:可以发现其中有规律原创 2017-03-21 19:06:37 · 432 阅读 · 0 评论 -
Pascal's Triangle II问题及解法
问题描述:Given an index k, return the kth row of the Pascal's triangle.示例:For example, given k = 3,Return [1,3,3,1].问题分析:本题与Pascal's Triangle问题类似,可参考我之前的Pascal's Triangle问题及解法的博客。具体过原创 2017-03-21 20:07:17 · 236 阅读 · 0 评论 -
Intersection of Two Arrays II问题及解法
问题描述:Given two arrays, write a function to compute their intersection.示例:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].问题分析:寻找两个数组共同的元素,可重复。可以利用map把nums1中的元素出现次数记录下来,跟nums2原创 2017-04-07 10:50:48 · 393 阅读 · 0 评论 -
Sum of Two Integers问题及解法
问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.示例:Given a = 1 and b = 2, return 3.问题分析:不用+和-,我们可以考虑从位运算的角度出发。主要用到了进位操作和异或运算.原创 2017-04-07 12:46:42 · 243 阅读 · 0 评论 -
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 (ie, buy one and sell one share of the原创 2017-03-22 10:42:50 · 261 阅读 · 0 评论 -
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原创 2017-03-29 08:51:10 · 4035 阅读 · 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 el原创 2017-03-29 09:10:02 · 331 阅读 · 0 评论 -
Contains Duplicate II问题及解法
问题描述: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原创 2017-03-29 09:50:06 · 310 阅读 · 0 评论 -
Single Number问题及解法
问题描述:Given an array of integers, every element appears twice except for one. Find that single one.问题分析:本题可用位运算------异或(相同的数异或为0,任何数与0异或均为0)过程详见代码:class Solution {public: int singleN原创 2017-03-22 11:02:35 · 852 阅读 · 0 评论 -
Valid Palindrome问题及解法
问题描述:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.示例:"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a pali原创 2017-03-22 15:46:28 · 449 阅读 · 0 评论 -
Maximum Subarray问题及解法
问题描述:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.示例:For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray原创 2017-03-12 12:32:22 · 386 阅读 · 0 评论 -
Invert Binary Tree问题及解法
问题描述:Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1过程详见代码:/** * Definition for a binary tree node. * struct Tree原创 2017-03-29 13:18:55 · 275 阅读 · 0 评论 -
Implement Stack using Queues问题及解法
问题描述:Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Re原创 2017-03-29 14:13:55 · 234 阅读 · 0 评论 -
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原创 2017-03-13 17:32:30 · 1958 阅读 · 0 评论 -
Power of Two问题及解法
问题描述:Given an integer, write a function to determine if it is a power of two.问题分析:每一个2的幂,都是大于0的整数,且转换成二进制时,里面1的个数只有一个。过程详见代码:class Solution {public: bool isPowerOfTwo(int n) {原创 2017-03-30 08:16:44 · 498 阅读 · 0 评论 -
Implement Queue using Stacks问题及解法
问题描述:Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front eleme原创 2017-03-30 08:42:25 · 205 阅读 · 0 评论 -
Binary Tree Paths问题及解法
问题分析:Given a binary tree, return all root-to-leaf paths.示例:given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]问题分析:这里涉及到将整数转换为字符原创 2017-03-30 09:31:09 · 336 阅读 · 0 评论 -
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原创 2017-03-30 10:26:33 · 1147 阅读 · 0 评论 -
Add Digits问题及解法
问题描述:Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.示例:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one dig原创 2017-03-30 13:01:57 · 284 阅读 · 0 评论 -
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 ma原创 2017-03-30 15:39:38 · 311 阅读 · 0 评论