Skip to content

Commit 775d62a

Browse files
committed
auto commit
1 parent 889f664 commit 775d62a

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

notes/Leetcode 题解.md

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -629,9 +629,12 @@ public int[] twoSum(int[] numbers, int target) {
629629
int i = 0, j = numbers.length - 1;
630630
while (i < j) {
631631
int sum = numbers[i] + numbers[j];
632-
if (sum == target) return new int[]{i + 1, j + 1};
633-
else if (sum < target) i++;
634-
else j--;
632+
if (sum == target)
633+
return new int[]{i + 1, j + 1};
634+
else if (sum < target)
635+
i++;
636+
else
637+
j--;
635638
}
636639
return null;
637640
}
@@ -654,9 +657,12 @@ public boolean judgeSquareSum(int c) {
654657
int i = 0, j = (int) Math.sqrt(c);
655658
while (i <= j) {
656659
int powSum = i * i + j * j;
657-
if (powSum == c) return true;
658-
if (powSum > c) j--;
659-
else i++;
660+
if (powSum == c)
661+
return true;
662+
if (powSum > c)
663+
j--;
664+
else
665+
i++;
660666
}
661667
return false;
662668
}
@@ -681,11 +687,11 @@ public String reverseVowels(String s) {
681687
while (i <= j) {
682688
char ci = s.charAt(i);
683689
char cj = s.charAt(j);
684-
if (!vowels.contains(ci)) {
690+
if (!vowels.contains(ci))
685691
result[i++] = ci;
686-
} else if (!vowels.contains(cj)) {
692+
else if (!vowels.contains(cj))
687693
result[j--] = cj;
688-
} else {
694+
else {
689695
result[i++] = cj;
690696
result[j--] = ci;
691697
}
@@ -709,20 +715,18 @@ Explanation: You could delete the character 'c'.
709715
```java
710716
public boolean validPalindrome(String s) {
711717
int i = -1, j = s.length();
712-
while (++i < --j) {
713-
if (s.charAt(i) != s.charAt(j)) {
718+
while (++i < --j)
719+
if (s.charAt(i) != s.charAt(j))
714720
return isPalindrome(s, i, j - 1) || isPalindrome(s, i + 1, j);
715-
}
716-
}
721+
717722
return true;
718723
}
719724

720725
private boolean isPalindrome(String s, int i, int j) {
721-
while (i < j) {
722-
if (s.charAt(i++) != s.charAt(j--)) {
726+
while (i < j)
727+
if (s.charAt(i++) != s.charAt(j--))
723728
return false;
724-
}
725-
}
729+
726730
return true;
727731
}
728732
```
@@ -743,13 +747,18 @@ Output: [1,2,2,3,5,6]
743747

744748
```java
745749
public void merge(int[] nums1, int m, int[] nums2, int n) {
746-
int i = m - 1, j = n - 1; // 需要从尾开始遍历,否则在 nums1 上归并得到的值会覆盖还未进行归并比较的值
750+
// 需要从尾开始遍历,否则在 nums1 上归并得到的值会覆盖还未进行归并比较的值
751+
int i = m - 1, j = n - 1;
747752
int index = m + n - 1;
748753
while (i >= 0 || j >= 0) {
749-
if (i < 0) nums1[index] = nums2[j--];
750-
else if (j < 0) nums1[index] = nums1[i--];
751-
else if (nums1[i] > nums2[j]) nums1[index] = nums1[i--];
752-
else nums1[index] = nums2[j--];
754+
if (i < 0)
755+
nums1[index] = nums2[j--];
756+
else if (j < 0)
757+
nums1[index] = nums1[i--];
758+
else if (nums1[i] > nums2[j])
759+
nums1[index] = nums1[i--];
760+
else
761+
nums1[index] = nums2[j--];
753762
index--;
754763
}
755764
}
@@ -763,10 +772,12 @@ public void merge(int[] nums1, int m, int[] nums2, int n) {
763772

764773
```java
765774
public boolean hasCycle(ListNode head) {
766-
if (head == null) return false;
775+
if (head == null)
776+
return false;
767777
ListNode l1 = head, l2 = head.next;
768778
while (l1 != null && l2 != null && l2.next != null) {
769-
if (l1 == l2) return true;
779+
if (l1 == l2)
780+
return true;
770781
l1 = l1.next;
771782
l2 = l2.next.next;
772783
}
@@ -793,22 +804,19 @@ public String findLongestWord(String s, List<String> d) {
793804
String longestWord = "";
794805
for (String target : d) {
795806
int l1 = longestWord.length(), l2 = target.length();
796-
if (l1 > l2 || (l1 == l2 && longestWord.compareTo(target) < 0)) {
807+
if (l1 > l2 || (l1 == l2 && longestWord.compareTo(target) < 0))
797808
continue;
798-
}
799-
if (isValid(s, target)) {
809+
if (isValid(s, target))
800810
longestWord = target;
801-
}
802811
}
803812
return longestWord;
804813
}
805814

806815
private boolean isValid(String s, String target) {
807816
int i = 0, j = 0;
808817
while (i < s.length() && j < target.length()) {
809-
if (s.charAt(i) == target.charAt(j)) {
818+
if (s.charAt(i) == target.charAt(j))
810819
j++;
811-
}
812820
i++;
813821
}
814822
return j == target.length();

0 commit comments

Comments
 (0)