博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-Solution/ ———————————————————————————————————————— ———————————————————————————————————————— LeetCode OJ 题解 LeetCode OJ is a platform for preparing tech…
50. Pow(x, n) (中等) double myPow(double x, int n) { ; unsigned long long p; ) { p = -n; x = / x; } else { p = n; } while (p) { ) ans *= x; x *= x; p >>= ; } return ans; } 96. Unique Binary Search Trees(很快) class Solution { public: int numTrees(int n)…
这是leetcode第一题,通过较为简单. 第一题用来测试的,用的c,直接暴力法过, /** * Note: The returned array must be malloced, assume caller calls free(). */ int* twoSum(int* nums, int numsSize, int target) { ]={}; ;i<numsSize-;i++) ;j<numsSize;j++) if(nums[i]+nums[j]==target){ a[]=i…
https://leetcode.com/problems/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. 思路: DFS AC代码: 1.递归 /…
https://leetcode.com/problems/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 o…
https://leetcode.com/problems/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: Elements in a triplet (a,b,c) must be in non-des…
https://leetcode.com/problems/valid-palindrome/ 题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car"…
https://leetcode.com/problems/min-stack/ 题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the to…
https://leetcode.com/problems/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 additi…
https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->…
https://leetcode.com/problems/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->…
https://leetcode.com/problems/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? 1)递归: 超时 class Solution { public…
https://leetcode.com/problems/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(strin…
https://leetcode.com/problems/plus-one/ 题目: 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. 思路: 设置进1位,逢9进1.注意999这种情况要首位inser…
https://leetcode.com/problems/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 de…
https://leetcode.com/problems/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…
https://leetcode.com/problems/container-with-most-water/ 题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0)…
https://leetcode.com/problems/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, and there exists one unique longest palindromic substring. 思路: 我的思路是遍…
https://leetcode.com/problems/implement-strstr/ 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思路: 判断一个string是否另一个string的子序列并返回位置. naive法:遍历查找,复杂度O(mn). advance法还有Rabi…
https://leetcode.com/problems/remove-element/ Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 思路: 因为可以改变元…