摘要:problem 349. Intersection of Two Arrays 题意,是求解两个数组的交集。 solution 参考 1. Leetcode_349. Intersection of Two Arrays; 完
阅读全文
摘要:problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { int left = 0, right =s.size()-1; char chl, chr; whil
阅读全文
摘要:problem 344. Reverse String solution: solution2: 参考 1. Leetcode_344_Reverse String; 完
阅读全文
摘要:problem 342. Power of Four solution1:loop; solution ref: 1. Leetcode_342_Power of Four; 2. GrandYang; end
阅读全文
摘要:problem 326. Power of Three solution: Iteration ref 1. Leetcode_326_Power of Three; 2. GrangYang; 3. A summary of `all` solutions; end
阅读全文
摘要:problem 303. Range Sum Query - Immutable solution 注意,累加和的下标,因为累加和的第一项是初始化值0. 参考 1. Leetcode_303_Range Sum Query - Immutable; 完
阅读全文
摘要:problem 292. Nim Game solution 来generalize一下这道题,当可以拿1~n个石子时,那么个数为(n+1)的整数倍时一定会输,我们试着证明一下这个结论,若当前共有m*(n+1)个石子,那么: 当m=1时,即剩n+1个的时候,肯定会输,因为不管你取1~n中的任何一个数
阅读全文
摘要:problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致。疑惑!知道的可以分享一下下哈~ 之前理解有误,应该是pattern中的每个字符和某个单词的一一映射关系。 参考 1. Leetcode_290_Wor
阅读全文
摘要:problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可。 参考 1. Leetcode_283_Move Zeroes; 完
阅读全文
摘要:problem 278. First Bad Version solution1:遍历; solution2:二分法; 参考 1. Leetcode_278_First Bad Version; 完
阅读全文
摘要:problem 268. Missing Number solution1:等差数列求和公式 根据题意,(0, 1, 2,...,n),共有(n+1)个数,丢失了一个数之后,剩余的数组成的数组,求解丢失的数据。 等差数列求和减去数组累加和得到的结果即为所求数据。 solution2:异或操作。 so
阅读全文
摘要:problem 263. Ugly Number 丑数 所谓丑数就是其质数因子只能是2,3,5。那么最直接的办法就是不停的除以这些质数,如果剩余的数字是1的话就是丑数了。 solution1 solution2 参考 1. Leetcode_263_Ugly Number; 完
阅读全文
摘要:problem 258. Add Digits solution1: solution2: Digital_root 参考 1. Leetcode_258. Add Digits; 2. Digital_root; 完
阅读全文
摘要:problem 257. Binary Tree Paths solution1:recursive递归方法。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * Tr
阅读全文
摘要:problem 242. Valid Anagram 首先,要先弄清楚什么是anagram,anagram是指由颠倒字母顺序组成的单词。 解决方法一种是排序,一种是哈希表。 solution: 参考 1. Leetcode_242_Valid Anagram; 完
阅读全文
摘要:problem 237. Delete Node in a Linked List 这道题是删除链表的一个节点,和通常情况不同的是,没有给出链表的起点,只给了一个要删的节点。一般来说删除一个节点的方法是要有其前一个节点的位置,然后将其前一个节点的next连向要删节点的下一个,然后delete掉要删的
阅读全文
摘要:problem 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的性质 百科解释 solution1:递归方法: 由于二叉搜索树的特点是左<根<右,所以根节点的值一直都是中间值,大于左子树的所有节点值,小于右子树的所有节点值,那么我们
阅读全文
摘要:234. Palindrome Linked List 1. 使用快慢指针找中点的原理是fast和slow两个指针,每次快指针走两步,慢指针走一步,等快指针走完时,慢指针的位置就是中点。如果是偶数个数,正好是一半一半,如果是奇数个数,慢指针正好在中间位置,判断回文的时候不需要比较该位置数据。 注意好
阅读全文
摘要:problem 232. Implement Queue using Stacks 参考 1. Leetcode_ 232_Implement Queue using Stacks; 完
阅读全文
摘要:problem 231-power-of-two solution1 class Solution { public: bool isPowerOfTwo(int n) { if(n==0) return false; while(n%2==0) { n /= 2; } return n==1; }
阅读全文