LeetCode
AlphaGQ
握不住的沙,何不随手扬了它
展开
-
按照分数进行名次计算、名次排序
一个元素在一个序列中的名次是所有比它小的元素个数加上在它左边出现的与它相同的元素个数例如:数组a=[4,2,9,2,6]是一个序列,各元素的名次为ra=[2,0,4,1,3]。//名次计算templatevoid rank(T a[], int n, int ra[]){ //给数组a的n个元素排名次结果在ra中返回 for (int 1 = 0; i < n; i++) {原创 2017-03-17 20:38:51 · 7344 阅读 · 0 评论 -
[LeetCode]18.4Sum
题目:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note: The solut原创 2017-05-13 17:30:08 · 278 阅读 · 0 评论 -
[LeetCode]31.nextPermutation
题目: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 the lowest possibl原创 2017-05-24 18:08:02 · 326 阅读 · 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 link原创 2017-05-15 22:02:33 · 290 阅读 · 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 valid原创 2017-05-15 22:15:27 · 451 阅读 · 0 评论 -
[leetCode]11.Container With Most Water
题目:Given n non-negativeintegers a1, a2, ..., an, where eachrepresents a point at coordinate (i, ai). n verticallines are drawn such that the two endpoints of line i is at (i, ai) and (i,0). Find原创 2017-05-07 22:38:11 · 363 阅读 · 0 评论 -
[LeetCode]33. Search in Rotated Sorted Array
题目:Suppose a sorted array is rotatedat some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 01 2).You are given a target value to search.If found in the array return its原创 2017-05-26 21:56:59 · 304 阅读 · 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. * struct原创 2017-05-16 22:35:35 · 331 阅读 · 0 评论 -
[LeetCode]12. Integer to Roman
题目:Given an integer, convert it to a romannumeral.Input is guaranteed to be within the rangefrom 1 to 3999.罗马数字简介:罗马数字采用七个罗马字母作数字、即Ⅰ(1)、X(10)、C(100)、M(1000)、V(5)、L(50)、D(500)。记数的方法:相同的数字连写,所表示原创 2017-05-09 19:26:54 · 287 阅读 · 0 评论 -
[LeetCode]13. Roman to Integer
题目:Given a roman numeral, convert it to aninteger.Input is guaranteed to be within the rangefrom 1 to 3999.代码如下://罗马数字转为int型整数 int RomanToint(string &s ) { unordered_map T = { { 'I', 1 }, {原创 2017-05-09 19:49:35 · 258 阅读 · 0 评论 -
[LeetCode]22. Generate Parentheses
题目:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:["((()))","(()())","(())()","()(())"原创 2017-05-18 22:22:18 · 336 阅读 · 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.在处理这种问题时,我们通常加上一个dummy头结点指向head,至于思路很清晰了就是隔一个去交换两个原创 2017-05-20 21:10:37 · 316 阅读 · 0 评论 -
[LeetCode]26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such thateach element appear only once and return the new length.Do not allocate extra space for another array,you must do this in place with原创 2017-05-20 21:30:44 · 253 阅读 · 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原创 2017-05-20 21:46:11 · 310 阅读 · 0 评论 -
[LeetCode]17. Letter Combinations of a Phone Number
题目:Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit str原创 2017-05-13 16:26:47 · 310 阅读 · 0 评论 -
[LeetCode]16.threeSumClosest
/*************************************************题目:找出数组中3个数和最接近Target的值For example, given array S = {-1 2 1 -4}, and target = 1.The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).*****原创 2017-05-13 14:28:50 · 413 阅读 · 0 评论 -
2.addTwoNumbers
题目如下:You are giventwo non-empty linked lists representing two non-negativeintegers. The digits are stored in reverse order and each of their nodescontain a single digit. Add the two numbers and re原创 2017-03-17 21:37:01 · 359 阅读 · 0 评论 -
6. ZigZag Conversion
题目:The string "PAYPALISHIRING" iswritten in a zigzag pattern on a given number of rows like this: (you may wantto display this pattern in a fixed font for better legibility)P A H NA P L S I原创 2017-03-28 00:23:47 · 290 阅读 · 0 评论 -
9. Palindrome Number
题目:9. Palindrome NumberDeterminewhether an integer is a palindrome. Do this without extra space。该题比较简单代码如下//法一class Solution1 {public: bool isPalindrome(int x) { if (x<0 || (x != 0 && x %原创 2017-04-08 22:25:59 · 367 阅读 · 0 评论 -
[Leetcode]10.RegularExpression Matching
题目:10.RegularExpression MatchingImplement regular expression matching with support for'.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element. The matc原创 2017-04-09 15:51:05 · 419 阅读 · 0 评论 -
1.twoSum
题目: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 the sa原创 2017-03-14 22:13:00 · 556 阅读 · 0 评论 -
4.Median of Two Sorted Arrays
题目:There are twosorted arrays nums1 and nums2 of size m and nrespectively.Find the median of the two sorted arrays. The overall runtime complexity should be O(log (m+n)).Example 1: nums1原创 2017-03-26 14:37:26 · 589 阅读 · 0 评论 -
3.lengthOfLongestSubstring
题目: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 i原创 2017-03-18 23:40:39 · 1238 阅读 · 0 评论 -
5. Longest Palindromic Substring
题目:Given a string s, find thelongest palindromic substring in s. You may assume that the maximumlength of s is 1000. Example: Input:"babad" Output:"bab" Note:"aba"原创 2017-03-26 20:17:21 · 363 阅读 · 0 评论 -
7. Reverse Integer
题目:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Note:The input is assumed to be a 32-bit signed integer. Your functio原创 2017-04-05 20:15:18 · 258 阅读 · 0 评论 -
8.Atoi
题目:Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible i原创 2017-04-05 22:44:37 · 270 阅读 · 0 评论 -
[LeetCode]14. Longest Common Prefix
题目:Write a function to find the longest common prefix string amongst an array of strings.寻找一组字符串中的最大公共字符串#include#include#includeusing namespace std;class Solution {public: string longestCom原创 2017-05-11 22:40:31 · 278 阅读 · 0 评论 -
[LeetCode]29. Divide Two Integers
题目:Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.题意:求两个数的商;有两种情况溢出:1.divisor = 0;2.dividend = INT_MIN and divisor = -1 (因为 abs原创 2017-05-20 22:14:04 · 312 阅读 · 0 评论 -
[LeetCode]15.threeSum
/************************************************* 题目:找出数组中3个数和为0的组合 For example, given array S = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ] **************************原创 2017-05-13 12:46:39 · 309 阅读 · 0 评论 -
[LeetCode]28. Implement strStr()
题目:Implement strStr().Returns the index of the first occurrence of needle in haystack,or -1 if needle is not part of haystack.意思是:从haystack字符串中查找给定字符串needle第一次出现的位置代码如下:class Solution {pu原创 2017-05-20 21:53:45 · 386 阅读 · 0 评论