leetcode [#20] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (4)
目录1. 题目2. 解决方案3. 注意事项
题目Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid.
The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.
解决方案12345678910111213141516171819202122232425262728293031323334353637383940public class Solution {    public boolean isValid(String s) {        char[] sArr = s.toCharArray();        Stack<String> stack = new Stack<>();        boolean result = true...阅读全文

leetcode [#205] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (4)
目录1. 题目2. 解决方案3. 注意事项
题目Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
ExampleGiven “egg”, “add”, return true.
Given “foo”, “bar”, return false.
Given “paper”, “title”, return true.
NoteYou may assume both s...阅读全文

leetcode [#203] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (5)
目录1. 题目2. 解决方案3. 注意事项
题目Remove all elements from a linked list of integers that have value val.
Example:Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6Return: 1 –> 2 –> 3 –> 4 –> 5
解决方案12345678910111213141516171819202122232425/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public ListNode removeElements(ListNode head, int val) {        if(head == null) return null;       ...阅读全文

leetcode [#202] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (10)
目录1. 题目2. 解决方案3. 注意事项
题目Write an algorithm to determine if a number is “happy”.
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
Example:19 is a happy number
12 + 92 = 8282 + 22 = 6862 + 82 = ...阅读全文

leetcode [#206] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (8)
目录1. 题目2. 解决方案3. 注意事项
题目Reverse a singly linked list.
解决方案123456789101112131415161718192021222324252627282930313233/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public ListNode reverseList(ListNode head) {        if(head == null || head.next == null) return head;        ArrayList<Integer> valList = new ArrayList<Integer>();        ListNode p = head;        while (p != nu...阅读全文