个人刷题总结

发布于: 2020 年 07 月 07 日
1. 二叉树剪枝 M

https://leetcode-cn.com/problems/binary-tree-pruning/

track:需要再次复习

public TreeNode pruneTree(TreeNode root) {
if (root == null) {
return root;
}
root.left = pruneTree(root.left);
root.right = pruneTree(root.right);
if (root.val == 0 && root.left == null && root.right == null) {
root = null;
}
return root;
}
2. 存在重复元素 E

https://leetcode-cn.com/problems/contains-duplicate/

track:使用Set的数据唯一性

  public boolean containsDuplicate(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if (!set.add(nums[i])) {
                return true;
            }
        }
        return false;
    }
3. 只出现一次的数字 E

https://leetcode-cn.com/problems/single-number/

track:位运算

    public int singleNumber(int[] nums) {
        int res = nums[0];
        for (int i = 1; i < nums.length; i++) {
            res = res ^ nums[i];
        }
        return res;
    }
4. 反转字符串 E

https://leetcode-cn.com/problems/reverse-string/

track:左右双指针

    public void reverseString(char[] s) {
        int start = 0;
        int end = s.length - 1;
        while (start < end) {
            char temp = s[start];
            s[start] = s[end];
            s[end] = temp;
            start ++;
            end --;
        }
    }
5. 字符串中的第一个唯一字符 E

https://leetcode-cn.com/problems/first-unique-character-in-a-string/

track:用映射

public int firstUniqChar(String s) {
int[] temp = new int[26];
for (int i = 0; i < s.length(); i++) {
temp[s.charAt(i) - 'a'] ++;
}
for (int i = 0; i < s.length(); i++) {
if (temp[s.charAt(i) - 'a'] == 1) {
return i;
}
}
return - 1;
}
6. 验证回文串 E

https://leetcode-cn.com/problems/valid-palindrome/

track:左右双指针

public boolean isPalindrome(String s) {
int start = 0;
int end = s.length() - 1;
String s1 = s.toUpperCase();
while (start < end) {
char cS = s1.charAt(start);
char cE = s1.charAt(end);
if (!(Character.isDigit(cE) || Character.isUpperCase(cE))) {
end --;
continue;
}
if (!(Character.isDigit(cS) || Character.isUpperCase(cS))) {
start ++;
continue;
}
if (cE != cS) {
return false;
}
start ++;
end --;
}
return true;
}
7. 实现strStr()

https://leetcode-cn.com/problems/implement-strstr/

track:复习JDK中对indexOf的实现方式

public int strStr(String haystack, String needle) {
return haystack.indexOf(needle);
}
8. 最长公共前缀 E

https://leetcode-cn.com/problems/longest-common-prefix/

public String longestCommonPrefix(String[] strs) {
if (strs.length == 0) return "";
String temp = strs[0];
for (int i = 1; i < strs.length; i++) {
temp = compare(temp, strs[i]);
}
return temp;
}
private String compare(String temp, String temp2) {
int min = Math.min(temp.length(), temp2.length());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < min; i++) {
if (temp.charAt(i) == temp2.charAt(i)) {
sb.append(temp.charAt(i));
}else {
break;
}
}
return sb.toString();
}
9. 删除链表的倒数第N个节点

https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/

track:添加一个头结点

public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode newHead = new ListNode();
newHead.next = head;
int step = n - 1;
ListNode forward = newHead;
ListNode slow = newHead;
ListNode first = newHead;
while (forward.next != null) {
forward = forward.next;
if (step != 0) {
step --;
} else {
first = slow;
slow = slow.next;
}
}
first.next = slow.next;
return newHead.next;
}
10. 整数反转

https://leetcode-cn.com/problems/reverse-integer/

public int reverse(int x) {
boolean temp = x < 0;
StringBuilder stringBuilder = new StringBuilder(String.valueOf(x));
if (temp) {
stringBuilder.deleteCharAt(0);
}
stringBuilder.reverse();
if (temp) {
stringBuilder.insert(0,'-');
}
int res = 0;
try {
res = Integer.valueOf(stringBuilder.toString());
} catch (NumberFormatException e) {
System.out.println("反转后数字过大");
}
return res;
}
11. FizzBuzz

https://leetcode-cn.com/problems/fizz-buzz/

public List<String> fizzBuzz(int n) {
List<String> list = new ArrayList<>();
for (int i = 1; i <= n; i++) {
StringBuilder sb = new StringBuilder();
boolean temp = false;
if (i % 3 == 0) {
sb.append("Fizz");
temp = true;
}
if (i % 5 == 0) {
sb.append("Buzz");
temp = true;
}
if (!temp) {
sb.append(i);
}
list.add(sb.toString());
}
return list;
}

用户头像

Nano

关注

还未添加个人签名 2020.05.28 加入

还未添加个人简介

评论

发布
暂无评论
个人刷题总结