LeetCode 905. Sort Array By Parity && LeetCode 824. Goat Latin && LeetCode
LC 905 : Sort Array By Parity
Description:
Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.
You may return any answer array that satisfies this condition.
Example 1:
Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
Note:
1 <= A.length <= 5000
0 <= A[i] <= 5000
Solution:
public int[] sortArrayByParity(int[] A) { int[] ans = new int[A.length]; int t = 0; int low = 0, high = A.length - 1; for (int i = 0; i < A.length; ++i) { if (A[i] % 2 ==0) ans[low++] = A[i]; else ans[high--] = A[i]; } return ans; }
LC 824 Goat Latin
Description:
A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only.
We would like to convert the sentence to “Goat Latin” (a made-up language similar to Pig Latin.)
The rules of Goat Latin are as follows:
If a word begins with a vowel (a, e, i, o, or u), append “ma” to the end of the word.
For example, the word ‘apple’ becomes ‘applema’.
If a word begins with a consonant (i.e. not a vowel), remove the first letter and append it to the end, then add “ma”.
For example, the word “goat” becomes “oatgma”.
Add one letter ‘a’ to the end of each word per its word index in the sentence, starting with 1.
For example, the first word gets “a” added to the end, the second word gets “aa” added to the end and so on.
Return the final sentence representing the conversion from S to Goat Latin.
Example 1:
Input: “I speak Goat Latin”
Output: “Imaa peaksmaaa oatGmaaaa atinLmaaaaa”
Example 2:
Input: “The quick brown fox jumped over the lazy dog”
Output: “heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa”
Solution:
public String toGoatLatin(String S) { Set<Character> set = new HashSet<>(); set.add('a'); set.add('e'); set.add('i'); set.add('o'); set.add('u'); set.add('A'); set.add('E'); set.add('I'); set.add('O'); set.add('U'); // 1 represents the vowel ; 2 represents the consonant int flag = 0; char tem = ' '; int index = 1; StringBuilder sb = new StringBuilder(); for (int i = 0; i < S.length(); ++i) { if (S.charAt(i) == ' ' || i == S.length() - 1) { if (i == S.length() - 1) sb.append(S.charAt(i)); if (flag == 2) sb.append(tem); flag = 0; sb.append("ma"); for(int j = 1; j <= index; ++j) sb.append('a'); index++; if (i != S.length() - 1) sb.append(" "); } else { if (flag == 0) { if (set.contains(S.charAt(i))) { flag = 1; } else { flag = 2; tem = S.charAt(i); } sb.append(flag == 1 ? S.charAt(i) : ""); continue; } sb.append(S.charAt(i)); } } return sb.toString(); }
- 905.[leetcode]C++实现按奇偶排序数组(Sort Array By Parity)
- LeetCode 905. Sort Array By Parity
- Leetcode 905. Sort Array By Parity, Java解法
- [leetcode] 905. Sort Array By Parity [easy]
- Leetcode——905. Sort Array By Parity
- Leetcode 905 Sort Array By Parity
- Leetcode 905 Sort Array By Parity
- Golang Leetcode 905. Sort Array By Parity.go
- LeetCode 905. Sort Array By Parity python
- 905. Sort Array By Parity
- 905. Sort Array By Parity。
- 905. Sort Array By Parity
- [parallelg刷Leetcode] 902 Sort Array By Parity
- 922. Sort Array By Parity II
- Sort Transformed Array -- LeetCode
- 不同IComparer对数组排序Array.Sort,Linq orderby的性能的影响
- .sortEntities()is called this array is sorted by the entities.zIndex.
- 深入聊聊Array的sort方法的使用技巧.详细点评protype.js中的sortBy方法
- LeetCode88 Merge two sort array
- Leetcode May Challenge - 05/22: Sort Characters By Frequency(Python)