2021-08-03 21:15:22 星期二java
https://leetcode-cn.com/problems/fibonacci-number/submissions/git
class Solution { public int fib(int n) { if(n < 2){ return n; } int f = 0, s = 1; for(int i = 2; i <= n; i++){ int t = f + s; f = s; s = t; } return s; } }
https://www.nowcoder.com/practice/6fe361ede7e54db1b84adc81d09d8524?tpId=13&tqId=11203&tab=answerKey&from=cyc_github
大佬總結:
https://github.com/CyC2018/CS-Notes/blob/master/notes/3. 數組中重複的數字.mdgithub
import java.util.*; public class Solution { /** * 代碼中的類名、方法名、參數名已經指定,請勿修改,直接返回方法規定的值便可 * * * @param numbers int整型一維數組 * @return int整型 */ //方法一 public int duplicate (int[] numbers) { int[] tmp = new int[numbers.length]; for(int i = 0; i < numbers.length; i++){ if(tmp[numbers[i]] > 0){ return numbers[i]; } tmp[numbers[i]]++; } return -1; } //方法二 public int duplicate (int[] numbers) { for(int i = 0; i < numbers.length; i++){ while(i != numbers[i]){ if(numbers[i] == numbers[numbers[i]]){ return numbers[i]; } int t = numbers[i]; numbers[i] = numbers[numbers[i]]; numbers[t] = t; } } return -1; } }
2021-08-04 21:32:21 星期三數組
class Solution { public int[] twoSum(int[] numbers, int target) { int i = 0, j = numbers.length - 1; while (i < j){ if (numbers[i] + numbers[j] == target) return new int[] {i + 1, j + 1}; if (numbers[i] + numbers[j] > target){ j--; continue; }else { i++; continue; } } return null; } }
class Solution { public boolean judgeSquareSum(int c) { int sqrt = (int) Math.sqrt(c); int i = 0, j = sqrt; while (i <= j) { if (i*i + j*j == c) return true; if (i*i + j*j > c){ j--; }else { i++; } } return false; } }
class Solution { public String reverseVowels(String s) { List<Character> t = Arrays.asList('a', 'o', 'e', 'i', 'u', 'A', 'O', 'E', 'I' ,'U'); char[] array = s.toCharArray(); int i = 0, j = array.length - 1; while (i <= j){ if (t.contains(array[i]) && t.contains(array[j])){ char tmp = array[i]; array[i] = array[j]; array[j] = tmp; i++; j--; }else if (t.contains(array[i])){ j--; }else if (t.contains(array[j])){ i++; }else { i++; j--; } } return new String(array); } }
2021-08-05 23:14:32 星期四3d
class Solution { public boolean validPalindrome(String s) { if (s == null) return false; if (s.length() < 2) return true; char[] array = s.toCharArray(); int i = 0, j = array.length - 1; while (i < j) { if (array[i] == array[j]) { i++; j--; continue; } else { return isPalindrome(new String(array, i, j - i)) || isPalindrome(new String(array, i + 1, j - i)); } } return true; } public boolean isPalindrome(String s) { int i = 0, j = s.length() - 1; while (i < j) { if (s.charAt(i) != s.charAt(j)) return false; i++; j--; } return true; } }
class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int i = m - 1, j = n - 1, k = m + n - 1; while (i >= 0 && j >= 0){ if (nums1[i] >= nums2[j]){ nums1[k--] = nums1[i--]; }else { nums1[k--] = nums2[j--]; } } if (i >= 0){ while (k >= 0){ nums1[k--] = nums1[i--]; } }else { while (k >= 0){ nums1[k--] = nums2[j--]; } } } }
public class Solution { public boolean hasCycle(ListNode head) { if (head == null || head.next == null) return false; ListNode h1 = head, h2 = head.next; while (h1 != null && h2 != null && h2.next != null){ if (h1 == h2) return true; h1 = h1.next; h2 = h2.next.next; } return false; } }
2021-08-07 22:49:23 星期六code
class Solution { public String findLongestWord(String s, List<String> dictionary) { if(dictionary == null) return ""; Collections.sort(dictionary); int i = 0, max = 0; String result = ""; while (i < dictionary.size()){ if (isSubStr(s, dictionary.get(i)) && dictionary.get(i).length() > max){ max = dictionary.get(i).length(); result = dictionary.get(i); } i++; } return result; } private boolean isSubStr(String s, String target){ int i = 0, j = 0; while (i < s.length() && j < target.length()){ if (s.charAt(i) == target.charAt(j)){ j++; } i++; } return j == target.length(); } }
class Solution { public int findKthLargest(int[] nums, int k) { Arrays.sort(nums); return nums[nums.length - k]; } }
2021-08-08 10:57:32 星期日blog
class Solution { public boolean isValid(String s) { char[] array = s.toCharArray(); Stack<Character> stack = new Stack<>(); for (char c : array) { if (c == '(' || c == '[' || c == '{'){ stack.push(c); }else { if (stack.isEmpty()) return false; switch (stack.pop()){ case '(': if (c != ')') { return false; } break; case '[': if (c != ']'){ return false; } break; case '{': if (c != '}'){ return false; } break; } } } return stack.isEmpty(); } }
2021-08-09 21:10:23 星期一隊列
class MyQueue { Stack<Integer> inStack = new Stack<>(); Stack<Integer> outStack = new Stack<>(); /** Initialize your data structure here. */ public MyQueue() { } /** Push element x to the back of queue. */ public void push(int x) { inStack.push(x); } /** Removes the element from in front of queue and returns that element. */ public int pop() { if (!outStack.isEmpty()) { return outStack.pop(); } while (!inStack.isEmpty()) { outStack.push(inStack.pop()); } return outStack.pop(); } /** Get the front element. */ public int peek() { if (!outStack.isEmpty()) { return outStack.peek(); } while (!inStack.isEmpty()) { outStack.push(inStack.pop()); } return outStack.peek(); } /** Returns whether the queue is empty. */ public boolean empty() { return outStack.isEmpty() && inStack.isEmpty(); } }
class Solution { public int mySqrt(int x) { if (x < 2) return x; int left = 1, right = x; while (left <= right){ int med = (left + right)/ 2; int sqrt = x / med; if (sqrt == med){ return med; }else if (sqrt > med){ left = med + 1; }else { right = med - 1; } } return right; } }