leetcode题解汇总

来源:互联网 发布:侣行为什么停播知乎 编辑:程序博客网 时间:2024/05/07 02:59

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 distinct.
链接:http://blog.csdn.net/sysu_cis/article/details/51705591

350.Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].
链接:http://blog.csdn.net/sysu_cis/article/details/51705591

206.Reverse Linked List
Reverse a singly linked list.
链接:http://blog.csdn.net/sysu_cis/article/details/51705591


13.Roman to Integer
Difficulty: Easy
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
题意:将罗马数字转换成阿拉伯数字
链接:http://blog.csdn.net/sysu_cis/article/details/51705839

235.Lowest Common Ancestor of a Binary Search Tree
Difficulty: Easy
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
题意:给定一棵二叉查找树,找到两个给定节点的最低公共祖先。
链接:http://blog.csdn.net/sysu_cis/article/details/51705839

191.Number of 1 Bits
Difficulty: Easy
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 00000000000000000000000000001011,
so the function should return 3.
题意:很简单,就是求32位整型数字的二进制表示中1的个数
链接:http://blog.csdn.net/sysu_cis/article/details/51705839


326.Power of Three | Difficulty: Easy
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?
题意:给定一个整数,判断是不是3的次方,能否不用任何的循环与递归实现?
http://blog.csdn.net/sysu_cis/article/details/51723219
231.Power of Two | Difficulty: Easy
Given an integer, write a function to determine if it is a power of two.
描述:给定一个整数,判断是不是2的次方。
http://blog.csdn.net/sysu_cis/article/details/51723219

263.Ugly Number | Difficulty: Easy
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 since it includes another prime factor 7.

Note that 1 is typically treated as an ugly number.
题意:丑数是指的素因子只包含2,3,5的正数,1是一个特殊的丑数,因为其不包含2、3、5。
http://blog.csdn.net/sysu_cis/article/details/51723219

83.Remove Duplicates from Sorted List | Difficulty: Easy
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.
题意:给定一个排好序的链表,删除所有连续出现的重复值保证每个值只出现一次。
http://blog.csdn.net/sysu_cis/article/details/51723219

202.Happy Number | Difficulty: Easy
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 its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
这里写图片描述
题意:将数字各位的平方相加,然后得到的相加之后再循环上一步操作,如果最后能等于1就是Happy Number,也存在不会停下的的可能。
http://blog.csdn.net/sysu_cis/article/details/51723219


  1. Climbing Stairs | Difficulty: Easy

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?
题意:爬台阶,每次可以爬1-2阶,问有多少中爬法可以到顶?
http://blog.csdn.net/sysu_cis/article/details/51708095
141. Linked List Cycle | Difficulty: Easy

Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
题意:给定一个链表,判断是否有环。
http://blog.csdn.net/sysu_cis/article/details/51708095
121. Best Time to Buy and Sell Stock | Difficulty: Easy

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 stock), design an algorithm to find the maximum profit.
题意:假定有一个数组给定第i天的石头的价格,设计一个最大利润的算法。
http://blog.csdn.net/sysu_cis/article/details/51708095
21. Merge Two Sorted Lists | Difficulty: Easy

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.
题意:拼接两个排序链表
http://blog.csdn.net/sysu_cis/article/details/51708095


  1. Swap Nodes in Pairs | Difficulty: Easy

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 not modify the values in the list, only nodes itself can be changed.
题意:给一个单向链表,需要交换相邻的两个节点,不能修改链表的节点。
http://blog.csdn.net/sysu_cis/article/details/51723222
345. Reverse Vowels of a String | Difficulty: Easy

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:
Given s = “hello”, return “holle”.

Example 2:
Given s = “leetcode”, return “leotcede”.
题意:将数组中的元音逆序。
http://blog.csdn.net/sysu_cis/article/details/51723222
198. House Robber | Difficulty: Easy

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 have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

题意:一连串非负的整数,不能取连续相邻的两个元素,如何取才能取得最大的累加和。
http://blog.csdn.net/sysu_cis/article/details/51723222


今日题目:1、二叉树的自底向上遍历;2、判定是否平衡树;3、去除元素;4、加1

  1. Binary Tree Level Order Traversal II | Difficulty: Easy

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,15,7],
Given a linked list, swap every two adjacent nodes and return its head.
题意:给定一棵二叉树,给出自底向上的遍历。
http://blog.csdn.net/sysu_cis/article/details/51730192
110. Balanced Binary Tree | Difficulty: Easy

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 never differ by more than 1.
题意:判断一棵树是否是平衡树,平衡树的定义是每个节点的子树高度相差不会大于1.
http://blog.csdn.net/sysu_cis/article/details/51730192
27. Remove Element | Difficulty: Easy

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.

The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

Example:
Given input array nums = [3,2,2,3], val = 3

Your function should return length = 2, with the first two elements of nums being 2.
题意:返回数组中不为val的元素的个数。
http://blog.csdn.net/sysu_cis/article/details/51730192
66. Plus One | Difficulty: Easy

Given a non-negative number represented as an array of digits, plus one to the number.

The digits are stored such that the most significant digit is at the head of the list.
http://blog.csdn.net/sysu_cis/article/details/51730192


  1. Symmetric Tree | Difficulty: Easy

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
Bonus points if you could solve it both recursively and iteratively.
题意:给定一棵二叉树,检查它是否对称。
http://blog.csdn.net/sysu_cis/article/details/51736673
342. Power of Four | Difficulty: Easy

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.
题意:给一个int型的数,判断是否是4的平方
http://blog.csdn.net/sysu_cis/article/details/51736673
232. Implement Queue using Stacks | Difficulty: Easy

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 element.
empty() – Return whether the queue is empty.

题意:使用栈来模拟队列的功能。
http://blog.csdn.net/sysu_cis/article/details/51736673
118. Pascal’s Triangle | Difficulty: Easy

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]
]
题意:杨辉三角,给出一个数返回一个杨辉三角形式的数组。
http://blog.csdn.net/sysu_cis/article/details/51736673


0 0
原创粉丝点击