leetCode(Using C)——760. Find Anagram Mappings
Description:
Given two lists Aand B, and B is an anagram of A. B is an anagram of A means B is made by randomizing the order of the elements in A.
We want to find an index mapping P, from A to B. A mapping P[i] = j means the ith element in A appears in B at index j.
These lists A and B may contain duplicates. If there are multiple answers, output any of them.
Example:
given:
A = [12, 28, 46, 32, 50]
B = [50, 12, 32, 46, 28]
We should return
[1, 4, 3, 2, 0]
as P[0] = 1 because the 0th element of A appears at B[1], and P[1] = 4 because the 1st element of A appears at B[4], and so on.
Note:
A, B have equal lengths in range [1, 100].
A[i], B[i] are integers in range [0, 10^5].
Link:
If you want to solve the problem, you can visite the web site.click me
Code:
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* anagramMappings(int* A, int ASize, int* B, int BSize, int* returnSize) {
int *p;
p = (int *)malloc(sizeof(int)*ASize);
int flag[ASize];
memset(flag, 0, sizeof(flag));
*returnSize = ASize;
for(int i=0; i<ASize; i++){
for(int j=0; j<BSize; j++){
if(A[i]==B[j]&&flag[j]==0){
p[i]=j;
// printf("p[%d]=%d\n",i,j);
flag[j]=1;
break; //一定別忘了break!
}
}
}
return p;
}
感想
刷題一定要看清題目讓你返回什麼!博主一開始就寫對了,但是就是在提交的時候一直結果都是空,不知道哪個地方有問題。後來仔細的研究了以下程式碼前面的幾句說明性的話。
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
第一句跟你說,讓你將返回的陣列大小放在returnSize中。第二句讓你動態的分配陣列,不要直接用定義陣列的方法。注意到這兩點,就很簡單了。
相關文章
- [LeetCode] Group AnagramLeetCode
- Leetcode Valid AnagramLeetCode
- 242. Valid Anagram--LeetCode RecordLeetCode
- leetCode(Using C)——657. Judge Route CircleLeetCode
- 力扣760. 找出變位對映 C++力扣C++
- leetCode(Using C)——461. Hamming DistanceLeetCode
- How to Find Out How Much Space an Index is UsingIndex
- leetCode(Using C)——718. Maximum Length of Repeated SubarrayLeetCode
- [LeetCode] Find the Duplicate NumberLeetCode
- LeetCode-Find the CelebrityLeetCode
- Find Peak element leetcodeLeetCode
- LeetCode 389. Find the DifferenceLeetCode
- LeetCode-Find the Duplicate NumberLeetCode
- Leetcode-Find Peak ElementLeetCode
- [Javascript] Find Items from the end of the JavaScript Array using at, findLast and findLastIndexJavaScriptASTIndex
- Leetcode Implement Queue using StacksLeetCode
- [LeetCode] 277. Find the CelebrityLeetCode
- [LeetCode] 724. Find Pivot IndexLeetCodeIndex
- [leetcode] 890. Find and Replace PatternLeetCode
- leetcode287: Find the Duplicate NumberLeetCode
- Using MongoDB in C#MongoDBC#
- LeetCode-Implement Stack Using QueuesLeetCode
- LeetCode-Implement Queue using StacksLeetCode
- [LeetCode] Find First and Last Position of Element in SortedLeetCodeAST
- Leetcode 442. Find All Duplicates in an ArrayLeetCode
- [leetcode] 1394. Find Lucky Integer in an ArrayLeetCode
- LeetCode-Find K Pairs with Smallest SumsLeetCodeAI
- LeetCode 題解(252) : Find the Duplicate NumberLeetCode
- 09 對映(mappings)APP
- Oracle GoldenGate – MappingsOracleGoAPP
- LeetCode | 232 Implement Queue Using StacksLeetCode
- LeetCode之Find Common Characters(Kotlin)LeetCodeKotlin
- LeetCode 438. Find All Anagrams in a StringLeetCode
- LeetCode-Find Median from Data StreamLeetCode
- Find Minimum in Rotated Sorted Array leetcode javaLeetCodeJava
- Leetcode-Find Minimum in Rotated Sorted ArrayLeetCode
- [LeetCode] 2831. Find the Longest Equal SubarrayLeetCode
- C#中using的使用C#