leetcode [#350] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (14)
目录1. 题目2. 解决方案3. 注意事项
题目Given two arrays, write a function to compute their intersection.
Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].
Note:
Each element in the result should appear as many times as it shows in both arrays.The result can be in any order.解决方案1234567891011121314151617181920212223242526public class Solution {    public int[] intersect(int[] nums1, int[] nums2) {        Arrays.sort(nums1);        Arrays.sort(nums2);        List<Integer> list = new ArrayList...阅读全文

leetcode [#35] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (14)
目录1. 题目2. 解决方案3. 注意事项
题目Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Example:[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0
解决方案12345678910111213141516171819202122232425262728public class Solution {    public int searchInsert(int[] nums, int target) {        int N = nums.length;        int result = 0;        if(N == 0) retu...阅读全文

leetcode [#38] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (13)
目录1. 题目2. 解决方案3. 注意事项
题目The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, …
1 is read off as “one 1” or 11.11 is read off as “two 1s” or 21.21 is read off as “one 2, then one 1” or 1211.Given an integer n, generate the nth sequence.
Note:The sequence of integers will be represented as a string.
解决方案1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677...阅读全文

leetcode [#36] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (11)
解决方案12注意事项以下方法是错误的:12345678910111213141516171819202122import java.util.HashSet;public class Solution {    public boolean isValidSudoku(char[][] board) {        for(int i = 0; i < 9; i++){            HashSet<Character> set = new HashSet<>();            for(int j = 0; j < 9; j++) if(!set.add(board[i][j]) && board[i][j] != '.') return false;        }        for(int i = 0; i < 9; i += 3){            HashSet<Character> set = new HashSet<>();            for(int j = i; j < i+3; j++){                for...阅读全文

leetcode [#389] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (12)
目录1. 题目2. 解决方案3. 注意事项
题目Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.
Example:Input:s = “abcd”t = “abcde”
Output:e
Explanation:‘e’ is the letter that was added.
解决方案12345678910public class Solution {    public char findTheDifference(String s, String t) {        if ((s.length() == 0)) return t.charAt(0);        char result = 'a';   ...阅读全文