Java(30)
-
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 -
Single Number
SolutionGiven a non-empty array of integers nums, every element appears twice except for one. Find that single one.You must implement a solution with a linear runtime complexity and use only constant extra space. ExampleExample 1:Input: nums = [2,2,1]Output: 1Example 2:Input: nums = [4,1,2,1,2]Output: 4Example 3:Input: nums = [1]Output: 1 Explaination class Solution { public int singleNumber(..
2024.09.05 -
Rotate Array
SolutionGiven an integer array nums, rotate the array to the right by k steps, where k is non-negative. ExampleInput: nums = [1,2,3,4,5,6,7], k = 3Output: [5,6,7,1,2,3,4]Explanation:rotate 1 steps to the right: [7,1,2,3,4,5,6]rotate 2 steps to the right: [6,7,1,2,3,4,5]rotate 3 steps to the right: [5,6,7,1,2,3,4] class Solution { public void rotate(int[] nums, int k) { k %= nums.length..
2024.09.03