leetcode
nedwons
这人很懒,啥也不懂。
展开
-
leetcode: single number
1.[2,2,1,7,6,9,0,4,4,0,9,6,1]思路:1.1 假设a[0]就是dingle number,a[0]与a[1]之后的所有元素比较,直到最后一个元素。1.2 如果中途a[0]等于其他元素,跳转出来,a[1]与后面的元素比,直到最后一个元素。重复扫完所有元素。瞬间方了,,,写的啥玩意儿#include <iostream>#i...原创 2019-04-21 21:19:29 · 118 阅读 · 0 评论 -
leetcode:217. Contains Duplicate
ac了,这个比较简单,1.先排序,2.然后 从头扫起,遇到两个相等的说明有重复/*1.数组完全映射过去,2.扫一遍,如果元素地址内的值大于1则返回true,否则返回false*/class Solution {public: bool containsDuplicate(vector<int>& nums) { if(nums....原创 2019-05-07 15:57:27 · 114 阅读 · 0 评论 -
leetcode:70. Climbing Stairs
sorry ,giving up..想了下:/*思路整理:1 和 2步2=1+1想考察什么? 排列组合,分治奇数偶数?前缀树?C21 */class Solution {public: int climbStairs(int n) { int count=0; }};想的不太清楚,...转载 2019-05-20 15:52:30 · 164 阅读 · 0 评论 -
leetcode:1. Two Sum
如何优化呢:没考虑负数,错误,,,/*1.寻找加起来等于某个数的两个数,返回其小标*/class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { // sort(nums.begin(),nums.end); ...原创 2019-05-20 16:51:37 · 135 阅读 · 0 评论 -
leetcode:53. Maximum Subarray
53. Maximum Subarray又是放弃治疗,直接学习大佬的写法吧。lucastan1150Last Edit: September 30, 2018 11:11 AM42.2K VIEWSIdea is very simple. Basically, keep adding each integer to the sequence until th...转载 2019-05-20 17:36:22 · 140 阅读 · 0 评论 -
leetcode:171. Excel Sheet Column Number
就是26进制数转化成10进制醉了,熬夜无法思考:想用map帮忙,,,没会写,,,,class Solution {public: int titleToNumber(string s) { int len=s.length()-1,sum=0; map <char,int> covert_map; for(i...原创 2019-05-08 01:55:17 · 102 阅读 · 0 评论 -
leetcode:679. 24 Game
mgj 看到在lc上是hard类,我就算找了点借口安慰自己,,,然而,面那边要求不许用double我也是醉了。怎么写?jasonshieh208Last Edit: October 2, 2018 11:07 AM2.0K VIEWSSearch for all possible cases.Looks like backtracking......转载 2019-05-24 21:03:31 · 110 阅读 · 0 评论 -
leetcode:21. Merge Two Sorted Lists
Mgj又不会了,比较直接的方法是,先拼接,后排序:写的又复杂又乱,,而且没写出来,赶紧学大佬怎么写的吧,,,/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), n...原创 2019-05-16 16:41:13 · 86 阅读 · 0 评论 -
leetcode:118. Pascal's Triangle
118. Pascal's Triangle放弃治疗,,,直接看大佬答案,class Solution {public: vector<vector<int> > generate(int numRows) { vector<vector<int>> r(numRows); for (i...转载 2019-05-16 17:38:10 · 84 阅读 · 0 评论 -
leetcode:202. Happy Number
又是放弃治疗的题,,,肯定有别的简单的方法,菜,,,已经懒得思考了class Solution {public: bool isHappy(int n) { if(n==1) return true; int sum=0; int a=n%10; int b=n/10; sum=pow(a,2)+p...原创 2019-05-16 21:07:43 · 134 阅读 · 0 评论 -
leetcode:371. Sum of Two Integers
条件为什么是:b!=0https://blog.csdn.net/liyuanbhu/article/details/51803974https://blog.csdn.net/MebiuW/article/details/51788817https://blog.csdn.net/u014248127/article/details/530570872,思...转载 2019-05-12 21:50:42 · 90 阅读 · 0 评论 -
leetcode:108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree玩完了,不会,,,大佬的答案,class Solution { TreeNode* sortedArrayToBST(vector<int>& nums, int start, int end){ if(end<=start) r...转载 2019-05-12 23:08:26 · 101 阅读 · 0 评论 -
leetcode:121. Best Time to Buy and Sell Stock
弱智的csdn规则,,,一天限制发10篇,简直有病吧,,,搬其他博客算了。。。只需要买卖各一次,审题不认真。直接写错了,抄了下大佬的class Solution {public: int maxProfit(vector<int>& prices) { int maxPro=0,len=prices.size(),minPri...原创 2019-05-15 08:49:44 · 79 阅读 · 0 评论 -
leetcode:350. Intersection of Two Arrays II
350. Intersection of Two Arrays II不会做!!没想法,小的set?1.需要记住顺序,2.长短不固定,假设短的是长的子集?直接拼接成字符串?大佬答案:class Solution {public: vector<int> intersect(vector<int>& nums1,...转载 2019-05-14 14:34:11 · 83 阅读 · 0 评论 -
bug:越界访问导致ERROR: AddressSanitizer: heap-buffer-overflow on address
==30==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000000054 at pc 0x00000040d6e7 bp 0x7fffd87fd2f0 sp 0x7fffd87fd2e8READ of越界访问了,注意数组等的边界,len-1==================...原创 2019-04-22 20:35:45 · 6047 阅读 · 0 评论 -
leetcode:237. Delete Node in a Linked List
删节点,题目给的条件和给的类的参数不太明白。。。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class So...原创 2019-05-01 17:25:39 · 110 阅读 · 0 评论 -
leetcode:169. Majority Element
结果貌似正确,但提交heap overflow 了,没被ac,数组老犯这种错误。/*思路:1.找出两个不同的元素的值存起来,用标记区分2.默认tmp[0]的个数最多,*/class Solution {public: int majorityElement(vector<int>& nums) { vector<int...原创 2019-05-01 20:16:30 · 115 阅读 · 0 评论 -
leetcode:13. Roman to Integer
这题没想清楚,,,好好穷举啊,我的哥,总能枚举完比较蠢的写法,再说/* 顺序,从后往前,I V X L C D M 如果逆序就减去, 后一位小于等于它本身就是+,否则是- 要用栈? */class Solution {public: int romanToInt(string s) { int len=s.length(); int s...原创 2019-05-03 17:59:21 · 78 阅读 · 0 评论 -
leetcode:242. Valid Anagram
想法太天真了点:错误,容我想想,class Solution {public: bool isAnagram(string s, string t) { int len=s.length(); int len_t=t.length(); if(len!=len_t)return false; int r...原创 2019-05-03 20:23:54 · 99 阅读 · 0 评论 -
leetcode:412. Fizz Buzz
思路整理:3的倍数 Fizz5的倍数 Buzz3&&5 则FizzBuzzn%3==0n%5==0&&注意并列,提高整合,不要写多余的代码,看人家的写的多好。抄别人的,class Solution {public: vector<string> fizzBuzz(int n) { ...原创 2019-04-24 10:40:51 · 123 阅读 · 0 评论 -
leetcode:283. Move Zeroes
思路整理:Input: [0,1,0,3,12]Output: [1,3,12,0,0]怎么想到的:直接删除最好。1.遇到0直接删除,统计遇到0的个数,扫到最后。2.对处理过后的进行添加0,vector,/*思路:扫到当前的元素,要么是0要么不是,是则删除。最后再添加回来就行了*/class Solution {public: void moveZe...原创 2019-04-24 11:35:27 · 98 阅读 · 0 评论 -
leetcode: Reverse Linked List
我去,单链表的逆转都没搞定,,,醉了写的啥破玩意儿,,, 还是写不出来。能出结果,但不知道哪里错了,没被ac, 太low了。low写法:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(in...原创 2019-04-24 17:41:39 · 122 阅读 · 0 评论 -
leetcode:122. Best Time to Buy and Sell Stock II
最佳买卖点:写的啥,太复杂了/*thinking:1.get the smaller num to buy, 2.assume cur num to buy if next is smaller ,not buy,3.when cur<before, cur<next buy,================for each num:buy or no...原创 2019-05-05 20:18:57 · 93 阅读 · 0 评论 -
leetcode:387. First Unique Character in a String
不要慌,搞懂题目先,思路:unordered_map 映射好,然后判断:不过好像比大家的慢了好多,为什么呢?有什么更快的呢class Solution {public: int firstUniqChar(string s) { int len=s.length(); unordered_map<int,int> mp;...原创 2019-05-14 09:44:50 · 93 阅读 · 0 评论 -
leetcode:268. Missing Number
思路:1.先加进map,2.第二遍循环缺的class Solution {public: int missingNumber(vector<int>& nums) { int len=nums.size(); if(len==0) return 0; map<int...原创 2019-05-14 13:14:59 · 112 阅读 · 0 评论 -
leetcode:101. Symmetric Tree
敢不敢把问题理清楚点???懒,写的啥破玩意儿,,,,回来继续写/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NU...原创 2019-05-23 12:03:15 · 125 阅读 · 0 评论