leetcode(21)
-
Reverse String
SolutionWrite a function that reverses a string. The input string is given as an array of characters s.You must do this by modifying the input array in-place with O(1) extra memory. ExamplesExample 1:Input: s = ["h","e","l","l","o"]Output: ["o","l","l","e","h"] Example 2:Input: s = ["H","a","n","n","a","h"]Output: ["h","a","n","n","a","H"] Explanationclass Solution { public void reverseStri..
2024.10.06 -
Valid Sudoku
SolutionDetermine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:Each row must contain the digits 1-9 without repetition.Each column must contain the digits 1-9 without repetition.Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.Note:A Sudoku board (partially filled) could be valid but is no..
2024.10.04 -
Two Sum
SolutionGiven an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.You may assume that each input would have exactly one solution, and you may not use the same element twice.You can return the answer in any order. Example Explanationclass Solution { public int[] twoSum(int[] nums, int target) { Map map = new HashMap(); ..
2024.09.24 -
Move Zeroes
SolutionGiven an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.Note that you must do this in-place without making a copy of the array. Examples Explanationclass Solution { public void moveZeroes(int[] nums) { int idx = 0; for(int i = 0; i Create a variable, idx, to keep track of the position where the next non-..
2024.09.23 -
Plus One
SolutionYou are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.Increment the large integer by one and return the resulting array of digits. Examples Explanationclass Solution { public..
2024.09.20 -
Intersection of Two Arrays II
SolutionGiven two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order. ExampleExample 1:Input: nums1 = [1,2,2,1], nums2 = [2,2]Output: [2,2]Example 2:Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]Output: [4,9]Explanation: [9,4] is also accepted. Explainatio..
2024.09.12