leetcode [#8] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (25)
目录1. 题目2. 解决方案3. 注意事项
题目Implement atoi to convert a string to an integer.
HintCarefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
NotesIt is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
解决方案12345678910111213141516171819202122232425262728293031323334353637383940import java.util.regex.Matcher;import jav...阅读全文

leetcode [#82] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (23)
目录1. 题目2. 解决方案3. 注意事项
题目Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example:Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2->3.
解决方案1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Sol...阅读全文

leetcode [#86] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (26)
目录1. 题目2. 解决方案3. 注意事项
题目Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
Example:Given 1->4->3->2->5->2 and x = 3,return 1->2->2->4->3->5.
解决方案1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556/** * Definition for singly-linked list. * public class ListNode { *     int val;...阅读全文

leetcode [#88] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (24)
目录1. 题目2. 解决方案3. 注意事项
题目Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
解决方案1234567891011121314151617181920212223242526272829303132public class Solution {    public void merge(int[] nums1, int m, int[] nums2, int n) {        int[] result = new...阅读全文

leetcode [#83] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (26)
目录1. 题目2. 解决方案3. 注意事项
题目Given a sorted linked list, delete all duplicates such that each element appear only once.
Example:Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.
解决方案123456789101112131415161718192021/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public ListNode deleteDuplicates(ListNode head) {        ListNode p = head;        while(p != null && ...阅读全文