[关闭]
@Yano 2015-12-30T11:26:29.000000Z 字数 5866 阅读 6013

LeetCode之Sort题目汇总

LeetCode

我的博客:http://blog.csdn.net/yano_nankai
LeetCode题解:https://github.com/LjyYano/LeetCode


LeetCode 题目汇总

LeetCode之Array题目汇总
LeetCode之Hash Table题目汇总
LeetCode之Linked List题目汇总
LeetCode之Math题目汇总
LeetCode之String题目汇总
LeetCode之Binary Search题目汇总
LeetCode之Divide and Conquer题目汇总
LeetCode之Dynamic Programming题目汇总
LeetCode之Backtracing题目汇总
LeetCode之Stack题目汇总
LeetCode之Sort题目汇总
LeetCode之Bit Manipulation题目汇总
LeetCode之Tree题目汇总
LeetCode之Depth-first Search题目汇总
LeetCode之Breadth-first Search题目汇总
LeetCode之Graph题目汇总
LeetCode之Trie题目汇总
LeetCode之Design题目汇总


文章目录:


H-Index

Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.

According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."

For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively. Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, his h-index is 3.

Note: If there are several possible values for h, the maximum one is taken as the h-index.

Hint:

  1. An easy approach is to sort the array first.
  2. What are the possible values of h-index?
  3. A faster approach is to use extra space.

Credits: 
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.


题目描述不清,实际上,H-Index的核心计算方法是:

1、将某作者的所有文章的引用频次按照从大到小的位置排列

2、从前到后,找到最后一个满足条件的位置,其条件为:此位置是数组的第x个,其值为y,必须满足 y >= x;

  1. public int hIndex(int[] citations) {
  2. if (citations == null) {
  3. return 0;
  4. }
  5. int h = 0, n = citations.length;
  6. Arrays.sort(citations);
  7. for (int i = n - 1; i >= 0; i--) {
  8. if (citations[i] >= n - i) {
  9. h = n - i;
  10. } else {
  11. break;
  12. }
  13. }
  14. return h;
  15. }

H-Index II

Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?

Hint:

  1. Expected runtime complexity is in O(log n) and the input is sorted.

二分查找

  1. public int hIndex(int[] citations) {
  2. int n = citations.length;
  3. int low = 0, high = n - 1;
  4. while (low <= high) {
  5. int mid = low + (high - low) / 2;
  6. if (citations[mid] == n - mid) {
  7. return n - mid;
  8. } else if (citations[mid] < n - mid) {
  9. low = mid + 1;
  10. } else {
  11. high = mid - 1;
  12. }
  13. }
  14. return n - low;
  15. }

Insertion Sort List

Sort a linked list using insertion sort.


  1. public ListNode insertionSortList(ListNode head) {
  2. if (head == null)
  3. return null;
  4. if (head.next == null)
  5. return head;
  6. final ListNode _head = new ListNode(Integer.MIN_VALUE);
  7. _head.next = head;
  8. head = head.next;
  9. _head.next.next = null;
  10. next: while (head != null) {
  11. ListNode taken = head;
  12. head = head.next;
  13. ListNode cur = _head.next;
  14. ListNode last = _head;
  15. while (cur != null) {
  16. if (cur.val > taken.val) {
  17. // insert
  18. last.next = taken;
  19. taken.next = cur;
  20. continue next;
  21. }
  22. cur = cur.next;
  23. last = last.next;
  24. }
  25. last.next = taken;
  26. taken.next = null;
  27. }
  28. return _head.next;
  29. }

Largest Number

Given a list of non negative integers, arrange them such that they form the largest number.

For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.

Note: The result may be very large, so you need to return a string instead of an integer.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.


  1. [3, 30, 34, 5, 9]
  2. 排序成……
  3. [9, 5, 34, 3, 30]

如何判断大小?对于每个元素,左边第一位大的在前面,如5>30。因为5的第一位是5是5,30的第一位是3。依次比较。

那么3应该比30大,因为3 + 30 = 330 ,而30 + 3 = 303。

所以本题可分为4步:

  1. 定义string数组,将int数组,转成string数组
  2. 对string数组按照定义的规则排序
  3. 如果strs第一个元素是“0”,则结果是0
  4. 连接strs数组成字符串,即为结果
  1. public String largestNumber(int[] nums) {
  2. String[] strs = new String[nums.length];
  3. // 将int数组,转成string数组
  4. for (int i = 0; i < strs.length; i++) {
  5. strs[i] = nums[i] + "";
  6. }
  7. // 对strs排序
  8. Arrays.sort(strs, new Comparator<String>() {
  9. public int compare(String x, String y) {
  10. return (y + x).compareTo(x + y);
  11. }
  12. });
  13. // 如果strs第一个元素是“0”,则结果是0
  14. if ("0".equals(strs[0])) {
  15. return "0";
  16. }
  17. // 连接strs数组成字符串
  18. return String.join("", strs);
  19. }

Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Note: 
You are not suppose to use the library's sort function for this problem.

click to show follow up.

Follow up: 
A rather straight forward solution is a two-pass algorithm using counting sort. 
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.

Could you come up with an one-pass algorithm using only constant space?


  1. public void sortColors(int[] nums) {
  2. if (nums == null || nums.length == 0) {
  3. return;
  4. }
  5. // 定义三个变量,存储三种颜色出现次数
  6. int red = 0;
  7. int white = 0;
  8. int blue = 0;
  9. // 循环一次,记录每种颜色出现次数
  10. for (int i = 0; i < nums.length; i++) {
  11. if (nums[i] == 0) {
  12. red++;
  13. } else if (nums[i] == 1) {
  14. white++;
  15. } else {
  16. blue++;
  17. }
  18. }
  19. // 对nums数组重新赋值
  20. int i = 0;
  21. while (red-- > 0) {
  22. nums[i++] = 0;
  23. }
  24. while (white-- > 0) {
  25. nums[i++] = 1;
  26. }
  27. while (blue-- > 0) {
  28. nums[i++] = 2;
  29. }
  30. }

Sort List

Sort a linked list in O(n log n) time using constant space complexity.


O(n log n) 的时间复杂度,归并排序最好,因为它不会因为输入序列的基本有序而变化。

参考:LeetCode 021 Merge Two Sorted Lists

  1. 首先将List分成两个
  2. MergeSort(left) ,MegerSort(right)
  3. LeetCode 021 Merge Two Sorted Lists
  1. public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
  2. ListNode rt = new ListNode(0);
  3. ListNode h = rt;
  4. while (l1 != null && l2 != null) {
  5. if (l1.val < l2.val) {
  6. rt.next = l1;
  7. l1 = l1.next;
  8. } else {
  9. rt.next = l2;
  10. l2 = l2.next;
  11. }
  12. rt = rt.next;
  13. }
  14. if (l1 != null)
  15. rt.next = l1;
  16. else
  17. rt.next = l2;
  18. return h.next;
  19. }
  20. public ListNode sortList(ListNode head) {
  21. if (head == null)
  22. return null;
  23. if (head.next == null)
  24. return head;
  25. ListNode fast = head.next;
  26. ListNode slow = head;
  27. while (fast != null && fast.next != null) {
  28. slow = slow.next;
  29. fast = fast.next.next;
  30. }
  31. ListNode h2 = slow.next;
  32. slow.next = null;
  33. return mergeTwoLists(sortList(head), sortList(h2));
  34. }

Valid Anagram

Given two strings s and t, write a function to determine if t is an anagram of s.

For example, 
s = "anagram", t = "nagaram", return true. 
s = "rat", t = "car", return false.

Note: 
You may assume the string contains only lowercase alphabets.

Follow up: 
What if the inputs contain unicode characters? How would you adapt your solution to such case?


用数组模拟哈希表,统计字符出现次数。

  1. public boolean isAnagram(String s, String t) {
  2. if (s == null || t == null) {
  3. return false;
  4. }
  5. if (s.length() != t.length()) {
  6. return false;
  7. }
  8. if (s.equals(t)) {
  9. return false;
  10. }
  11. int len = s.length();
  12. int[] map = new int[26];
  13. // 统计s中每个字符出现的次数
  14. for (int i = 0; i < len; i++) {
  15. map[s.charAt(i) - 'a']++;
  16. }
  17. // 减去t中相应字符出现次数
  18. for (int i = 0; i < len; i++) {
  19. map[t.charAt(i) - 'a']--;
  20. if (map[t.charAt(i) - 'a'] < 0) {
  21. return false;
  22. }
  23. }
  24. for (int i = 0; i < 26; i++) {
  25. if (map[i] != 0) {
  26. return false;
  27. }
  28. }
  29. return true;
  30. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注