leetcode
xjc864588399
这个作者很懒,什么都没留下…
展开
-
LeetCode 155. Min Stack
解题思路:利用stack 重写 MinStack() class MinStack { public: /** initialize your data structure here. */ stackres; stackmin; void push(int x) { res.push(x); if(min.empty()原创 2017-02-05 13:54:17 · 238 阅读 · 0 评论 -
LeetCode 100. Same Tree
解题思路:树的递归 class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL || q == NULL) return p == q; else return p->val == q->val && i原创 2017-01-25 09:10:36 · 152 阅读 · 0 评论 -
LeetCode 237. Delete Node in a Linked List
解题思路:假删除,因为不能访问到节点的前一个指针,所以只能用后一个的去覆盖前一个. class Solution { public: void deleteNode(ListNode* node) { *node = *node->next; } };原创 2017-01-25 09:20:08 · 161 阅读 · 0 评论 -
LeetCode
解题思路:统计出现的字符是否都相同且次数相同,使用map容器,运行时间16ms class Solution { public: bool isAnagram(string s, string t) { if(s == t) return true; if(s.length() != t.length()) return false; ma原创 2017-01-25 09:44:02 · 201 阅读 · 0 评论 -
LeetCode 326. Power of Three
解题思路:第一种想到的是打表,直接把整数范围内的是3的立方的数打出来,比较trick 哈.这是一个纯粹的数学问题,看讨论区,有许多关于取对数的方法,log() 和log10(),其中log是以自然对数e为底的,这两个方式对于243来说,结果是不相同的,原因是以e为底的数产生了误差,因为3和e相差远比3和10相差的小,导致换底之后的结果有误差. class Solution { public:原创 2017-01-27 12:15:22 · 298 阅读 · 0 评论 -
LeetCode 171. Excel Sheet Column Number
解题思路: 找出来规律就行了. class Solution { public: int titleToNumber(string s) { int res = 0; for(int i = 0; i < s.length(); i++){ res = res * 26 + int(s[i] - 'A' + 1);原创 2017-01-28 19:46:16 · 234 阅读 · 0 评论 -
LeetCode
解题思路: 使用异或运算 class Solution { public: int missingNumber(vector& nums) { int res = nums.size(); for(int i = 0; i < nums.size(); i++){ res ^= i; res ^= nums[原创 2017-01-28 21:36:41 · 177 阅读 · 0 评论 -
LeetCode 409. Longest Palindrome
解题思路: 一次AC,统计字符串中每个字符出现的次数,偶数个就直接累加,奇数个减一再求和,最后返回值需要注意,含有奇数的字符时,返回结果为count+1,若只有偶数的字符,直接返回count. class Solution { public: int longestPalindrome(string s) { mapres; for(int i = 0;原创 2017-01-29 10:49:37 · 373 阅读 · 0 评论 -
LeetCode 486. Predict the Winner
解题思路:DFS class Solution { int maxScore(vector& nums, int start, int end, vector> &m) { if(start>end) return 0; if(start==end) return nums[start]; if(m[start][end]) return原创 2017-01-29 14:13:06 · 912 阅读 · 0 评论 -
LeetCode 206. Reverse Linked List
解题思路: 使用两个指针,分别指向head 和 head->next,或者直接递归. class Solution { public: ListNode* reverseList(ListNode* head) { ListNode* p1 = NULL; ListNode* p2 = NULL; while(head){原创 2017-02-02 16:06:10 · 163 阅读 · 0 评论 -
LeetCode 35. Search Insert Position
解题思路: 二分法 class Solution { public: int searchInsert(vector& nums, int target) { int index1 = 0; int index2 = nums.size(); int mid = 0; while(index1 < index2){原创 2017-02-02 22:27:16 · 210 阅读 · 0 评论 -
LeetCode 88. Merge Sorted Array
解题思路: 比较两个数组的每个数的大小,从后往前比较,大的保存在nums1的最后,遍历. class Solution { public: void merge(vector& nums1, int m, vector& nums2, int n) { while(m>0 && n>0){ if(nums2[n-1]>nums1[m-1]){原创 2017-02-03 20:36:05 · 332 阅读 · 0 评论 -
LeetCode 58. Length of Last Word
解题思路:使用 istringstream class Solution { public: int lengthOfLastWord(string s) { istringstream ss(s); string result; while(ss>>result){ } return result.leng原创 2017-02-03 21:08:51 · 339 阅读 · 0 评论 -
LeetCode 69. Sqrt(x)
解题思路: 牛顿迭代法, https://en.wikipedia.org/wiki/Integer_square_root#Using_only_integer_division 点击打开链接 class Solution { public: int mySqrt(int x) { long long r = x; while (r*r > x)原创 2017-02-03 21:34:04 · 329 阅读 · 0 评论 -
LeetCode 204. Count Primes
解题思路:维基百科中 https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes Input: an integer n > 1. Let A be an array of Boolean values, indexed by integers 2 to n, initially all set to true. for i =原创 2017-02-03 22:05:24 · 244 阅读 · 0 评论 -
LeetCode 321. Create Maximum Number
由于k的大小不定,所以有三种可能,第一种是当k为0时,两个数组中都不取数;第二种是当k不大于其中一个数组的长度时,有可能只从一个数组中取数;第三种情况是k大于其中一个数组的长度,则需要从两个数组中分别取数,至于每个数组中取几个,每种情况都要考虑到,然后每次更结果即可。对于分别从两个数组中取数字的情况,我们需要将两个取出的小数组混合排序成一个数组,小数组中各自的数字之间的相对顺序不变。我们还需要一个原创 2017-03-18 17:20:50 · 389 阅读 · 0 评论 -
LeetCode 20. Valid Parentheses
解题思路: 首先想到栈的操作,先入后出,用一个vector容器来模拟栈的操作. class Solution { public: bool isValid(string s) { vector res; map m{ {']','['},{')','('},{'}','{'} }; for(int i = 0; i < s.原创 2017-01-25 00:59:34 · 136 阅读 · 0 评论 -
LeetCode 290. Word Patter
解题思路:先将字符串使用string流的形式分成一个个字符串,之后,将pattern和str 分别存到一个map容器中,并比较出现的次数是否相等. class Solution { public: bool wordPattern(string pattern, string str) { stringstream ss(str); string原创 2017-01-24 23:28:18 · 184 阅读 · 0 评论 -
LeetCode 455. Assign Cookies
解题思路:先排序,然后从大到小依次遍历比较 class Solution { public: int findContentChildren(vector& g, vector& s) { sort(g.begin(),g.end()); sort(s.begin(),s.end()); int i=g.size()-1, j=s.size原创 2017-01-21 02:13:05 · 227 阅读 · 0 评论 -
LeetCode 383. Ransom Note
解题思路:这题主要考查字符串的查找,对于这个问题,倘若是一个字符串中必须连续的包含一个字符串,那么直接可以使用c++ string类中的str1.find(str2)这个方法.但是,题目的意思是稍微比这个难一些,是要求一个字符串的所有字符是另一个的子集,考虑使用map,将两个string 重新写入map中,再进行比较,即可.class Solution { public: bool can原创 2017-01-21 03:00:17 · 207 阅读 · 0 评论 -
LeetCode 451. Sort Characters By Frequency
解题思路:使用map 将string里面的每个字符insert进去,之后需要对map的value进行一个降序,考虑到无法直接对map的值进行降序排列,所以把map 放到vector容器中,这样可以对vector直接排序,最后字符串转string,return 即可.class Solution { public: string frequencySort(string s) {原创 2017-01-21 15:38:40 · 256 阅读 · 0 评论 -
LeetCode 462. Minimum Moves to Equal Array Elements II
解题思路:这题相对比较简单,第一次提交就AC了.首先先将vector排序,然后求中位数,每一个数与中位数的差就是需要移动的move,遍历一次求和即可.代码也很短class Solution { public: int minMoves2(vector& nums) { sort(nums.begin(),nums.end()); int count = 0;原创 2017-01-21 21:41:09 · 367 阅读 · 0 评论 -
LeetCode 167. Two Sum II - Input array is sorted
解题思路: 一开始暴力,结果超时,然后利用快排的思想,两个index,一个从头部开始,另一个从尾部开始,每次若是相加和小于target,那么小的index++;否则,大的index++ class Solution { public: vector twoSum(vector& numbers, int target) { vector res; int原创 2017-01-21 22:42:03 · 156 阅读 · 0 评论 -
LeetCode 347. Top K Frequent Elements
解题思路:这一题和leetcode451题的思路是类似的,只不过是返回前topK出现最多的频率,所以自然而然地相当先将vector写进map中,然后根据value值排序,注意题目要求的时间复杂度,使用全排sort虽然也是采用的快排思想,有时也会达到O(N2),所以使用部分排序,堆排序,C++ stl里面有partial_sort,仅对前K个排序. class Solution { public:原创 2017-01-22 08:37:12 · 199 阅读 · 0 评论 -
LeetCode 238. Product of Array Except Self
解题思路:l两个循环,第一个从0到i-1,累乘,第二个从n到i+1,累乘. class Solution { public: vector productExceptSelf(vector& nums) { int N = nums.size(); vector res(N,1); for(int i=0; i<N; i++){ if (i原创 2017-01-22 08:47:34 · 154 阅读 · 0 评论 -
LeetCode 387. First Unique Character in a String
解题思路:先用一个map存放string里面每个字符出现的次数,然后遍历string每个字符,如果字符出现的次数为1就返回index,否则就返回-1 class Solution { public: int firstUniqChar(string s) { map s_m; for(int i = 0; i < s.length(); i++){原创 2017-01-22 21:11:25 · 153 阅读 · 0 评论 -
LeetCode 202. Happy Number
解题思路:因为不知道如何去终止循环,所以看了一下讨论区的方法,一种是根据规律来终止循环,在维基百科上给出了一个"开心数字"的序列,1,7,...所以当发现n的取值范围 min:-2147483648 max:2147483647,而根据"开心数字"的规则,最大的应该是每位都是9,这样最大才810,假设10位全是9,其实并没有那么大,所以可以开一个1000的数组,用来记录这个数组是否出现过,如果出现原创 2017-01-22 23:38:59 · 204 阅读 · 0 评论 -
LeetCode 423. Reconstruct Original Digits from English
解题思路:主要是观察0-9这10 个英文字母的出现规律,先找到每个字符唯一标识的数字,然后找其他出现的字符代表的数字减去唯一标识的字符出现的数字. class Solution { public: string originalDigits(string s) { vector low_letter(128,0); vector digit(10,0);原创 2017-01-23 13:36:44 · 198 阅读 · 0 评论 -
LeetCode Integer to Roman
解题思路:首先要知道罗马数字是怎么去表示整数的,这里在维基百科上可以搜到,之后就直接枚举case就行了. class Solution { public: string intToRoman(int num) { string res; int flag[4]; flag[3] = num/1000; num原创 2017-01-23 14:30:43 · 162 阅读 · 0 评论 -
LeetCode 263. Ugly Number
解题思路:类似质因数分解,但是因数只能为2,3,5,所以用m去整除数字不能超过6,之前写了一个m class Solution { public: bool isUgly(int num) { if(num == 1) return true; if(num <= 0) return false;原创 2017-01-23 16:33:21 · 174 阅读 · 0 评论 -
LeetCode 231. Power of Two
解题思路: 这题很简单,直接用2除即可,注意n = 0 的情况 class Solution { public: bool isPowerOfTwo(int n) { while(n%2 == 0 && n != 0){ n /= 2; } return n==1; } };原创 2017-01-23 16:43:03 · 183 阅读 · 0 评论 -
LeetCode 112. Path Sum
解题思路:递归即可 class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root == NULL) return false; if(root->val == sum && root->left == NULL && root->right == NU原创 2017-01-24 10:41:16 · 222 阅读 · 0 评论 -
LeetCode 111. Minimum Depth of Binary Tree
解题思路:DFS遍历整棵树,取最小深度 class Solution { public: int minDepth(TreeNode* root) { if(root == NULL) return 0; if(root->left == NULL) return 1+minDepth(root->right);原创 2017-01-24 15:15:00 · 211 阅读 · 0 评论 -
Leetcode 537. Complex Number Multiplication
解题思路:主要是对字符分割 class Solution { public: string complexNumberMultiply(string a, string b) { pair av = parse(a); pair bv = parse(b); int ra = av.first * bv.first - av.second原创 2017-04-05 10:36:35 · 361 阅读 · 0 评论