leetcode [#13] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (6)
目录1. 题目2. 解决方案3. 注意事项
题目Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
解决方案12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849public class Solution {    public int romanToInt(String s) {        int len = s.length();        int result = 0;        if(s == null || len == 0) return 0;        for(int i = 0; i < len; i++){            if(s.charAt(i) == 'M'){                result += 1000;            } ...阅读全文

leetcode [#141] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (5)
目录1. 题目2. 解决方案3. 注意事项
题目Given a linked list, determine if it has a cycle in it.
解决方案12345678910111213141516171819202122232425/** * Definition for singly-linked list. * class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public boolean hasCycle(ListNode head) {        ListNode p = head;        HashSet<ListNode> arr = new HashSet<>();        while(p != null){            if(arr.add(p) == fa...阅读全文

leetcode [#125] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (9)
目录1. 题目2. 解决方案3. 注意事项
题目Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Example“A man, a plan, a canal: Panama” is a palindrome.“race a car” is not a palindrome.
NoteHave you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
解决方案12345678910111213141516171819202122232425262728293031323334public class Solution {  ...阅读全文

leetcode [#142] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (5)
目录1. 题目2. 解决方案3. 注意事项
题目Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
解决方案12345678910111213141516171819202122232425/** * Definition for singly-linked list. * class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public ListNode detectCycle(ListNode head) {        ListNode p = head;        HashSet<ListNode> arr = new HashSet<>();        whil...阅读全文

leetcode [#160] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (7)
目录1. 题目2. 解决方案3. 注意事项
题目Write a program to find the node at which the intersection of two singly linked lists begins.
Example:the following two linked lists:A:          a1 → a2                   ↘                     c1 → c2 → c3                   ↗B:     b1 → b2 → b3
begin to intersect at node c1.
解决方案12345678910111213141516171819202122232425262728293031323334353637/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { ...阅读全文