leetcode
exmy
Less is more, and more is less.
展开
-
leetcode--004. Median of Two Sorted Arrays
Median of Two Sorted Arrays题意求两个有序数组的中位数。题解way 1. 合并两个数组,然后排序,找中位数,O(nlogn)O(nlogn),当然这样效率不高。class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {原创 2016-05-13 22:09:30 · 385 阅读 · 0 评论 -
leetcode--36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’.题解class Solution {public: b原创 2017-01-15 23:18:45 · 271 阅读 · 0 评论 -
leetcode--38. 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 off as “one 2, the原创 2017-01-16 14:16:25 · 221 阅读 · 0 评论 -
leetcode--58. 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 defined as原创 2017-01-16 14:38:00 · 281 阅读 · 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.Your algorithm should use only constant space. You may no原创 2017-01-12 21:47:00 · 276 阅读 · 0 评论 -
leetcode--66. Plus One
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The digits are st原创 2017-01-17 19:56:11 · 529 阅读 · 0 评论 -
leetcode--67. Add Binary
Given two binary strings, return their sum (also a binary string).For example, a = “11” b = “1” Return “100”.题解class Solution {public: string addBinary(string a, string b) { string s;原创 2017-01-17 20:42:24 · 260 阅读 · 0 评论 -
leetcode--70. 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?题解class Solution {public: int cl原创 2017-01-17 20:52:37 · 247 阅读 · 0 评论 -
leetcode--108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.题解以序列的中间元素作为根,转换后的BST肯定是平衡的。/** * Definition for a binary tree node. * struct TreeNode { * int va原创 2017-02-08 21:45:46 · 253 阅读 · 0 评论 -
leetcode--110. 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 never differ by原创 2017-02-09 22:24:43 · 224 阅读 · 0 评论 -
leetcode--111. 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-02-10 21:03:11 · 239 阅读 · 0 评论 -
leetcode--112. 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.题解class Solution {public: bool hasPathSum(TreeNo原创 2017-02-10 21:18:10 · 267 阅读 · 0 评论 -
leetcode--83. Remove Duplicates from Sorted List
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.题解/** * Definition for singly-l原创 2017-01-22 22:27:18 · 197 阅读 · 0 评论 -
leetcode--88. Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional原创 2017-01-22 22:53:11 · 211 阅读 · 0 评论 -
leetcode--100. Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.题解/** * Definition原创 2017-01-25 18:51:11 · 234 阅读 · 0 评论 -
leetcode--101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center)./** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; *原创 2017-01-25 20:14:13 · 244 阅读 · 0 评论 -
leetcode--102. Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).题解一个NULL标记一层的结束。/** * Definition for a binary tree node. * struct TreeNode { *原创 2017-01-25 21:56:49 · 243 阅读 · 0 评论 -
leetcode--104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.题解/** * Definition for a binary tree node.原创 2017-01-26 15:56:40 · 231 阅读 · 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.题解class Solution {public: int strStr(string haystack, string needle) {原创 2017-01-15 22:35:51 · 264 阅读 · 0 评论 -
leetcode--26. 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 place with cons原创 2017-01-15 22:20:19 · 276 阅读 · 0 评论 -
leetcode--005. Longest Palindromic Substring
Longest Palindromic Substring题意给一个字符串,找到最长的回文子串。题解对串中的每个字符,分别向左右延伸,判断是否回文。 回文串长度可以是奇数,也可以是偶数,这得注意。class Solution {public: string longestPalindrome(string s) { string ans; for(int原创 2016-05-13 22:16:37 · 465 阅读 · 0 评论 -
leetcode--001. Two Sum
Two sum题意给一个整数数组和数 target,返回数组中某两个数相加之和为 target 的下标。题解思路见代码。 时间复杂度: O(nlogn)O(nlogn)class Solution {public: struct node{ int val, pos; node(int v, int p):val(v), pos(p){} };原创 2016-05-06 21:35:17 · 360 阅读 · 0 评论 -
leetcode--002. Add Two Numbers
Add Two Numbers 题意用链表来表示非负整数,链表的每个节点存储整数的一个数位,且是逆序存储的。 计算两个数相加,返回和的链表。题解逆序存储的好处是计算方便, 链表的第一个节点就是个位,这样只需直接遍历就好了。/** * Definition for singly-linked list. * struct ListNode { * int val; * Lis原创 2016-05-07 23:56:32 · 339 阅读 · 0 评论 -
leetcode--003. Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters题意给定一个字符串,返回其无重复字符的最长子串的长度。题解Way 1. 尺取法。 时间复杂度:O(nlogn)O(nlogn)// 132msclass Solution {public: int lengthOfLongestSubstring(string s) { set<原创 2016-05-08 08:33:58 · 345 阅读 · 0 评论 -
leetcode--007. Reverse Integer
Reverse Integer题意将一个整数反转。 注意负数,溢出的情况。// 8msclass Solution {public: int reverse(int x) { bool flag = false; if(x < 0) { x = -x; flag = true; }原创 2016-06-01 12:55:52 · 333 阅读 · 0 评论 -
leetcode--008. String to Integer (atoi)
String to Integer (atoi)题解考虑如下条件:空字符串,返回 0;第一个数字前是 ‘+’ 或者 ‘-‘;数字前有空格;转换过程中,遇到第一个非数字字符,停止转换。int 范围在[-2147483468, 2147483467],超过这个范围返回相应的INT_MAX, INT_MINclass Solution {public: int myAtoi(stri原创 2016-06-03 23:26:04 · 440 阅读 · 0 评论 -
leetcode--009. Palindrome Number
Palindrome Number题解判断一个整数是否为回文数。 负数不是回文数。bool isPalindrome(int x) { char s[11]; sprintf(s, "%d", x); int n = strlen(s); for(int i = 0, j = n - 1; i < j; i++, j--){ if(s[i] != s[原创 2016-06-04 00:05:25 · 441 阅读 · 0 评论 -
leetcode--006. ZigZag Conversion
ZigZag Conversion题解将一串字符按之字形排列成 n 行。 用一个数组 pos[ ] 记录每个字符排列后的位置。 对于首行和尾行,递增间隔是 2 * numRows - 2; 中间行还要区分不同的列,第一列递增间隔是 2 * (numRows - row - 1),第二列是 2 * row,其中,row表示当前处理的行。 往后的列按此轮流交替。class Solution {原创 2016-05-23 20:27:46 · 353 阅读 · 0 评论 -
leetcode--010 Regular Expression Matching
Regular Expression Matching题解// TODOclass Solution {public: bool isMatch(string s, string p) { if(s.length() == 0){ if(p.length() & 1) return false; else{原创 2016-07-19 09:34:42 · 386 阅读 · 0 评论 -
leetcode--011 Container With Most Water
Container With Most Water题解考虑一个容器,其面积是 min(height[left], height[right]) * (right - left). 利用两个指针,分别指向头和尾,height[l] > height[r] ? r--, l++。class Solution {public: int maxArea(vector<int>& height)原创 2016-07-19 09:53:18 · 405 阅读 · 0 评论 -
leetcode--014. Longest Common Prefix
Longest Common Prefix题解way 1 先将数组排序,那么最长公共前缀就是数组中的第一个和最后一个字符串的最长公共前缀。public class Solution { public String longestCommonPrefix(String[] strs) { if(strs == null || strs.length == 0) return原创 2016-07-31 21:45:14 · 275 阅读 · 0 评论 -
leetcode--015. 3Sum
3Sum题意给一个含有n个整数的数组S, 数组中是否存在三个数,a, b, c 使 a + b + c = 0,找出所有这样的唯一的三元组(a, b, c)。题解对每一个nums[i],在区间[i + 1, n - 1]寻找。class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { v原创 2016-08-02 19:49:18 · 308 阅读 · 0 评论 -
leetcode--016. 3Sum 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 would have exactly原创 2016-08-02 19:51:40 · 404 阅读 · 0 评论 -
leetcode--17. Letter Combinations of a Phone Number
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) i原创 2016-08-07 13:00:13 · 278 阅读 · 0 评论 -
leetcode--20. Valid Parentheses
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 “()[]原创 2016-12-24 20:10:47 · 368 阅读 · 0 评论 -
leetcode--27. Remove Element
Remove ElementDescriptionGiven 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原创 2016-12-19 21:30:33 · 355 阅读 · 0 评论 -
leetcode--118. 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] ]题解a1. class Solution {public:原创 2017-01-07 11:42:22 · 277 阅读 · 0 评论 -
leetcode--107. 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).题解/** * Definition for a binary tree node. * public cl原创 2017-02-03 23:30:40 · 307 阅读 · 0 评论