今天我们接着讲简单题

1.leetcode#136. Single Number

1.1题目描述

iven an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

1.2思路

简单的想法就是直接建一个hash表,然后记录下见过的词,如果是新词,就求和,如果是见过的,那么就减掉。

public int singleNumber(int[] nums) {HashSet<Integer> hashMap=new HashSet<>();int sum=0;for(int i=0;i<nums.length;i++){if(hashMap.contains(nums[i])){sum-=nums[i];}else{hashMap.add(nums[i]);sum+=nums[i];}}return sum;}

1.3更进一步

但是事实上,人家只允许使用额外空间为O(1),因此上述方法行不通了,这时候,我们就要巧用位或运算符的性质,即一个数和0异或还是自己,一个数和自己异或是0。

1.4代码

public int singleNumber2(int[] nums) {int a = 0 ;for(int i : nums){a ^= i;}return a;}

2.leetcode#141. Linked List Cycle

2.1问题描述

Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?

2.2思路

这道题考察的是新的一个思路,即快慢指针的使用。这样可以发现如果出现环了,那么快指针一定会追上慢指针。

2.3代码

 public boolean hasCycle(ListNode head) {if(head==null||head.next==null) return false;ListNode step1=head;ListNode step2=head;while(step1!=null&&step2!=null){//慢指针step1=step1.next;if(step2.next!=null){//快指针step2=step2.next.next;}else{return false;}if(step1.equals(step2)){return true;}}return false;}

3.leetcode#155. Min Stack

3.1问题描述

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
getMin() – Retrieve the minimum element in the stack.

Example:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); –> Returns -3.
minStack.pop();
minStack.top(); –> Returns 0.
minStack.getMin(); –> Returns -2.

3.2思路

这道题是比较清楚的,即让你设计一个最小的栈,它和普通的栈的唯一区别就是,它可以一直找到那个最小的。非常巧妙的一个方法就是只记录当前数与当前最小数的差值即可。

3.3代码

class MinStack {long min;Stack<Long> stack;public MinStack(){stack=new Stack<>();}//压人public void push(int x) {//如果是空的,就压入0if (stack.isEmpty()){stack.push(0L);//设置最小值为X;min=x;}//如果不是空的else{//就需要压入差值stack.push(x-min);//如果是小于最小值的话if (x<min) min=x;}}//弹出public void pop() {//如果是空的,就返回为空if (stack.isEmpty()) return;long pop=stack.pop();//如果pop小于0,那么min就需要加上那么多if (pop<0)  min=min-pop;}//顶层public int top() {long top=stack.peek();//如果顶层是大于0的,说明不是最小值if (top>0){return (int)(top+min);}else{return (int)(min);}}//获取到最小值public int getMin() {return (int)min;}
}

4.leetcode#160. Intersection of Two Linked Lists

4.1问题描述

Write a program to find the node at which the intersection of two singly linked lists begins.

Example

For example, the following two linked lists:
A: a1 → a2

c1 → c2 → c3

B: b1 → b2 → b3
begin to intersect at node c1.

Notes:

If the two linked lists have no intersection at all, return null.
The linked lists must retain their original structure after the function returns.
You may assume there are no cycles anywhere in the entire linked structure.
Your code should preferably run in O(n) time and use only O(1) memory.

4.2思路

这道题最朴素的想法就是,我先遍历一遍,然后看谁长,谁长的话,我先遍历谁,然后到达两个一样长后,同时遍历。

4.3 代码

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {if(headA==null||headB==null){return null;}int countA=0;int countB=0;ListNode a = headA;ListNode b = headB;//统计个数while(a!=null){countA++;a=a.next;}while(b!=null){countB++;b=b.next;}//长的先走a=headA;b=headB;while(countA>countB){a=a.next;countA--;}while(countB>countA){b=b.next;countB--;}//判断相同的第一个while(a!=b){a=a.next;b=b.next;}return a;}

4.4更进一步

但是,其实还有更加巧妙的方法,这个方法不一定非常快,但是写代码一定非常快。那就是让指针A和指针B分别指向a和b链表,当指到头的时候,再换另一个链表头开始指,那么他们一定可以同时到达相同的节点,要么这个节点存在,要么同时到达NULL。
证明:假设a长度为a+c,假设b长度为b+c,那么如果有重叠的就是a+c+b=b+c+a,如果没重叠,则是a+c+b+d=b+d+a+c;

4.5代码

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {if(headA==null||headB==null){return null;}ListNode a = headA;ListNode b = headB;while( a != b){//如果A到头了,那么就移动到B上面a = a == null? headB : a.next;//如果B到头了,那么就移动到A上面b = b == null? headA : b.next;    }//要么a为相同的,要么a为nullreturn a;}

5.小结

这一次,我们学习了几个小技巧,第一个是使用异或运算,第二个是使用快慢指针,第三个是使用存储差值,第四个是使用两个链表链接,都是非常神奇的操作。

leetcode(7): easy2相关推荐

  1. leetcode 5. Longest Palindromic Substring 字符串中的最长回文数 逐步从O(n^2)优化至线性时间

    题目 解析 思路一 暴力解法 思路二 指针+最大长度 思路3 由中间至两边找回数 思路4 Manacher's algorithm 线性时间 参考文档 题目 链接 给定一个字符串 s,找到 s 中最长 ...

  2. LeetCode 10. Regular Expression Matching python特性、动态规划、递归

    前言 本文主要提供三种不同的解法,分别是利用python的特性.动态规划.递归方法解决这个问题 使用python正则属性 import reclass Solution2:# @return a bo ...

  3. leetcode Longest Substring with At Most Two Distinct Characters 滑动窗口法

    题目解析 代码如下 题目解析 这一题是一道会员的题目,题目介绍如下: Given a string, find the length of the longest substring T that c ...

  4. leetcode 3. Longest Substring Without Repeating Characters 最长非重复子串的长度 滑动窗口法

    题目链接 根据我们之前介绍的滑动窗口法的解法: 滑动窗口法详解 leetcode 438. Find All Anagrams in a String 滑动窗口法 这题,我们不难解决,使用之前的模板. ...

  5. leetcode:2680 Remove Duplicates from Sorted Array 删除数组中的重复元素

    leetcode:26 对数组元素进行去重,使得原数组重复元素最多保留1个 限制: 我们不可以额外分配数组,必须保持空间复杂度为O(1) 这个并不难实现: class Solution(object) ...

  6. LeetCode简单题之二进制表示中质数个计算置位

    题目 给你两个整数 left 和 right ,在闭区间 [left, right] 范围内,统计并返回 计算置位位数为质数 的整数个数. 计算置位位数 就是二进制表示中 1 的个数. 例如, 21 ...

  7. LeetCode简单题之删除字符使字符串变好

    题目 一个字符串如果没有 三个连续 相同字符,那么它就是一个 好字符串 . 给你一个字符串 s ,请你从 s 删除 最少 的字符,使它变成一个 好字符串 . 请你返回删除后的字符串.题目数据保证答案总 ...

  8. LeetCode简单题之找出两数组的不同

    题目 给你两个下标从 0 开始的整数数组 nums1 和 nums2 ,请你返回一个长度为 2 的列表 answer ,其中: answer[0] 是 nums1 中所有 不 存在于 nums2 中的 ...

  9. LeetCode中等题之区域和检索 - 数组可修改

    题目 给你一个数组 nums ,请你完成两类查询. 其中一类查询要求 更新 数组 nums 下标对应的值 另一类查询要求返回数组 nums 中索引 left 和索引 right 之间( 包含 )的nu ...

最新文章

  1. hadoop balance failed
  2. 注意事项,不定期更新
  3. 关于Python的编译
  4. leetcode :动态规划
  5. java的for循环取出数据只是拿到最后一个_一问SQL优化就无从藏身?那只是你对原理的精髓掌握不深
  6. python打印九九加法表_Python小脚本
  7. devops基础扫盲篇_在2020年取得成功的8篇必读的DevOps文章
  8. 京东商品信息及其价格爬虫
  9. centos 6.5卸载Mysql
  10. Github应用最广泛的开源项目
  11. cocos creator android之微信开放平台修改签名 baseResp.errCode=-6
  12. 最速下降法matlab全局最小值_matlab实现最速下降法和dfp求函数最小值
  13. 游戏开发如此简单?我直接创建了一个游戏场景【python 游戏实战 02】
  14. 微信PC版通信协议研究
  15. VB.Net实现身份证读卡器调用读取身份证信息和社保卡信息
  16. 嘿!不备案不要HTTPS!小程序请求任意网站黑科技来了!
  17. JEECMS 自定义标签
  18. android集成友盟u app,友盟U-Mini小程序集成指南
  19. 美的 Dell 国信证券面经整理
  20. MySQL Order by 和 max哪个性能更高更好?

热门文章

  1. VMware环境部署vFW虚拟防火墙
  2. Oracle数据库配置
  3. 基本数据类型的隐式转换
  4. 如何优雅的阅读DBNet的训练代码
  5. Qt扫盲-QTabWidget理论总结
  6. OpenCV每日函数 几何图像变换模块 (9) resize函数
  7. 反向传播神经网络(BPNN)的实现(Python,附源码及数据集)
  8. oracle 主键、唯一键值、唯一索引关系
  9. Oracle--“ORA-28007: the password cannot be reused”解决
  10. BiSeNet V2网络结构详解