LeetCode
小贤2016
中山大学数据科学与计算机学院16级研究生
展开
-
LeetCode:Kth Largest Element in an Array
LeetCode,Kth Largest Element in an Array : *Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,原创 2017-03-05 22:53:30 · 235 阅读 · 0 评论 -
LeetCode:8. String to Integer (atoi)
题目链接:https://leetcode.com/problems/string-to-integer-atoi/#/description愿意就是把string型转化成int型,但是其中有很多种情况,比如说遇到字母,符号,空格,溢出的处理办法,经过很多次的测试,终于accept了。代码如下:class Solution { public: int myAtoi(string str) {原创 2017-06-28 18:35:55 · 191 阅读 · 0 评论 -
LeetCode: Combination Sum
题目: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen fro原创 2017-06-08 10:32:18 · 168 阅读 · 0 评论 -
LeetCode: count and say
题目:The count-and-say sequence is the sequence of integers with the first five terms as following: 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原创 2017-06-09 11:09:24 · 219 阅读 · 0 评论 -
LeetCode: Maximim SubArray
https://leetcode.com/problems/maximum-subarray/题目原意是给定一个序列,找出和最大的连续子序列。如:[-2,1,-3,4,-1,2,1,-5,4],最大子序列是[4,-1,2,1],返回最大和是6。经典的动态规划问题,在没有思路前一直在用贪心算法的思路,后来看了别人的博客才发现思路不对,应该用动态规划来做。这道题代码很简单,但是思路却没那么简单。 一个重原创 2017-06-12 09:57:26 · 245 阅读 · 0 评论 -
LeetCode: N-Queens
题目链接:https://leetcode.com/problems/n-queens/#/description这是十分经典的八皇后问题的变形,以八皇后问题为例,在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。方法: 回溯法。原创 2017-06-12 10:09:47 · 213 阅读 · 0 评论 -
LeetCode: N-Queens II
题目链接:https://leetcode.com/problems/n-queens-ii/#/description这道题也是八皇后问题的变形,不过上一题是要求输出各组不同的解,这道题是输出解的个数,只需要稍微改动就可以了,设置一个变量sum来记录解的总数。每得到一组解sum就加一即可。class Solution { public: int *x; int sum = 0;原创 2017-06-12 10:56:31 · 204 阅读 · 0 评论 -
LeetCode: Subsets
题目链接:https://leetcode.com/problems/subsets/#/description 意思就是给定一个数组,输出它的所有子集(空集也包括)解题方法: 使用回溯法,利用递归来实现。代码如下:class Solution { public: vector<vector<int>> subsets(vector<int>& nums) { vector原创 2017-06-14 17:03:09 · 255 阅读 · 0 评论 -
LeetCode: 368. Largest Divisible Subset
题目链接: https://leetcode.com/problems/largest-divisible-subset/#/description这是一道动态规划题,重点是写出他的状态转移方程,这道题状态转移方程是: dp[i] = dp[j] +1 ( 当nums[j] % nums[i] == 0)。 所以可以得出解题代码:class Solution { public: vector原创 2017-06-14 22:37:52 · 191 阅读 · 0 评论 -
Leetcode: Longest Palindromic Substring
问题描述: Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example:Input: “babad”Output: “bab”Note: “aba” is also a valid answer. Example原创 2017-05-22 08:15:27 · 228 阅读 · 0 评论 -
LeetCode: 3 Sum and 3Sum Closest问题
3 Sum closest *Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input原创 2017-04-27 11:05:19 · 316 阅读 · 0 评论 -
LeetCode:Two Sum
LeetCode,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 yo原创 2017-02-25 20:40:23 · 184 阅读 · 0 评论 -
LeetCode: Reverse Integer
Reverse Integer *Reverse digits of an integer.Example1: x = 123, return 321 Example2: x = -123, return -321click to show spoilers.Note: The input is assumed to be a 32-bit signed integer. Your fu原创 2017-03-12 19:20:16 · 172 阅读 · 0 评论 -
LeetCode: 3Sum
3Sum *Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: The solution set must not cont原创 2017-03-13 10:52:47 · 172 阅读 · 0 评论 -
LeetCode:Rotate Image
Rotate Image *You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).*解题过程 : 用一个二维数组来模拟一个图像,要把图像顺时针旋转90度,旋转中心为图像的中心。基本思路是把图片分为行数/2层,然后一层层进行旋转,每一层有上下左原创 2017-04-09 16:37:44 · 186 阅读 · 0 评论 -
LeetCode:Longest Substring Without Repeating Characters
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 lengt原创 2017-03-27 10:16:52 · 176 阅读 · 0 评论 -
LeetCode: Next Permutation
Next Permutation *Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as th原创 2017-05-03 17:58:59 · 178 阅读 · 0 评论 -
LeetCode: Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice?For example, Given sorted array nums = [1,1,1,2,2,3],Your function should ret原创 2017-05-09 09:50:26 · 198 阅读 · 0 评论 -
LeetCode: 204. Count Primes判断是否为素数的高效方法
链接: https://leetcode.com/problems/count-primes/#/description Description: Count the number of prime numbers less than a non-negative number, n. 计算小于n的素数的个数。 这道题用普通方法来判断素数的话必然超限,在别人的博客上看到了一个比较高效的判断素原创 2017-07-19 17:39:24 · 1522 阅读 · 0 评论