自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

菜鸟

too difficult

  • 博客(41)
  • 收藏
  • 关注

原创 LeetCode 204. Count Primes

描述找素数个数解决打表class Solution {public:int countPrimes(int n) { if (n <= 2) return 0; vector<int> arr(n + 1, 0); for (int i = 2; i <= sqrt(n); ++i) { if (!arr[i])

2016-10-31 10:54:58 239

原创 LeetCode 202. Happy Number

描述给出一个数,判定该数是否是Happy Number,给出了定义。解决可以发现,若不是Happy Number,会出现重复出现的情况。class Solution {public: bool isHappy(int n) { map<int, int> m; bool flag = false; if (n == 1)

2016-10-31 09:57:34 276

原创 LeetCode 195. Tenth Line (shell)

描述打印文件的第十行内容解决迭代# Read from the file file.txt and output the tenth line to stdoutcount=1cat file.txt | while read linedo if [ $count -eq 10 ] then echo "$line" break else

2016-10-30 15:08:48 530

原创 LeetCode 235. Lowest Common Ancestor of a Binary Search Tree

描述BST中找最近公共父节点解决递归搜索/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL

2016-10-30 13:46:19 235

原创 LeetCode 232. Implement Queue using Stacks

描述用栈来实现队列解决用两个栈模拟即可class Queue {public: // Push element x to the back of queue. void push(int x) { s1.push(x); } // Removes the element from in front of queue. void pop(void

2016-10-30 12:42:10 216

原创 LeetCode 206. Reverse Linked List

描述反转链表解决/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListN

2016-10-29 11:05:48 190

原创 LeetCode 141. Linked List Cycle

描述判断链表是否有环路解决两个指针,走一步和两步,假设走了n次,回路的长度为l, 那么若有环路,会有: 2 * n % l == n % l/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(

2016-10-29 10:53:14 190

原创 LeetCode 111. Minimum Depth of Binary Tree

描述求二叉树的最小深度,最近的叶子节点到根节点的距离解决左右递归遍历/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NUL

2016-10-29 10:19:00 208

原创 LeetCode 118. Pascal's Triangle

描述按照题目规则,输出三角形解决class Solution {public: vector<vector<int>> generate(int numRows) { vector<vector<int>> res; for (int i = 1; i <= numRows; ++i) { vector<int> tm

2016-10-29 10:04:03 217

原创 LeetCode 107. Binary Tree Level Order Traversal II

描述层序遍历树,倒着输出解决/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {}

2016-10-27 14:31:31 251

原创 LeetCode 343. Integer Break

描述给出一个数,分解该数,求能获得最大的乘积值解决数学题class Solution {public: int integerBreak(int n) { if (n < 3) return 1; else if (n == 3) return 2; else if (n % 3 == 0)

2016-10-27 13:42:52 220

原创 LeetCode 260. Single Number III

描述找出数组中只出现过一次的数解决用异或。class Solution {public: vector<int> singleNumber(vector<int>& nums) { int t = 0; for (auto& val : nums) { t ^= val; } int t

2016-10-26 16:50:34 237

原创 LeetCode 169. Majority Element

描述找出出现次数大于n / 2 的数解决class Solution {public: int majorityElement(vector<int>& nums) { int lenth = nums.size(); unordered_map<int, int> cnt; for (int i = 0; i < lenth; ++i){

2016-10-26 16:01:00 220

原创 LeetCode 191. Number of 1 Bits

描述求二进制1的数量解决class Solution {public: int hammingWeight(uint32_t n) { return __builtin_popcount(n); }};

2016-10-26 15:52:09 254

原创 LeetCode 167. Two Sum II - Input array is sorted

描述给出一个有序数组,求是否存在两个数的和等于目标值解决遍历。class Solution {public: vector<int> twoSum(vector<int>& numbers, int target) { int l = 0, r = numbers.size() - 1; while (l < r) {

2016-10-25 19:00:02 260

原创 LeetCode 108. Convert Sorted Array to Binary Search Tree

描述给出一个升序数组,构造BST解决利用递归,每次取数组的中间值/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL)

2016-10-25 18:34:53 206

原创 LeetCode 319. Bulb Switcher

描述给出一个组灯,按照规则开灯关灯后,有多少盏灯还亮着解决纸上写出来后,发现其中关灯的数量构成了2,4,6,8,10……的等差数列。用等差数列求和,然后比较判断即可。class Solution {public: int bulbSwitch(int n) { int t = 1; int sum = 0; do{ s

2016-10-24 17:22:31 224

原创 LeetCode 318. Maximum Product of Word Lengths

描述求一对字符串,使得这两个字符串的长度之积最大,要求这两个字符串中无相同元素。解决二进制存下每个字符的值,然后比较class Solution {public: int maxProduct(vector<string>& words) { int length = words.size(); vector<int> flag(length, 0);

2016-10-24 16:30:11 310

原创 hihocoder 1032 最长回文子串

描述找出字符串中的最长回文字串解决#include <iostream>#include <string>#include <cstring>using namespace std;int manacher(string str);int cnt[2000005];int main(){ int t; cin >> t; while (t--) {

2016-10-23 23:15:22 232

原创 hihocoder 1501 补提交卡

描述给出一个数组,求两个数之间最大的差距。解决贪心遍历#include <iostream>#include <vector>using namespace std;int main(){ int n; cin >> n; while (n--) { int N, M; cin >> N >> M; vector<i

2016-10-23 21:15:41 280

原创 LeetCode 328. Odd Even Linked List

描述 给出一个链表,把索引为基数的节点排在索引为偶数的节点前。解决指针的变换而已。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */

2016-10-21 23:33:56 216

原创 LeetCode 409. Longest Palindrome

描述 求给出字符串所能构成的最长回文串解决利用hash表存对应字符的数量,然后分别统计偶数与奇数的个数class Solution {public: int longestPalindrome(string s) { unordered_map<char, int> m; int length = s.size(); for (int i

2016-10-21 09:44:11 257

原创 LeetCode 5. Longest Palindromic Substring

描述求出最长的回文字符串解决利用manacher算法class Solution {public: string longestPalindrome(string s) { string str = "$#"; int lenth = s.size(); for (int i = 0; i != lenth; ++i){

2016-10-21 09:12:56 211

原创 LeetCode 104. Maximum Depth of Binary Tree

描述求树的深度解决递归。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} *

2016-10-21 09:07:06 174

原创 LeetCode 412. Fizz Buzz

描述按照规则得到一个字符串数组解决用一下to_string().class Solution {public: vector<string> fizzBuzz(int n) { vector<string> res; for (int i = 1; i <= n; ++i) { if (i % 3 == 0 && i

2016-10-20 22:45:39 508

原创 LeetCode 415. Add Strings

描述给出两个字符串表示的大整数,求和解决模拟题,注意进位即可。class Solution {public: string addStrings(string num1, string num2) { int i1 = num1.size() - 1; int i2 = num2.size() - 1; string res;

2016-10-20 22:32:40 231

原创 LeetCode 406. Queue Reconstruction by Height

描述给出一个向量,按要求重建这个向量解决先排序,然后再插入list容器中。class Solution {public: vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) { sort(people.begin(), people.end(), comp); lis

2016-10-19 15:25:34 377

原创 LeetCode 404. Sum of Left Leaves

描述给出一棵二叉树,求所有左叶子节点的数值和解决就递归,找到左叶子节点就加起来。注意不要sf了。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : va

2016-10-19 13:22:13 215

原创 LeetCode 382. Linked List Random Node

描述实现链表成员函数,该函数使得获取每一个节点值的概率相等。解决 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Sol

2016-10-19 13:01:42 282

原创 LeetCode 102. Binary Tree Level Order Traversal

描述层序遍历二叉树解决利用队列存每一层的节点即可。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right

2016-10-18 19:24:27 217

原创 LeetCode 101. Symmetric Tree

描述判断一棵树是否对称解决利用递归遍历该树,因为需要判断对称性,所以需要传入两个节点参数。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x

2016-10-18 18:41:50 236

原创 LeetCode 144. Binary Tree Preorder Traversal

描述给出一棵二叉树,前序遍历该树解决利用栈来进行前序遍历,即可实现迭代/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NU

2016-10-17 13:59:51 211

原创 LeetCode 73. Set Matrix Zeroes

描述给出一个矩阵,当其中一个坐标值为0时,令该坐标所在行与列的值都设置为0解决利用矩阵所现有的空间,用前面的空间记录所要进行变换的行与列class Solution {public: void setZeroes(vector<vector<int>>& matrix) { int rowLength = matrix.size(); int colLeng

2016-10-17 13:50:01 205

原创 LeetCode 94. Binary Tree Inorder Traversal

描述给出一棵二叉树,求中序遍历结果解决一种方法是递归,另一种是利用栈进行迭代//迭代版本/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x

2016-10-16 20:38:19 243

原创 LeetCode 46. Permutations

描述求出一个数组的所有排列解决利用std::next_permutation()class Solution {public: vector<vector<int>> permute(vector<int>& nums) { sort(nums.begin(), nums.end()); vector<vector<int>> res; do

2016-10-16 20:07:03 215

原创 LeetCode 237. Delete Node in a Linked List

描述删除链表中的一个节点解决参数是指向链表的一个指针。如果node = node -> next,改变的只是该指针的值,而没有实际改变链表中的节点, *node = *(node -> next), 才是正确的。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode

2016-10-15 13:05:18 199

原创 LeetCode 226. Invert Binary Tree

描述对称调换二叉树解决考虑到二叉树的遍历与构造通常都是由递归实现的,在递归的过程中实现指针的改变即可。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) :

2016-10-15 12:26:51 178

原创 LeetCode 225. Implement Stack using Queues

描述用队列来实现栈解决最简单的是用deque实现了,用一个queue也可以实现。class Stack {public: deque<int> que; // Push element x onto stack. void push(int x) { return que.push_back(x); } // Removes the eleme

2016-10-14 13:24:44 367

原创 LeetCode 219. Contains Duplicate II

描述给出一个数组,判断是否存在距离不超过k,值相等的两个数。解决利用unordered_map<>记录下数的位置,然后判断即可。class Solution {public: bool containsNearbyDuplicate(vector<int>& nums, int k) { unordered_map<int, int> t; int lent

2016-10-13 22:40:03 227

原创 LeetCode 264. Ugly Number II

描述求第n个丑数解决每次用最小的数去更新下一个数class Solution {public: int nthUglyNumber(int num) { std::vector<int> arr(num + 1); arr[0] = 1; int t2 = 0, t3 = 0, t5 = 0; for (int i = 1;

2016-10-13 22:06:52 273

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除