![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
一枚想好好学习的小鬼
这个作者很懒,什么都没留下…
展开
-
leetcode167 two sum 的问题
#include<iostream>using namespace std;#include<vector>class Solution {public: vector<int> twoSum(vector<int>& numbers, int target) { int low=0; int high=n...原创 2018-11-21 09:36:11 · 80 阅读 · 0 评论 -
leetcode5 Longest Palindromic Substring
O(n^3)时间复杂度方法——暴力求解1.思想:1)从最短的子串开始,遍历所有该原字符串的子串;2)每找出一个字符串,就判断该字符串是否为回文;3)子串为回文时,则找到了最长的回文子串,因此结束;反之,则继续遍历。//采用暴力的方式进行求解class Solution {public: string isPalindrome(string s) { str...原创 2018-12-11 22:19:23 · 120 阅读 · 0 评论 -
153. Find Minimum in Rotated Sorted Array
取数组的中间位置的值:如果它比数组末尾的值大,说明最小元素就位于[mid + 1, right]之间。//不取mid 是因为已经比末尾的值大否则,最小元素一定在[left, mid]中// mid 的值不能舍去还有可能成为最小值。这道题是一个二分法的变形,被二分的边界条件搞得比较晕…// 旋转数组也是半有序的数组//这里面是没有重复的所以是不可能相等的class Solution {p...原创 2018-12-10 17:59:51 · 122 阅读 · 0 评论 -
leetcode Merge sorted array
这道题采取的是从后往前填补的方式,如果还只剩下num1 就已经是完全排好序的如果nums2更加长然后还需要填充到num1里面using namespace std;class Solution {public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {...原创 2018-12-10 15:35:08 · 120 阅读 · 0 评论 -
leetcode 58. Length of Last Word
这道题主要要注意string 末尾有很多空格的情况,C++ 里面空格的判断是用单引号,顺便学习一下revese 需要#include#include<algorithm>class Solution {public: int lengthOfLastWord(string s) { int num = s.size(); if ...原创 2018-12-10 14:49:36 · 109 阅读 · 0 评论 -
leetcode 26 从重复中的数组中删除相同的元素并且返回长度
相同的元素直接覆盖的方法class Solution {public: int removeDuplicates(vector<int>& nums) { if(nums.size()==0) return 0; if(nums.size()==1) return 1; int index=0; /...原创 2018-12-09 23:59:15 · 152 阅读 · 0 评论 -
leetcode 9判断是不是回文数字
不可以采用回文字符串的方式两种方法:第一种方法是对先进行一个reverse 判断两个数字是否相等即可第二种方法采用的是判断迭代首位和末尾的是否相等;出去首位% 末位/10 的技巧博客中给出的是第二种方法class Solution {public: bool isPalindrome(int x) { if(x<0) return...原创 2018-12-09 19:52:43 · 128 阅读 · 0 评论 -
leetcode 1 two sum
改进版本//#include&amp;amp;lt;tr1/unordered_map&amp;amp;gt;#include &amp;amp;lt;iostream&amp;amp;gt;#include &amp;amp;lt;vector&amp;amp;gt;#include &amp;amp;lt;cassert&amp;amp;gt;#include &am原创 2018-12-09 16:58:28 · 90 阅读 · 0 评论 -
leet code 7 reverse Integer主要是考查溢出
class Solution {public: int reverse(int x) { bool flag=false; if(x<0) { flag=true; x=x*(-1); } int res=0; int temp=0; while(x) ...原创 2018-12-09 15:50:14 · 91 阅读 · 0 评论 -
leetcode 2 add two sum 包含链表创建和打印的程序易于测试
#include <iostream>#include <algorithm>using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} };//打印链表 void print...原创 2018-12-09 14:33:00 · 244 阅读 · 2 评论 -
3. Longest Substring Without Repeating Characters
暴力的方法,刚开始substr 用错导致走了很多弯路,需要注意的是第一个参数是首个下标,第二的参数是截取的长度,还需要注意的是s.substr(0,length) 才是整个字符串,所以第二个循环j<=length;路漫漫其修远,还要加油呀!以前没怎么接触过算法,所以想先写个暴力的算法让自己先理解,然后再试其它优化的方法#include<iostream>#include ...原创 2018-12-12 11:05:32 · 149 阅读 · 0 评论