头像

ymy

A person


访客:1747

离线:2天前



ymy
2个月前
class Solution {
public:
    int NumberOf1(int n) {
        int res = 0;
       while(n)
       {
           n -= (n & (-n));
           res ++;
       }
        return res;
    }
};



ymy
2个月前
class Solution {
public:
    vector<int> findNumbersWithSum(vector<int>& nums, int target) {
       unordered_set<int> M;
       for(int x : nums)
       {
           if(M.count(target - x))  return {x,target - x};
           M.insert(x);
       }
    }
};



ymy
2个月前
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode deleteDuplication(ListNode head) {

        ListNode dummy = new ListNode(-1);
        dummy.next = head;

        ListNode p = dummy;
        while(p.next != null)
        {
            ListNode q = p.next;

            while(q != null && p.next.val == q.val)
            {
                q = q.next;
            }
            if(p.next.next == q)
            {
                p = p.next;
            }
            else
            {
                p.next = q;
            }
        }

        return dummy.next;

    }
}



ymy
2个月前
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
class Solution {
    public ListNode findFirstCommonNode(ListNode headA, ListNode headB) {
        ListNode p = headA, q = headB;

        while(p != q)
        {
            if(p != null)
            {
                p = p.next;
            }
            else
            {
                p = headB;
            }
            if(q != null)
            {
                q = q.next;
            }
            else
            {
                q = headA;
            }
        }

        return q;
    }
}


活动打卡代码 AcWing 35. 反转链表

ymy
2个月前
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    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;   
    }
}



ymy
2个月前
class Solution {
    public int strToInt(String str) {
        if(str.length() == 0)
        {
            return 0; 
        }
        int k = 0;
        while(k < str.length() && str.charAt(k) == ' ') //过滤空格
        {
            k ++;
        }
        long number = 0;
        boolean is_minus = false;
        if(str.charAt(k) == '+')
        {
            k ++;
        }
        else if(str.charAt(k) == '-')
        {
            k ++;
            is_minus = true;
        }
        while(k < str.length() && (str.charAt(k) >= '0' && str.charAt(k) <= '9'))
        {
            number = number * 10 + str.charAt(k) - '0';
            k ++;
        }
        if(is_minus) number *= -1;
        if(number > Integer.MAX_VALUE) number = Integer.MAX_VALUE;
        if(number < Integer.MIN_VALUE) number = Integer.MIN_VALUE;

        return (int)number;
    }
}


活动打卡代码 AcWing 78. 左旋转字符串

ymy
2个月前
class Solution {
    public String leftRotateString(String str,int n) {
        String a = "";
        for(int i = n; i < str.length(); i ++)
        {
            a += str.charAt(i);
        }

        for(int i = 0; i < n; i ++)
        {
            a += str.charAt(i);
        }

        return a;
    }
}



ymy
2个月前
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* merge(ListNode* l1, ListNode* l2) {

       ListNode* dummyHead = new ListNode(-1);
       auto tail = dummyHead;

       while(l1 && l2)
       {
           if(l1 -> val < l2 -> val)
           {
               tail -> next = l1;
               l1 = l1 -> next;
           }

           else
           {
               tail -> next = l2;
               l2 = l2 -> next;
           }

          tail = tail -> next;
       } 
           if(l1) tail -> next = l1;
           if(l2) tail -> next = l2;

       return dummyHead -> next;
    }   
};



ymy
2个月前
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void deleteNode(ListNode* node) {  // 参数为当前 节点 的指针,删除当前节点
        node -> val = node -> next -> val;   // 伪装法,先伪装成为下一个人
        node -> next = node -> next -> next; // 跳过下一个人
    }
};


活动打卡代码 AcWing 16. 替换空格

ymy
2个月前
class Solution {
    public String replaceSpaces(StringBuffer str) {
        return str.toString().replaceAll(" ", "%20");
    }
}