这段时间断断续续刷力扣题,因为太菜,所以先把高频的简单题刷一遍...做个笔记记录一下,闲来无事就拿出来看一眼...也非常渴望和各位大佬讨论算法~
206.翻转链表(no)
思路:(迭代法)
因为给出了头结点,所以首先创建一个前结点pre对象记录当前结点的前一结点,然后再创建一个cur记录当前结点所在位置,如果当前结点不为null,则执行:创建一个next结点记录当前结点的下一个结点(避免当前结点的next指向pre后找不到下一个结点位置),接着当前结点的next指向上一结点(头结点则指向null),再让pre移到当前结点cur的位置,cur移到之前记录下来的next的位置,一直遍历,直到cur为null,pre在尾结点位置停止,返回头结点pre,因为链表翻转了,所以旧链表尾结点位置就是新链表的头结点位置。
代码:
public ListNode reverseList(ListNode head) {
ListNode pre=null;
ListNode cur=head;
while(cur!=null){
ListNode next=cur.next;
cur.next=pre;
pre=cur;
cur=next;
}
return pre;
}
思路:(递归法)
个人觉得比较难,看这个大佬的可能好理解一点
代码:
/**
* 以链表1->2->3->4->5举例
* @param head
* @return
*/
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
/*直到当前节点的下一个节点为空时返回当前节点
由于5没有下一个节点了,所以此处返回节点5
*/
return head;
}
//递归传入下一个节点,目的是为了到达最后一个节点
ListNode newHead = reverseList(head.next);
/*
第一轮出栈,head为5,head.next为空,返回5
第二轮出栈,head为4,head.next为5,执行head.next.next=head也就是5.next=4, 把当前节点的子节点的子节点指向当前节点 此时链表为1->2->3->4<->5,由于4与5互相指向,所以此处要断开4.next=null 此时链表为1->2->3->4<-5 返回节点5
第三轮出栈,head为3,head.next为4,执行head.next.next=head也就是4.next=3, 此时链表为1->2->3<->4<-5,由于3与4互相指向,所以此处要断开3.next=null 此时链表为1->2->3<-4<-5 返回节点5
第四轮出栈,head为2,head.next为3,执行head.next.next=head也就是3.next=2, 此时链表为1->2<->3<-4<-5,由于2与3互相指向,所以此处要断开2.next=null 此时链表为1->2<-3<-4<-5 返回节点5
第五轮出栈,head为1,head.next为2,执行head.next.next=head也就是2.next=1, 此时链表为1<->2<-3<-4<-5,由于1与2互相指向,所以此处要断开1.next=null 此时链表为1<-2<-3<-4<-5 返回节点5
出栈完成,最终头节点5->4->3->2->1
*/
head.next.next = head;
head.next = null;
return newHead;
}
思路:(对称交换值)
相当于是每次循环都new一个结点,然后让head结点开始的每一个值赋给新结点,并让这个结点指向上一个结点。比如第一个ans是null,new ListNode(x.val)表示new了一个新结点,结点的值就是head的结点值,并让其指向null,然后把新结点赋给ans,ans就成了上一个结点,继续进行也是同理,直到链表遍历完。
代码:
public ListNode reverseList(ListNode head) {
ListNode ans = null;
for (ListNode x = head; x != null; x = x.next) {
ans = new ListNode(x.val,ans); }
return ans;
}
53.最大子数组和(no)
思路:(动态规划)
当下标前面的一个数小于0时,舍弃掉,下标后移,当下标前面的数大于0时,下标元素和前一元素相加,一直遍历到最后一个数,然后找出最大值
代码:
public int maxSubArray(int[] nums) {
int max=nums[0];
for(int i=1;i<nums.length;i++){
if(nums[i-1]>0){
nums[i]+=nums[i-1];
}
if(max<nums[i])
max=nums[i];
}
return max;
}
思路:(分治算法)
利用分治的办法,将数组从最底层一直往上传递,每一层都找其以某个结点为最左/右端点的最大连续子序列和,和整个完整区间的和进行对比,找出两者的最大值,一直往上传递,直到最高一层进行对比可以得出结果。
代码:
class Solution {
public class Status {
public int lSum, rSum, mSum, iSum;
public Status(int lSum, int rSum, int mSum, int iSum) {
this.lSum = lSum;
this.rSum = rSum;
this.mSum = mSum;
this.iSum = iSum;
}
}
public int maxSubArray(int[] nums) {
return getInfo(nums, 0, nums.length - 1).mSum;
}
public Status getInfo(int[] a, int l, int r) {
if (l == r) {
return new Status(a[l], a[l], a[l], a[l]);
}
int m = (l + r) >> 1;
Status lSub = getInfo(a, l, m);
Status rSub = getInfo(a, m + 1, r);
return pushUp(lSub, rSub);
}
public Status pushUp(Status l, Status r) {
int iSum = l.iSum + r.iSum;
int lSum = Math.max(l.lSum, l.iSum + r.lSum);
int rSum = Math.max(r.rSum, r.iSum + l.rSum);
int mSum = Math.max(Math.max(l.mSum, r.mSum), l.rSum + r.lSum);
return new Status(lSum, rSum, mSum, iSum);
}
}
1.两数之和
思路:(暴力枚举)
就从第一个数开始,一直跟后面的数相加,看是不是等于目标数,如果是就返回
代码:
public int[] twoSum(int[] nums, int target) {
for(int i=0;i<nums.length-1;i++){
for(int j=i+1;j<nums.length;j++){
if(nums[i]+nums[j]==target){
return new int[]{i,j}
}
}
}
return nums[0];
}
思路:(哈希表)
对于数组中的每个数,都去判断哈希表中是否有数能和其相加等于目标数,如果有则返回哈希表的下标和当前数组下标
代码:
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; ++i) {
if (hashMap.containsKey(target - nums[i])) {
return new int[]{hashMap.get(target - nums[i]), i};
}
hashMap.put(nums[i], i);//键是当前下标元素值,值是当前下标
//所以可以使用containsKey查找有无能和数组当前下标元素相加为目标数的键,把它的值取出
}
return new int[0];
}
141.环形链表(no)
思路:(哈希表)
当head不为空的时候一直next,因为是hashset,所以元素是不能重复的,当遍历到一个重复元素时add就会失败,然后可以判断有环的存在。
代码
public boolean hasCycle(ListNode head) {
Set<ListNode> hash=new HashSet<ListNode>();
while(head!=null){
if(!hash.add(head))
return true;
head=head.next;
}
return false;
}
思路:(快慢指针)
设置一个快指针、一个慢指针,快指针每次移动两个节点即fast.next.next,慢指针每次移动一个结点即slow.next,如果快指针能追的上慢指针即fast=slow或者fast.next=slow就表示有环的存在
代码:
public boolean hasCycle(ListNode head) {
ListNode slow=head;
if(head==null||head.next==null)
return false;
ListNode fast=head.next;
while(fast!=null&&slow!=null){
if(fast==slow||fast.next==slow){
return true;
}
slow=slow.next;
if(fast.next==null||fast.next.next==null)
return false;
fast=fast.next.next;
}
return false;
}
121.买卖股票的最佳时机(no)
思路:(动态规划)
只遍历一次数组,可以获得最小值minprice,然后和后面的天数的价格进行相减,获得最大收益。因为是从头到尾遍历,所以一定能得到最小值minprice,就算是在最小值前面就已经出现了最大收益,也因为在这之前已经有maxPro记录下来,如果最小值和后面其他天数的相减结果不大于这个maxPro,则最小值还是原来的值。
代码:
public int maxProfit(int[] prices) {
int minprice=Integer.MAX_VALUE;
int maxPro=0;
for(int i=0;i<prices.length;i++){
if(prices[i]<minprice){
minprice=prices[i];//遍历数组顺便找出最小值,
//并从当前最小值开始与后面的数进行比较
}else if(prices[i]-minprice>maxPro){
maxPro=prices[i]-minprice;
}
}
return maxPro;
}
20.有效的括号(no)
思路:(栈)
如果是前括号则入栈,如果是后括号则出栈,判断出栈的元素是不是对应的前括号,如果是则继续进行入栈出栈的操作,如果不是则返回false。
代码:
public boolean isValid(String s) {
Stack<Character> stack=new Stack<>();
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
if(c=='('||c=='['||c=='{'){
stack.push(c);
}else if(stack.isEmpty()||c==')'&&stack.pop()!='('
||c==']'&&stack.pop()!='['||c=='}'&&stack.pop()!='{'){
return false;
}
}
return stack.isEmpty();
}