没有合适的资源?快使用搜索试试~ 我知道了~
力扣刷题笔记总结记录666
资源推荐
资源详情
资源评论
0、Java 链表
1) 什么是链表?
链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通
过链表中的指针连接次序实现的。
每一个链表都包含多个节点,节点又包含两个部分,一个是数据域(储存节点含有
的信息),一个是引用域(储存下一个节点或者上一个节点的地址)
2) 链表的特点?
获取数据麻烦,需要遍历查找,比数组慢。
方便插入、删除。
3) 链表的实现原理?
创建一个节点类,其中节点类包含两个部分,第一个是数据域(你到时候要往节点里面
储存的信息),第二个是引用域(相当于指针,单向链表有一个指针,指向下一个节点;
双向链表有两个指针,分别指向下一个和上一个节点)。
创建一个链表类,其中链表类包含三个属性:头结点、尾节点和大小,方法包含添加、
删除、插入等等方法。
单向链表的节点类
public class Node {
public Object data;
public Node next;
public Node(Object e){
this.data = e;
}
}
双向链表的节点类
public class Node {
public Object e;
public Node next;
public Node pre;
public Node(){
}
public Node(Object e){
this.e = e;
next = null;
pre = null;
}
}
1、哈希表用法举例
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那
两个 整数,并返回它们的数组下标。
class Solution {
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
Map<Integer,Integer> hashtable = new HashMap<Integer,Integer>();
for(int i=0;i<n;i++){
if(hashtable.containsKey(target-nums[i])){
return new int[]{hashtable.get(target-nums[i]),i};
}
hashtable.put(nums[i],i);
}
return new int[0];
}
}
哈希表:码值散列映射,给定 key 直接访问 value,加快查找速度。
2、栈的用法举例
class Solution {
private static final Map<Character,Character> map = new
HashMap<Character,Character>(){{
put('{','}'); put('[',']'); put('(',')'); put('?','?');
}};
public boolean isValid(String s) {
if(s.length() > 0 && !map.containsKey(s.charAt(0))) return false;
LinkedList<Character> stack = new LinkedList<Character>() {{ add('?'); }};
for(Character c : s.toCharArray()){
if(map.containsKey(c)) stack.addLast(c);
else if(map.get(stack.removeLast()) != c) return false;
}
return stack.size() == 1;
}
}
3、链表的用法举例
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有
节点组成的。
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
} else if (l2 == null) {
return l1;
} else if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
}
递归:将整个问题抽象出一个子问题封装该子问题。递归时不要想递归进去是什么,而是
想递归获得了什么结果,然后关注递归后对递归结果的操作就行, 递归的每一次调用都能
正确的完成你想完成的子任务。
4、简单算法
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),
返回其最大和。
class Solution {
public int maxSubArray(int[] nums) {
int pre = 0, maxAns = nums[0];
for (int x : nums) {
//前一个元素加当前值与当前值比较大小,若当前值大说明前一个值小于 0;
反之说明前一个值大于 0
pre = Math.max(pre + x, x);
maxAns = Math.max(maxAns, pre);
}
return maxAns;
}
}
动态规划:若前一个元素大于 0,则将其加到当前元素上。
前面累加的和 pre 如果是小于 0,那么和自己本身 x 累加(pre+x)后,一定小于自己本
身(本身单个整数也是一个子数组),所以之前的子数组就不需要考虑,那么最大和的子
序列就仅需要再从当前 x 开始累加,如果 pre 大于 0,那么自己本身就需要作为最大和的
子数组中的整数累加上去,以此类推。
5、判断字符是否唯一(开始面试真题集)
判断字符是否唯一: 实现一个算法,确定一个字符串 s 的所有字符是否全都不同。
方法一:利用 HashSet 集合唯一性。
public static boolean isUnique(String astr) {
int n = astr.length();
if(astr==null||n<=1){
return true;
}
HashSet<Character> hs = new HashSet<Character>();
for (Character c : astr.toCharArray()) {
hs.add(c);
}
System.out.println("hs="+hs.size()+";n="+n);
if(hs.size()==n){
return true;
}else{
return false;
}
}
方法二: 如果一个字符只出现一直,则 indexOf 和 lastIndexOf 的返回值一定是以一样的。
public static boolean isUnique2(String astr) {
if(astr==null||astr.length()<=1){
return true;
}
for (Character c:astr.toCharArray()){
if(astr.indexOf(c)!=astr.lastIndexOf(c)){
return false;
}
}
return true;
}
方法三:利用 replace 替换为空后做长度判断。
public static boolean isUnique3(String astr) {
if(astr==null||astr.length()<=1){
return true;
}
for (Character c:astr.toCharArray()){
astr.replaceAll(String.valueOf(c),"");
if(astr.indexOf(c)!=astr.lastIndexOf(c)){
return false;
剩余16页未读,继续阅读
资源评论
chi_666
- 粉丝: 66
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功