leetcode(21)
-
Longest Common Prefix
SolutionWrite a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string "". ExamplesExample 1:Input: strs = ["flower","flow","flight"]Output: "fl" Example 2:Input: strs = ["dog","racecar","car"]Output: ""Explanation: There is no common prefix among the input strings. Explanationclass Solution { public String longestCom..
2024.10.16 -
Implement strStr()
SolutionGiven two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. ExamplesExample 1:Input: haystack = "sadbutsad", needle = "sad"Output: 0Explanation: "sad" occurs at index 0 and 6.The first occurrence is at index 0, so we return 0. Example 2:Input: haystack = "leetcode", needle = "leeto"Output: -1Explanation: ..
2024.10.14 -
Valid Palindrome
SolutionA phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.Given a string s, return true if it is a palindrome, or false otherwise. Examples Explanationclass Solution { public boolean isPalindrome(String s) { S..
2024.10.11 -
Valid Anagram
SolutionGiven two strings s and t, return true if t is an anagram of s, and false otherwise. Examples Constraints:- 1 - s and t consist of lowercase English letters. Explanationclass Solution { public boolean isAnagram(String s, String t) { char[] sArr = s.toCharArray(); char[] tArr = t.toCharArray(); Arrays.sort(sArr); Arrays.sort(tArr); return Arra..
2024.10.10 -
First Unique Character in a String
SolutionGiven a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1. ExamplesExample 1:Input: s = "leetcode"Output: 0Explanation: The character 'l' at index 0 is the first character that does not occur at any other index. Example 2:Input: s = "loveleetcode"Output: 2 Example 3:Input: s = "aabb"Output: -1 Explanationclass Solution { publi..
2024.10.08 -
Reverse Integer
SolutionGiven a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.Assume the environment does not allow you to store 64-bit integers (signed or unsigned). ExamplesExample 1:Input: x = 123Output: 321 Example 2:Input: x = -123Output: -321 Example 3:Input: x = 120Output: 21 Explana..
2024.10.07