@@ -629,9 +629,12 @@ public int[] twoSum(int[] numbers, int target) {
629
629
int i = 0 , j = numbers. length - 1 ;
630
630
while (i < j) {
631
631
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-- ;
635
638
}
636
639
return null ;
637
640
}
@@ -654,9 +657,12 @@ public boolean judgeSquareSum(int c) {
654
657
int i = 0 , j = (int ) Math . sqrt(c);
655
658
while (i <= j) {
656
659
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++ ;
660
666
}
661
667
return false ;
662
668
}
@@ -681,11 +687,11 @@ public String reverseVowels(String s) {
681
687
while (i <= j) {
682
688
char ci = s. charAt(i);
683
689
char cj = s. charAt(j);
684
- if (! vowels. contains(ci)) {
690
+ if (! vowels. contains(ci))
685
691
result[i++ ] = ci;
686
- } else if (! vowels. contains(cj)) {
692
+ else if (! vowels. contains(cj))
687
693
result[j-- ] = cj;
688
- } else {
694
+ else {
689
695
result[i++ ] = cj;
690
696
result[j-- ] = ci;
691
697
}
@@ -709,20 +715,18 @@ Explanation: You could delete the character 'c'.
709
715
``` java
710
716
public boolean validPalindrome(String s) {
711
717
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))
714
720
return isPalindrome(s, i, j - 1 ) || isPalindrome(s, i + 1 , j);
715
- }
716
- }
721
+
717
722
return true ;
718
723
}
719
724
720
725
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-- ))
723
728
return false ;
724
- }
725
- }
729
+
726
730
return true ;
727
731
}
728
732
```
@@ -743,13 +747,18 @@ Output: [1,2,2,3,5,6]
743
747
744
748
``` java
745
749
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 ;
747
752
int index = m + n - 1 ;
748
753
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-- ];
753
762
index-- ;
754
763
}
755
764
}
@@ -763,10 +772,12 @@ public void merge(int[] nums1, int m, int[] nums2, int n) {
763
772
764
773
``` java
765
774
public boolean hasCycle(ListNode head) {
766
- if (head == null ) return false ;
775
+ if (head == null )
776
+ return false ;
767
777
ListNode l1 = head, l2 = head. next;
768
778
while (l1 != null && l2 != null && l2. next != null ) {
769
- if (l1 == l2) return true ;
779
+ if (l1 == l2)
780
+ return true ;
770
781
l1 = l1. next;
771
782
l2 = l2. next. next;
772
783
}
@@ -793,22 +804,19 @@ public String findLongestWord(String s, List<String> d) {
793
804
String longestWord = " " ;
794
805
for (String target : d) {
795
806
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 ))
797
808
continue ;
798
- }
799
- if (isValid(s, target)) {
809
+ if (isValid(s, target))
800
810
longestWord = target;
801
- }
802
811
}
803
812
return longestWord;
804
813
}
805
814
806
815
private boolean isValid(String s, String target) {
807
816
int i = 0 , j = 0 ;
808
817
while (i < s. length() && j < target. length()) {
809
- if (s. charAt(i) == target. charAt(j)) {
818
+ if (s. charAt(i) == target. charAt(j))
810
819
j++ ;
811
- }
812
820
i++ ;
813
821
}
814
822
return j == target. length();
0 commit comments