mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -331,6 +331,34 @@ var permute = function(nums) {
|
||||
|
||||
```
|
||||
|
||||
## TypeScript
|
||||
|
||||
```typescript
|
||||
function permute(nums: number[]): number[][] {
|
||||
const resArr: number[][] = [];
|
||||
const helperSet: Set<number> = new Set();
|
||||
backTracking(nums, []);
|
||||
return resArr;
|
||||
function backTracking(nums: number[], route: number[]): void {
|
||||
if (route.length === nums.length) {
|
||||
resArr.push(route.slice());
|
||||
return;
|
||||
}
|
||||
let tempVal: number;
|
||||
for (let i = 0, length = nums.length; i < length; i++) {
|
||||
tempVal = nums[i];
|
||||
if (!helperSet.has(tempVal)) {
|
||||
route.push(tempVal);
|
||||
helperSet.add(tempVal);
|
||||
backTracking(nums, route);
|
||||
route.pop();
|
||||
helperSet.delete(tempVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### C
|
||||
|
||||
```c
|
||||
|
@ -272,7 +272,28 @@ var subsets = function(nums) {
|
||||
};
|
||||
```
|
||||
|
||||
## TypeScript
|
||||
|
||||
```typescript
|
||||
function subsets(nums: number[]): number[][] {
|
||||
const resArr: number[][] = [];
|
||||
backTracking(nums, 0, []);
|
||||
return resArr;
|
||||
function backTracking(nums: number[], startIndex: number, route: number[]): void {
|
||||
resArr.push(route.slice());
|
||||
let length = nums.length;
|
||||
if (startIndex === length) return;
|
||||
for (let i = startIndex; i < length; i++) {
|
||||
route.push(nums[i]);
|
||||
backTracking(nums, i + 1, route);
|
||||
route.pop();
|
||||
}
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
## C
|
||||
|
||||
```c
|
||||
int* path;
|
||||
int pathTop;
|
||||
|
@ -318,6 +318,28 @@ var subsetsWithDup = function(nums) {
|
||||
|
||||
```
|
||||
|
||||
### TypeScript
|
||||
|
||||
```typescript
|
||||
function subsetsWithDup(nums: number[]): number[][] {
|
||||
nums.sort((a, b) => a - b);
|
||||
const resArr: number[][] = [];
|
||||
backTraking(nums, 0, []);
|
||||
return resArr;
|
||||
function backTraking(nums: number[], startIndex: number, route: number[]): void {
|
||||
resArr.push(route.slice());
|
||||
let length: number = nums.length;
|
||||
if (startIndex === length) return;
|
||||
for (let i = startIndex; i < length; i++) {
|
||||
if (i > startIndex && nums[i] === nums[i - 1]) continue;
|
||||
route.push(nums[i]);
|
||||
backTraking(nums, i + 1, route);
|
||||
route.pop();
|
||||
}
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### C
|
||||
|
||||
```c
|
||||
@ -387,7 +409,7 @@ int** subsetsWithDup(int* nums, int numsSize, int* returnSize, int** returnColum
|
||||
}
|
||||
```
|
||||
|
||||
## Swift
|
||||
### Swift
|
||||
|
||||
```swift
|
||||
func subsetsWithDup(_ nums: [Int]) -> [[Int]] {
|
||||
|
@ -315,5 +315,75 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
C:
|
||||
```C
|
||||
typedef struct HashNodeTag {
|
||||
int key; /* num */
|
||||
struct HashNodeTag *next;
|
||||
}HashNode;
|
||||
|
||||
/* Calcualte the hash key */
|
||||
static inline int hash(int key, int size) {
|
||||
int index = key % size;
|
||||
return (index > 0) ? (index) : (-index);
|
||||
}
|
||||
|
||||
/* Calculate the sum of the squares of its digits*/
|
||||
static inline int calcSquareSum(int num) {
|
||||
unsigned int sum = 0;
|
||||
while(num > 0) {
|
||||
sum += (num % 10) * (num % 10);
|
||||
num = num/10;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
#define HASH_TABLE_SIZE (32)
|
||||
|
||||
bool isHappy(int n){
|
||||
int sum = n;
|
||||
int index = 0;
|
||||
bool bHappy = false;
|
||||
bool bExit = false;
|
||||
/* allocate the memory for hash table with chaining method*/
|
||||
HashNode ** hashTable = (HashNode **)calloc(HASH_TABLE_SIZE, sizeof(HashNode));
|
||||
|
||||
while(bExit == false) {
|
||||
/* check if n has been calculated */
|
||||
index = hash(n, HASH_TABLE_SIZE);
|
||||
|
||||
HashNode ** p = hashTable + index;
|
||||
|
||||
while((*p) && (bExit == false)) {
|
||||
/* Check if this num was calculated, if yes, this will be endless loop */
|
||||
if((*p)->key == n) {
|
||||
bHappy = false;
|
||||
bExit = true;
|
||||
}
|
||||
/* move to next node of the same index */
|
||||
p = &((*p)->next);
|
||||
}
|
||||
|
||||
/* put n intot hash table */
|
||||
HashNode * newNode = (HashNode *)malloc(sizeof(HashNode));
|
||||
newNode->key = n;
|
||||
newNode->next = NULL;
|
||||
|
||||
*p = newNode;
|
||||
|
||||
sum = calcSquareSum(n);
|
||||
if(sum == 1) {
|
||||
bHappy = true;
|
||||
bExit = true;
|
||||
}
|
||||
else {
|
||||
n = sum;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return bHappy;
|
||||
}
|
||||
```
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -281,6 +281,38 @@ impl Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
C:
|
||||
```C
|
||||
int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
|
||||
|
||||
int nums1Cnt[1000] = {0};
|
||||
int lessSize = nums1Size < nums2Size ? nums1Size : nums2Size;
|
||||
int * result = (int *) calloc(lessSize, sizeof(int));
|
||||
int resultIndex = 0;
|
||||
int* tempNums;
|
||||
|
||||
int i;
|
||||
|
||||
/* Calculate the number's counts for nums1 array */
|
||||
for(i = 0; i < nums1Size; i ++) {
|
||||
nums1Cnt[nums1[i]]++;
|
||||
}
|
||||
|
||||
/* Check if the value in nums2 is existing in nums1 count array */
|
||||
for(i = 0; i < nums2Size; i ++) {
|
||||
if(nums1Cnt[nums2[i]] > 0) {
|
||||
result[resultIndex] = nums2[i];
|
||||
resultIndex ++;
|
||||
/* Clear this count to avoid duplicated value */
|
||||
nums1Cnt[nums2[i]] = 0;
|
||||
}
|
||||
}
|
||||
* returnSize = resultIndex;
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||
## 相关题目
|
||||
|
||||
* 350.两个数组的交集 II
|
||||
|
@ -396,7 +396,35 @@ var findSubsequences = function(nums) {
|
||||
|
||||
```
|
||||
|
||||
## TypeScript
|
||||
|
||||
```typescript
|
||||
function findSubsequences(nums: number[]): number[][] {
|
||||
const resArr: number[][] = [];
|
||||
backTracking(nums, 0, []);
|
||||
return resArr;
|
||||
function backTracking(nums: number[], startIndex: number, route: number[]): void {
|
||||
let length: number = nums.length;
|
||||
if (route.length >= 2) {
|
||||
resArr.push(route.slice());
|
||||
}
|
||||
const usedSet: Set<number> = new Set();
|
||||
for (let i = startIndex; i < length; i++) {
|
||||
if (
|
||||
nums[i] < route[route.length - 1] ||
|
||||
usedSet.has(nums[i])
|
||||
) continue;
|
||||
usedSet.add(nums[i]);
|
||||
route.push(nums[i]);
|
||||
backTracking(nums, i + 1, route);
|
||||
route.pop();
|
||||
}
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### C
|
||||
|
||||
```c
|
||||
int* path;
|
||||
int pathTop;
|
||||
|
@ -276,7 +276,7 @@ func search(nums []int, target int) int {
|
||||
```
|
||||
|
||||
**JavaScript:**
|
||||
(版本一)左闭右闭区间
|
||||
(版本一)左闭右闭区间 [left, right]
|
||||
|
||||
```js
|
||||
/**
|
||||
@ -285,10 +285,12 @@ func search(nums []int, target int) int {
|
||||
* @return {number}
|
||||
*/
|
||||
var search = function(nums, target) {
|
||||
// right是数组最后一个数的下标,num[right]在查找范围内,是左闭右闭区间
|
||||
let left = 0, right = nums.length - 1;
|
||||
// 使用左闭右闭区间
|
||||
// 当left=right时,由于nums[right]在查找范围内,所以要包括此情况
|
||||
while (left <= right) {
|
||||
let mid = left + Math.floor((right - left)/2);
|
||||
// 如果中间数大于目标值,要把中间数排除查找范围,所以右边界更新为mid-1;如果右边界更新为mid,那中间数还在下次查找范围内
|
||||
if (nums[mid] > target) {
|
||||
right = mid - 1; // 去左面闭区间寻找
|
||||
} else if (nums[mid] < target) {
|
||||
@ -300,7 +302,7 @@ var search = function(nums, target) {
|
||||
return -1;
|
||||
};
|
||||
```
|
||||
(版本二)左闭右开区间
|
||||
(版本二)左闭右开区间 [left, right)
|
||||
|
||||
```js
|
||||
/**
|
||||
@ -309,10 +311,13 @@ var search = function(nums, target) {
|
||||
* @return {number}
|
||||
*/
|
||||
var search = function(nums, target) {
|
||||
// right是数组最后一个数的下标+1,nums[right]不在查找范围内,是左闭右开区间
|
||||
let left = 0, right = nums.length;
|
||||
// 使用左闭右开区间 [left, right)
|
||||
// 当left=right时,由于nums[right]不在查找范围,所以不必包括此情况
|
||||
while (left < right) {
|
||||
let mid = left + Math.floor((right - left)/2);
|
||||
// 如果中间值大于目标值,中间值不应在下次查找的范围内,但中间值的前一个值应在;
|
||||
// 由于right本来就不在查找范围内,所以将右边界更新为中间值,如果更新右边界为mid-1则将中间值的前一个值也踢出了下次寻找范围
|
||||
if (nums[mid] > target) {
|
||||
right = mid; // 去左区间寻找
|
||||
} else if (nums[mid] < target) {
|
||||
|
Reference in New Issue
Block a user