每一步向前都是向自己的夢想更近一步,堅持不懈,勇往直前!
第一題:11. 盛最多水的容器 - 力扣(LeetCode)
class Solution {public int maxArea(int[] height) {//這道題比較特殊,因為兩邊是任意選的,//所以中間可以出現比兩邊高的情況,實際就是雙指針求一個矩形的面積//寬度為左右邊界的差值,高度為左右高度的較小值,所以我們進行模擬int l = 0, r = height.length - 1, maxres = 0;while(l < r){if(maxres < (r - l) * Math.min(height[l], height[r])){maxres = (r - l) * Math.min(height[l], height[r]);}//不斷挪動邊界,因為寬度是在不斷縮小的,那么如果我們挪動較高的那邊,//得到的結果一定比以前小,所以挪動較低的那一邊if(height[l] <= height[r]){l++;}else{r--;}}return maxres;}
}
第二題:12. 整數轉羅馬數字 - 力扣(LeetCode)
class Solution {// 數值對應的羅馬數字int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};// 羅馬數字String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};// 將整數轉換為羅馬數字public String intToRoman(int num) {StringBuilder sb = new StringBuilder();// 遍歷數值對應的數組for (int i = 0; i < values.length; ++i) {int value = values[i];String symbol = symbols[i];// 當 num 大于等于當前數值時,循環減去該數值,并將對應的羅馬數字追加到結果中while (num >= value) {num -= value;sb.append(symbol);}// 如果 num 等于 0,表示已經轉換完成,直接跳出循環if (num == 0) {break;}}// 返回轉換后的羅馬數字字符串return sb.toString();}
}
第三題:13. 羅馬數字轉整數 - 力扣(LeetCode)
class Solution {// 將羅馬數字轉換為整數public int romanToInt(String s) {int res = 0; // 結果變量,用于存儲轉換后的整數值int preNum = getValue(s.charAt(0)); // 前一個羅馬數字對應的整數值// 遍歷字符串 s 中的每個字符for(int i = 1; i < s.length(); i++){int num = getValue(s.charAt(i)); // 當前字符對應的整數值// 如果前一個字符對應的整數值小于當前字符對應的整數值,則將結果減去前一個字符對應的整數值if(preNum < num){res -= preNum;}else{ // 否則,將結果加上前一個字符對應的整數值res += preNum;}preNum = num; // 更新前一個字符對應的整數值為當前字符對應的整數值}res += preNum; // 加上最后一個字符對應的整數值return res; // 返回最終的整數值}// 獲取字符對應的整數值private int getValue(char ch){switch(ch) {case 'I' : return 1;case 'V' : return 5;case 'X': return 10;case 'L': return 50;case 'C': return 100;case 'D': return 500;case 'M': return 1000;default: return 0;}}
}
第四題:14. 最長公共前綴 - 力扣(LeetCode)
import java.util.Arrays;
import java.util.Comparator;class Solution {// 尋找字符串數組中的最長公共前綴public String longestCommonPrefix(String[] strs) {// 根據字符串長度對數組進行排序Arrays.sort(strs, new Comparator<String>(){@Overridepublic int compare(String o1, String o2){return o1.length() - o2.length();}});// 獲取最短字符串的長度int maxlen = strs[0].length();int res = 0, index = 0; // res: 公共前綴長度,index: 當前比較的字符索引// 遍歷最短字符串的長度while(index < maxlen){char tmpch = '.'; // 臨時變量,用于存儲當前比較的字符,默認為'.'表示未初始化// 遍歷字符串數組中的每個字符串for(String st : strs){// 如果臨時字符是'.',則將當前字符作為臨時字符if(tmpch == '.'){tmpch = st.charAt(index);}// 否則,如果當前字符與臨時字符不相等,則返回公共前綴(如果 res 不為 0),否則返回空字符串else{if(st.charAt(index) != tmpch){return res != 0 ? strs[0].substring(0, res) : "";}}}index++; // 移動到下一個字符位置res++; // 更新公共前綴長度}// 返回最長公共前綴return strs[0].substring(0, res);}
}
?第五題:15. 三數之和 - 力扣(LeetCode)
class Solution {// 定義一個方法threeSum,接收一個整數數組nums作為參數,返回所有和為0的三元組public List<List<Integer>> threeSum(int[] nums) {// 初始化結果列表List<List<Integer>> res = new ArrayList<>();// 對數組進行排序,以便于后續的雙指針操作Arrays.sort(nums);// 遍歷數組,使用i作為第一個數字for(int i = 0; i < nums.length - 2; i++) {// 跳過相同的元素,避免重復的三元組if(i > 0 && nums[i] == nums[i - 1]) {continue;}// 初始化左右指針int j = i + 1, k = nums.length - 1;// 使用while循環進行雙指針操作,尋找和為0的三元組while(j < k) {// 計算當前三元組的和int cursum = nums[i] + nums[j] + nums[k];// 如果當前和小于0,說明需要增大和,因此移動左指針jif(cursum < 0) {j++;} else if(cursum > 0) {// 如果當前和大于0,說明需要減小和,因此移動右指針kk--;} else {// 如果當前和為0,找到了一個三元組res.add(Arrays.asList(nums[i], nums[j], nums[k]));// 跳過相同的元素,避免重復的三元組while(j < k && nums[j] == nums[j + 1]) {j++;}while(j < k && nums[k] == nums[k - 1]) {k--;}// 移動指針繼續尋找下一個可能的三元組j++;k--;}}}// 返回所有找到的三元組return res;}
}