自定义博客皮肤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

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

原创 LeetCode 21. Merge Two Sorted Lists

描述合并两个有序链表解决利用一个临时表头,然后同时遍历这两个链表,连接节点即可。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ cl

2016-09-19 13:44:02 368

原创 LeetCode 100. Same 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-09-18 23:39:56 231

原创 LeetCode 11. Container With Most Water

描述给出一些点,过点作垂线,找出两条线,使得与x轴围成的面积最大。解决遍历比较即可class Solution { public: int maxArea(vector<int>& height) { int i = 0, j = height.size() - 1, maxVal = 0; int tmp; while (i != j){

2016-09-15 23:12:37 264

原创 LeetCode 397. Integer Replacement

描述问一个数经过多少次的变换才会变成1解决利用递归class Solution { public: int integerReplacement(int n) { if (n == INT_MAX){ return 32; } if (n == 1){ return 0; }

2016-09-14 16:11:03 644

原创 LeetCode 136. Single Number

描述找出数组中只出现一次的数解决利用^运算的特点即可找出class Solution { public: int singleNumber(vector<int>& nums) { int res = 0; for (auto& it : nums){ res ^= it; } return res;

2016-09-14 16:08:55 276

原创 LeetCode 389. Find the Difference

描述判断字符串不同的字符是哪个解决遍历直接class Solution { public: char findTheDifference(string s, string t) { int arr[27] = {0}; for (auto& iVal : s){ ++arr[iVal - 'a']; }

2016-09-13 20:57:51 230

原创 LeetCode 383. Ransom Note

描述判断序列ransomNote可否由magazine中的字符构成解决记录ransomNote的字符出现次数,再遍历magazine中的字符,最后再一次遍历判断。class Solution { public: bool canConstruct(string ransomNote, string magazine) { int arr[27] = {0};

2016-09-13 20:18:39 256

原创 LeetCode 387. First Unique Character in a String

描述找出字符串中第一个只出现过一次的字符解决利用数组存每个字符出现的次数,然后遍历判断class Solution { public: int firstUniqChar(string s) { int arr[27] = {0}; int lenth = s.size(); for (int i = 0; i < lenth; ++i){

2016-09-13 18:15:04 256

原创 LeetCode 392. Is Subsequence

描述判断字符串s是否是t的子串解决遍历判断class Solution { public: bool isSubsequence(string s, string t) { int lenthS = s.size(); int lenthT = t.size(); int i = 0, j = 0; while (i < len

2016-09-12 21:31:10 305

原创 LeetCode 231. Power of Two

描述判断一个数是否是2的幂解决把一个数思考成2进制的存储形式,那么n & (n - 1) == 0,当n为2的幂的时候class Solution { public: bool isPowerOfTwo(int n) { if (n <= 0) return false; return !(n & (n - 1)); } };

2016-09-12 21:20:27 188

原创 LeetCode 258. Add Digits

描述给出一个数,按照规则,求出最后只有1位数时的答案解决利用结论,当num == 0, res = 0; num %9 ==0 , res = 9,其它情况下, res = num % 9.class Solution { public: int addDigits(int num) { if (num == 0) return 0;

2016-09-12 21:12:21 220

原创 LeetCode 70. Climbing Stairs

描述走梯子解决最简单的动态规划问题了class Solution { public: int climbStairs(int n) { vector<int> arr(n + 1); arr[1] = 1; arr[2] = 2; for (int i = 3; i <= n; ++i){ arr[i]

2016-09-12 13:54:40 256

原创 LeetCode 67. Add Binary

描述string类型的二进制数的加法解决模拟class Solution { public: string addBinary(string a, string b){ int posA = a.size(); int posB = b.size(); //string res = ""; int minLenth = (pos

2016-09-11 23:19:39 227

原创 LeetCode 66. Plus One

描述对一个vector数组加1.解决模拟class Solution { public: vector<int> plusOne(vector<int>& digits) { int lenth = digits.size(); vector<int> tmp(lenth + 1, 0); digits[lenth - 1] += 1; f

2016-09-11 21:58:19 260

原创 LeetCode 58. Length of Last Word

描述给出字符串最后一个单词的长度解决从后向前扫描,扫描时遇到第一个字符开始计算。class Solution { public: int lengthOfLastWord(string s) { int pos = s.size() - 1; int cnt = 0; while (pos >= 0 && s[pos] == ' ')

2016-09-10 23:26:43 259

原创 LeetCode 83. Remove Duplicates from Sorted List

描述给出一条链表,删除重复的节点解决直接遍历/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { pub

2016-09-10 23:10:18 254

原创 LeetCode 24. Swap Nodes in Pairs

描述给出一条链表,交换节点。解决利用三个节点prev, now, post进行节点的交换。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * };

2016-09-10 22:47:56 283

原创 LeetCode 27. Remove Element

描述给出一个数组,移除数组懂值的大小等于val的值解决这种题目在leetcode中出现很多类似的,方法都一样。用辅助变量来标记位置即可。class Solution { public: int removeElement(vector<int>& nums, int val) { int lenth = nums.size(); int i = 0, j =

2016-09-10 14:33:35 245

原创 LeetCode 20. Valid Parentheses

描述给出一组字符串,判断是否符合规则解决利用stack就解决了。class Solution { public: bool isValid(string s) { int lenth = s.size(); map<char, char> m; m.insert(make_pair('{', '}')); m.insert(ma

2016-09-10 14:07:45 223

原创 LeetCode 19. Remove Nth Node From End of List

描述给出一条链表,删除从右往左数的第n个节点解决因为不知懂啊ListNode是如何构成的,所以不考虑删除内存,只是修改了指针。先遍历一遍统计节点个数,然后再利用辅助指针来移动扫描节点的位置。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; *

2016-09-09 21:26:36 251

原创 LeetCode 38. Count and Say

描述按照题目所描述的规则,给出第n次生成的字符串解决第一次字符串为: 1 第二次字符串为: 11 第三次字符串为: 2 1 第四次字符串为: 1211 第五次字符串为: 111221 第六次字符串为: 312211 这道题目主要就是找规律,统计前一次字符串的出现数目,从而得到下一次的字符串。格式为 数目 + 字符,从左往右统计,遇到相同的则递增,不同的则从0开始计数。class Solu

2016-09-08 16:01:28 245

空空如也

空空如也

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

TA关注的人

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