LeetCode AC代码
daixie544048040
这个作者很懒,什么都没留下…
展开
-
728. Self Dividing Numbers
class Solution {public: vector<int> selfDividingNumbers(int left, int right) { vector<int>a; for (int i = left; i <= right; ++i) { int k = i; ...原创 2018-08-30 19:20:34 · 152 阅读 · 0 评论 -
867. Transpose Matrix
class Solution {public: vector<vector<int>> transpose(vector<vector<int>>& A) { int n = A[0].size(); vector<vector<int>>B; for (int i = 0; i &l原创 2018-08-31 01:05:11 · 136 阅读 · 0 评论 -
876. Middle of the Linked List
class Solution {public: ListNode* middleNode(ListNode* head) { ListNode *p = head; int n = 0; while (p != NULL) { p = p->next; ++n; } ...原创 2018-08-31 01:13:01 · 100 阅读 · 0 评论 -
806. Number of Lines To Write String
class Solution {public: vector<int> numberOfLines(vector<int>& widths, string S) { vector<int>A; int sum = 0; int Cnt = 1; for (int i = 0; i ...原创 2018-08-31 10:55:20 · 127 阅读 · 0 评论 -
872. Leaf-Similar Trees
class Solution {public: vector<int>A; void vis(TreeNode *t) { if (t == NULL)return; if (t->left == NULL&&t->right == NULL)A.push_back(t->val); v...原创 2018-08-31 11:08:14 · 132 阅读 · 0 评论 -
557. Reverse Words in a String III
class Solution {public: string reverseWords(string s) { istringstream temp(s); string text; string fin; bool flag = true; while (temp >> text) { ...原创 2018-08-31 11:23:25 · 131 阅读 · 0 评论 -
617. Merge Two Binary Trees
class Solution {public: int k = 0; bool vis(TreeNode* t1, TreeNode* t2) { if (k == 0 && t1 == NULL) { return 2; } ...原创 2018-08-24 15:12:28 · 216 阅读 · 0 评论 -
657. Judge Route Circle
class Solution {public: bool judgeCircle(string moves) { int UCnt = 0, DCnt = 0, LCnt = 0, RCnt = 0; for (int i = 0; i < moves.length(); ++i) { switch (moves[...原创 2018-08-24 15:16:01 · 144 阅读 · 0 评论 -
832. Flipping an Image
cpp vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) { for (int i = 0; i < A.size(); ++i) { for (int j = 0; j < (A...原创 2018-08-24 15:17:26 · 131 阅读 · 0 评论 -
461. Hamming Distance
int hammingDistance(int x, int y) { string str1 = ""; string str2 = ""; while (x > 0) { int temp = x % 2; x /= 2; str...原创 2018-08-24 15:19:00 · 131 阅读 · 0 评论 -
804 Unique Morse Code Words
int uniqueMorseRepresentations(vector<string>& words) { string a[26] = { ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---原创 2018-08-24 15:19:26 · 136 阅读 · 0 评论 -
709 To Lower Case
string toLowerCase(string str) { string temp = ""; for (int i = 0; i < str.size(); ++i) { if (str[i] >= 65 && str[i] <= 90)str[i] += 32; temp += str[i]; }...原创 2018-08-24 15:20:08 · 164 阅读 · 0 评论 -
771 Jewels and Stones
int numJewelsInStones(string J, string S) { int Cnt = 0; for (int i = 0; i < J.size(); ++i) { for (int l = 0; l < S.size(); ++l) { if (J[i] == S...原创 2018-08-24 15:20:42 · 124 阅读 · 0 评论 -
852. Peak Index in a Mountain Array
class Solution {public: int peakIndexInMountainArray(vector<int>& A) { if (A.size() < 3||A[0]>A[1])return 0; bool flag = false; int ...原创 2018-08-24 15:38:56 · 101 阅读 · 0 评论 -
595. Big Countries
Write your MySQL query statement belowselect World.name,World.population,World.area from World where World.area>=3000000 or World.population>=25000000;原创 2018-08-24 15:39:58 · 137 阅读 · 0 评论 -
627. Swap Salary
Write your MySQL query statement belowupdate salary set sex=IF(sex=’m’,’f’, ‘m’);原创 2018-08-24 16:06:27 · 143 阅读 · 0 评论 -
8. String to Integer (atoi)
同样是我最喜欢最简单的遍历。。int myAtoi(string str) { int i = 0; while (str[i] == ' ')++i; if ((str[i] < 48 || str[i]>57)&&str[i]!='-'&&str[i]!='+')return 0; ...原创 2018-09-04 13:28:48 · 141 阅读 · 0 评论 -
9. Palindrome Number
又是我最喜欢的to_string 我这么做题是不是不太好class Solution {public: bool isPalindrome(int x) { if (x < 0)return false; string s = to_string(x); for (int i = 0; i < s.size()/2; ...原创 2018-09-04 13:34:02 · 97 阅读 · 0 评论 -
883. Projection Area of 3D Shapes
class Solution {public: int projectionArea(vector<vector<int>>& grid) { int a[1000]; memset(a, 0, sizeof(a)); int sum = 0; int howlong = 0; ...原创 2018-08-30 00:30:16 · 218 阅读 · 0 评论 -
476. Number Complement
class Solution {public: int findComplement(int num) { int temp = num; int m = 1; while (temp != 0) { temp /= 2; m *= 2; } int ans =...原创 2018-09-01 23:15:30 · 96 阅读 · 0 评论 -
344. Reverse String
class Solution {public: string reverseString(string s) { int n = s.length() - 1; for (int i = 0; i <= n / 2; ++i) { char temp = s[i]; s[i] = s[n - i]; ...原创 2018-09-01 23:37:18 · 101 阅读 · 0 评论 -
561. Array Partition I
“`cpp class Solution { public: int arrayPairSum(vector& nums) { sort(nums.begin(), nums.end()); int sum = 0; for (int i = 0; i < nums.size(); i += 2)sum += num...原创 2018-08-30 11:06:47 · 115 阅读 · 0 评论 -
3. Longest Substring Without Repeating Characters
我想到的是用数组位置的值代替字符串的重复遍历class Solution {public: int lengthOfLongestSubstring(string s) { int a[1000]; int ans = 0; for (int i = 0; i < s.size(); ++i) { mems...原创 2018-09-02 13:15:36 · 86 阅读 · 0 评论 -
4. Median of Two Sorted Arrays
这道题我写的很烂。。应该是最笨的方法吧 主要思想就是用指针按大小走 因为两个vector对象都是排好序的嘛 要是两个又扔一起重新排序就很蠢了,然后就是如果一个走到头了,就把它取最大的数 保证他取不到就行了double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { ...原创 2018-09-02 16:05:25 · 85 阅读 · 0 评论 -
5. Longest Palindromic Substring
先上自己的代码 同样没什么可学习性 三重循环。。注意substr函数截取的是从某一位开始后的几位而不是某一位开始后从某一位结束class Solution {public: string longestPalindrome(string s) { string ans = ""; bool flag = true; for (int...原创 2018-09-02 19:22:41 · 104 阅读 · 0 评论 -
15. 3Sum
首先不可能用暴力破解(n^3),那么想到two sum的另一种解法且复杂度只有(nlogn),先排序,双指针,分别指向首尾,和target做比较来移动前指针和后指针 而three sum则同样先排序 固定每一个出现的值当做two sum中的target(不过要取负噢)然后就可以做了,复杂度为(n^2)class Solution {public: vector<vector&...原创 2018-09-11 12:56:29 · 134 阅读 · 0 评论 -
10. Regular Expression Matching
动态规划 网上看的代码 以后学了再来写题解 现在一知半解class Solution {public: bool isMatch(string s, string p) { int m = s.size(), n = p.size(); vector<vector<bool>>dp = { m + 1,vector<...原创 2018-09-08 20:03:18 · 109 阅读 · 0 评论 -
6. ZigZag Conversion
It’s easy,just simulate!string convert(string s, int numRows) {//simulate if (numRows == 1)return s; char a[1000][1000]; memset(a, ' ', sizeof(a)); int k = numRows * ...原创 2018-09-03 17:02:17 · 104 阅读 · 0 评论 -
7. Reverse Integer
我自己最先想的是转字符串做。。 我也不知道为什么这么想 然后是VS能跑 LEETCODE上跑出来结果不一样,可能编译器不同缘故吧,先贴自己的 看了网上的恍然大悟。。int reverse(int x) { string s = to_string(x); string ans = ""; int k = 0; if (s[0] == '-') { ...原创 2018-09-03 17:40:24 · 117 阅读 · 0 评论 -
11. Container With Most Water
做这道题要想清楚一件事就是 想要求最大储水量 是两边的小边乘以底长 而假设移动长的那一条边 不管怎么移动 高还是短的那一条边 还是没有变 所以想要更大储水量一定是移动短的那一条边class Solution {public: int maxArea(vector<int>& height) { vector<int>::i...原创 2018-09-09 12:16:22 · 117 阅读 · 0 评论 -
12. Integer to Roman
模拟模拟模拟class Solution {public: string intToRoman(int num) { int Cnt = 0; string ans = ""; while (num) { ++Cnt; int temp=num % 10; num/...原创 2018-09-09 13:13:14 · 93 阅读 · 0 评论 -
13. Roman to Integer
第一个想到的就是用map,突然感觉12题是不是可以用map做得更简单int romanToInt(string s) { map<char, int>temp; temp['I'] = 1; temp['V'] = 5; temp['X'] = 10; temp['L'] = 50; temp['C'] = 100; temp...原创 2018-09-09 15:08:17 · 109 阅读 · 0 评论 -
14. Longest Common Prefix
string longestCommonPrefix(vector<string>& strs) { if (strs.empty())return ""; string ans = strs[0]; for (vector<string>::iterator it = strs.begin()+1; it != st...原创 2018-09-09 15:32:02 · 97 阅读 · 0 评论 -
16. 3Sum Closest
会做15 threesum就会做这道题 按之前同样的方法就行 非常简单class Solution {public: int threeSumClosest(vector<int>& nums, int target) { vector<int>::iterator it1, it2; sort(nums.begi...原创 2018-09-12 12:37:31 · 94 阅读 · 0 评论 -
4Sum
继续运用3sum的思路,3sum会做这道题就很简单:最内层循环同样是使用iterator首尾指向已排序的vector数列,外层则是双循环,将其转化为2sum的模式即target=target-nums[i]-nums[j]要注意的是每次push_back temp前要先sort一下temp,为了之后的unique去重vector<vector<int>> fourSu...原创 2018-10-13 14:41:29 · 136 阅读 · 0 评论 -
19 Remove Nth Node From End of List
别想花里胡哨的了。。倒置删除再倒置吧,复杂度也就n,就是代码长了点/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class S...原创 2018-10-13 15:42:17 · 129 阅读 · 0 评论 -
20 Valid Parentheses
水题,能想到stack就能做bool isValid(string s) { stack<char>scheck; for (int i = 0; i < s.size(); ++i) { if (s[i] == '(' || s[i] == '[' || s[i] == '{'){ if (s[i] == '(')scheck.push(')'); els...原创 2018-10-14 00:21:39 · 165 阅读 · 0 评论 -
21 Merge Two Sorted Lists
水题class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (l1 == NULL&&l2 == NULL)return l1; ListNode *firstNode = new ListNode(0); ListNode *p = new ListNode...原创 2018-10-14 01:15:16 · 298 阅读 · 0 评论 -
22 Generate Parentheses
递归然后剪枝 其实相当于dfs的思想 (我感觉的,因为也是一条路走到黑然后去重)思路:一开始把’(‘和’)'分列两旁 然后开始交换 '(‘只能和’)'交换,然后check检查两个情况1.重复 2.括号不正确class Solution {public: vector<string> generateParenthesis(int n) { vector<s...原创 2018-10-15 16:58:22 · 145 阅读 · 0 评论 -
23 Merge k Sorted Lists
想了两种方法,结果应该都是(n^2)我就选了一个感觉舒服一点的,定义一个指针集,然后像&lt; 21 Merge Two Sorted Lists&gt;里一样移动/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode...原创 2018-10-15 18:05:58 · 143 阅读 · 0 评论