剑指Offer(第二版):50. 第一个只出现一次的字符 57. 和为s的两个数字 52. 两个链表的第一个公共节点 18. 删除链表的节点

dasdasdas剑指Offer(第二版):50. 第一个只出现一次的字符 57. 和为s的两个数字 52. 两个链表的第一个公共节点 18. 删除链表的节点_第1张图片

//最麻烦的双层遍
class Solution {
     
    public char firstUniqChar(String s) {
     
     
        int[] target = new int[26];

        for(int i=0 ; i<s.length() ; i++){
     
            target[s.charAt(i) - 'a']++;
        }
        for(int i=0 ; i<s.length() ; i++){
     
            if(target[s.charAt(i) - 'a'] == 1){
     
                return s.charAt(i);
            }
        }
        return ' ';
    }
}

class Solution {
     
    public char firstUniqChar(String s) {
     
        //首先创建哈希表
        Map<Character , Boolean> char_Map = new HashMap<>();

        char[] s_Array = s.toCharArray();

        for(char c: s_Array){
     
            char_Map.put(c,!char_Map.containsKey(c));  //只要超过两个就令value=false
        }
        for(char c : s_Array){
     
            if(char_Map.get(c)) return c;
        }
        return ' ';
    }
}

dasdasdas剑指Offer(第二版):50. 第一个只出现一次的字符 57. 和为s的两个数字 52. 两个链表的第一个公共节点 18. 删除链表的节点_第2张图片

// 哈希
class Solution {
     
    public int[] twoSum(int[] nums, int target) {
     
        //遍历数组,放入到map中,val为下标,key为对应的值
        Map<Integer , Integer> map = new HashMap<>();
        for(int i=0 ; i<nums.length ; i++){
     
            map.put(nums[i] , i);
        }
        for(int i=0 ; i<nums.length ; i++){
     
            if(map.containsKey(target-nums[i]))
                return new int[]{
     nums[i],target-nums[i]};
        }
        return new int[]{
     };
    }
}

class Solution {
     
    public int[] twoSum(int[] nums, int target) {
     
        int i=0 , j= nums.length-1;
        while(i<j){
     
           int num = nums[i] + nums[j];
           if(num>target){
     
               j--;
           }else if(num < target){
     
               i++;
           }else{
     
               return new int[]{
     nums[i] , nums[j]};
           }
        }
        return new int[]{
     };
    }
}

dassaddasdasd剑指Offer(第二版):50. 第一个只出现一次的字符 57. 和为s的两个数字 52. 两个链表的第一个公共节点 18. 删除链表的节点_第3张图片

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
  
//让两个链表的头节点分别进行循环,当各自的链表走完后就接着走另一个链表的,如果有重合点最终会遇到
public class Solution {
     
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
     
        //边界
        if(headA == null || headB == null)
            return null;
        //到此,都不为空,开始遍历
        ListNode tmp1 = headA;
        ListNode tmp2 = headB;

        while(tmp1!=tmp2){
     
            //这里不能取tmp1.next==null,因为这样就漏掉了没有重复节点的时候,这样返回不了null,就造成了死循环
            tmp1 = tmp1==null? headB : tmp1.next;
            tmp2 = tmp2==null? headA : tmp2.next;
            
        }
        return tmp1;
    }
}

dasdasdas剑指Offer(第二版):50. 第一个只出现一次的字符 57. 和为s的两个数字 52. 两个链表的第一个公共节点 18. 删除链表的节点_第4张图片

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
     
    public ListNode deleteNode(ListNode head, int val) {
     
        //因为要判断删除的节点,所以要用到用next提前判断,然后记录下next.next
        //先判断是否为空节点
        if(head == null)    return null;
        //先判断头节点
        if(head.val == val)
            return head.next;
        // 到此不为空,或者头节点不是要找的值
        ListNode tmp = head;
        while(tmp.next!=null){
     
            if(tmp.next.val == val)
                tmp.next = tmp.next.next==null? null : tmp.next.next; 
            else
                tmp = tmp.next;
        }
        return head;
    }
}

你可能感兴趣的:(#,剑指Offer(第二版),算法)