leetcode刷题笔记(1)
2020-07-20 04:18
411 查看
算法基础很弱,所以慢慢刷,没有什么很好的想法,单纯地想记录,方便之后复习。
题解:
class Solution { public int[] twoSum(int[] nums, int target) { int i,j; for(i=0;i<nums.length;i++){ for(j=1;j<nums.length;j++){ if(nums[i]+nums[j]==target&&i!=j){ return new int[]{i,j}; } } } return null; } }
解法用的就是暴力解,时间复杂度o(n^2),空间复杂度o(1)
哈希是把时间复杂度变成n,空间复杂度变成n,牺牲空间来减少时间的方法
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector<int> res;//res储存结果 unordered_map<int, int> hash;//定义哈希表 for(int i = 0; i < nums.size(); i ++ ) { int another = target - nums[i];//假如target=8,nums[0]=2,需要去查找当前有没有6这个数字,位于哪里,所以本质是用到查找 if(hash.count(another))//判断hash表中是否存在关键字6 { res = vector<int> ({hash[another], i});//存在6,则给res赋值,代表6和2在数组中的位置,用hash[another]代表key break; } hash[nums[i]] = i;//不存在6,hash[nums[0]=0-->hash[2]=0 } return res; } };
题解:
class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode pre = new ListNode(0); //pre结点=0 ListNode cur = pre;//进位=pre int carry = 0;//初始化进位 while(l1 != null || l2 != null) { //若两链表均非0 int x = l1 == null ? 0 : l1.val; int y = l2 == null ? 0 : l2.val; int sum = x + y + carry; //l1赋值给x,如果l1=null,则赋值0,else 赋值l1.val //sum=x+y+进位 carry = sum / 10; //假如算出来等于13,取整=1,进位为1 sum = sum % 10; //sum=13对10取模 =3 cur.next = new ListNode(sum); //cur.next的sum=1 cur = cur.next; if(l1 != null)//遍历链表 l1 = l1.next; if(l2 != null) l2 = l2.next; } if(carry == 1) {//最终仍有进位 cur.next = new ListNode(carry); } return pre.next; } }
作者:guanpengchn
链接:https://leetcode-cn.com/problems/add-two-numbers/solution/hua-jie-suan-fa-2-liang-shu-xiang-jia-by-guanpengc/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
一般的方法是暴力遍历,不考虑
这道题用到的算法是滑动窗口,遍历一遍就可以了,时间复杂度o(n)
题解:
class Solution { public: int lengthOfLongestSubstring(string s) { if(s.size() == 0) return 0;//s=0情况 unordered_set<char> lookup; //定义哈希表 int maxStr = 0;//最大串 int left = 0; //从最左边开始遍历 for(int i = 0; i < s.size(); i++){ while (lookup.find(s[i]) != lookup.end()){ //a.find("eeee"):查找元素"eeee",返回结果为a.end()则表明没有找到,否则返回所对应元素 //look.find(s[i])查找s[i]元素(从i=0开始,寻找重复的元素,如果没找到,就返回s[i] lookup.erase(s[left]); //a.erase("aaa"):清除元素"aaa" left ++; //向右滑动窗口! } maxStr = max(maxStr,i-left+1); lookup.insert(s[i]); } return maxStr; } };
https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/comments/376989
class Solution { public int lengthOfLongestSubstring(String s) { // 记录字符上一次出现的位置 int[] last = new int[128]; for(int i = 0; i < 128; i++) { last[i] = -1; } int n = s.length(); int res = 0; int start = 0; // 窗口开始位置 for(int i = 0; i < n; i++) { int index = s.charAt(i); start = Math.max(start, last[index] + 1); res = Math.max(res, i - start + 1); last[index] = i; } return res; } }
相关文章推荐
- LeetCode刷题笔记一
- leetcode笔记:N-Queens II
- leetcode笔记:Unique Paths II
- leetcode笔记:Reverse Integer
- leetcode笔记:Move Zeroes
- LeetCode刷题笔记19-5-5
- (刷题笔记) Leetcode 896.单调数列
- [leetCode刷题笔记]2017.04.02
- leetcode刷题笔记,岛屿最大面积问题
- 【leetcode刷题笔记】Length of Last Word
- LeetCode刷题笔记 [26] 删除排序数组中的重复项
- leetCode 笔记 ————二叉树最小深度
- leetcode解题笔记-word-ladder (python版)
- leetcode笔记--Roman to Integer
- LeetCode刷题笔记(Sort Colors)
- leetcode笔记--Simplify Path
- bj_LeetCode刷题笔记
- LeetCode笔记:344. Reverse String
- LeetCode刷题笔记 202. 快乐数
- LeetCode笔记:263. Ugly Number