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 co...
https://oj.leetcode.com/discuss/3577/i-use-quick-sort-to-sort-the-list-but-why-i-get-time-limited View Code GITHUB: https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/list/SortList.java
leetcode:合并链表 func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? { var tmp1 = l1 var tmp2 = l2 var stepper = 0 var destListNode: ListNode? while tmp1 != nil || tmp2 != nil { var dest = 0 if(tmp1 != nil){ dest = dest + tmp1!.val tmp1 = tmp1...
publicclassSort{publicstaticvoidmain(String[]args){int[]unsortedArray=newint[]{6,5,3,1,8,7,2,4};bubbleSort(unsortedArray);System.out.println("After sort: ");for(intitem:unsortedArray){System.out.print(item+" ");}}publicstaticvoidbubbleSort(int[]nums){intlen=nums.length;for(inti=0;...
1. Leetcode 75 Sort Colors Given an array with n objects colored red, white or blue, sort them in-place 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, whi...
1classSolution {2publicString frequencySort(String s) {3HashMap<Character, Integer> map =newHashMap<>();4for(charc : s.toCharArray()) {5map.put(c, map.getOrDefault(c, 0) + 1);6}78List<Character>[] bucket =newList[s.length() + 1];9for(charkey : map.keySet()) {10intfreq...
遍历一遍的方法:https://leetcode.com/discuss/17000/share-my-one-pass-constant-space-10-line-solution the idea is to sweep all 0s to the left and all 2s to the right, then all 1s are left in the middle. classSolution {public:voidsortColors(intA[],intn) {intsecond=n-1, zero=0;f...
//compare参数实现,匿名函数lambda,这种比较方法的可行性ASCII码的比较,0对应dec的48 in ASCII中美国标准信息交换代码ANSI制定 string res; for(auto s:arr) res+=s; while(res[0]=='0' && res.length()>1) res.erase(0,1); /*erase函数的原型如下: ...
https://leetcode.com/problems/sort-colors/#/description 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 ...