Skip to content

Commit e52a88b

Browse files
Merge branch 'youngyangyang04:master' into master
2 parents b73a7da + 6b8b912 commit e52a88b

File tree

7 files changed

+212
-6
lines changed

7 files changed

+212
-6
lines changed

problems/0046.全排列.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,34 @@ var permute = function(nums) {
331331

332332
```
333333

334+
## TypeScript
335+
336+
```typescript
337+
function permute(nums: number[]): number[][] {
338+
const resArr: number[][] = [];
339+
const helperSet: Set<number> = new Set();
340+
backTracking(nums, []);
341+
return resArr;
342+
function backTracking(nums: number[], route: number[]): void {
343+
if (route.length === nums.length) {
344+
resArr.push(route.slice());
345+
return;
346+
}
347+
let tempVal: number;
348+
for (let i = 0, length = nums.length; i < length; i++) {
349+
tempVal = nums[i];
350+
if (!helperSet.has(tempVal)) {
351+
route.push(tempVal);
352+
helperSet.add(tempVal);
353+
backTracking(nums, route);
354+
route.pop();
355+
helperSet.delete(tempVal);
356+
}
357+
}
358+
}
359+
};
360+
```
361+
334362
### C
335363

336364
```c

problems/0078.子集.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,28 @@ var subsets = function(nums) {
272272
};
273273
```
274274

275+
## TypeScript
276+
277+
```typescript
278+
function subsets(nums: number[]): number[][] {
279+
const resArr: number[][] = [];
280+
backTracking(nums, 0, []);
281+
return resArr;
282+
function backTracking(nums: number[], startIndex: number, route: number[]): void {
283+
resArr.push(route.slice());
284+
let length = nums.length;
285+
if (startIndex === length) return;
286+
for (let i = startIndex; i < length; i++) {
287+
route.push(nums[i]);
288+
backTracking(nums, i + 1, route);
289+
route.pop();
290+
}
291+
}
292+
};
293+
```
294+
275295
## C
296+
276297
```c
277298
int* path;
278299
int pathTop;

problems/0090.子集II.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,28 @@ var subsetsWithDup = function(nums) {
318318

319319
```
320320

321+
### TypeScript
322+
323+
```typescript
324+
function subsetsWithDup(nums: number[]): number[][] {
325+
nums.sort((a, b) => a - b);
326+
const resArr: number[][] = [];
327+
backTraking(nums, 0, []);
328+
return resArr;
329+
function backTraking(nums: number[], startIndex: number, route: number[]): void {
330+
resArr.push(route.slice());
331+
let length: number = nums.length;
332+
if (startIndex === length) return;
333+
for (let i = startIndex; i < length; i++) {
334+
if (i > startIndex && nums[i] === nums[i - 1]) continue;
335+
route.push(nums[i]);
336+
backTraking(nums, i + 1, route);
337+
route.pop();
338+
}
339+
}
340+
};
341+
```
342+
321343
### C
322344

323345
```c
@@ -387,7 +409,7 @@ int** subsetsWithDup(int* nums, int numsSize, int* returnSize, int** returnColum
387409
}
388410
```
389411
390-
## Swift
412+
### Swift
391413
392414
```swift
393415
func subsetsWithDup(_ nums: [Int]) -> [[Int]] {

problems/0202.快乐数.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,5 +315,75 @@ class Solution {
315315
}
316316
```
317317

318+
C:
319+
```C
320+
typedef struct HashNodeTag {
321+
int key; /* num */
322+
struct HashNodeTag *next;
323+
}HashNode;
324+
325+
/* Calcualte the hash key */
326+
static inline int hash(int key, int size) {
327+
int index = key % size;
328+
return (index > 0) ? (index) : (-index);
329+
}
330+
331+
/* Calculate the sum of the squares of its digits*/
332+
static inline int calcSquareSum(int num) {
333+
unsigned int sum = 0;
334+
while(num > 0) {
335+
sum += (num % 10) * (num % 10);
336+
num = num/10;
337+
}
338+
return sum;
339+
}
340+
341+
#define HASH_TABLE_SIZE (32)
342+
343+
bool isHappy(int n){
344+
int sum = n;
345+
int index = 0;
346+
bool bHappy = false;
347+
bool bExit = false;
348+
/* allocate the memory for hash table with chaining method*/
349+
HashNode ** hashTable = (HashNode **)calloc(HASH_TABLE_SIZE, sizeof(HashNode));
350+
351+
while(bExit == false) {
352+
/* check if n has been calculated */
353+
index = hash(n, HASH_TABLE_SIZE);
354+
355+
HashNode ** p = hashTable + index;
356+
357+
while((*p) && (bExit == false)) {
358+
/* Check if this num was calculated, if yes, this will be endless loop */
359+
if((*p)->key == n) {
360+
bHappy = false;
361+
bExit = true;
362+
}
363+
/* move to next node of the same index */
364+
p = &((*p)->next);
365+
}
366+
367+
/* put n intot hash table */
368+
HashNode * newNode = (HashNode *)malloc(sizeof(HashNode));
369+
newNode->key = n;
370+
newNode->next = NULL;
371+
372+
*p = newNode;
373+
374+
sum = calcSquareSum(n);
375+
if(sum == 1) {
376+
bHappy = true;
377+
bExit = true;
378+
}
379+
else {
380+
n = sum;
381+
382+
}
383+
}
384+
385+
return bHappy;
386+
}
387+
```
318388
-----------------------
319389
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0349.两个数组的交集.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,38 @@ impl Solution {
281281
}
282282
}
283283
```
284+
285+
C:
286+
```C
287+
int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
288+
289+
int nums1Cnt[1000] = {0};
290+
int lessSize = nums1Size < nums2Size ? nums1Size : nums2Size;
291+
int * result = (int *) calloc(lessSize, sizeof(int));
292+
int resultIndex = 0;
293+
int* tempNums;
294+
295+
int i;
296+
297+
/* Calculate the number's counts for nums1 array */
298+
for(i = 0; i < nums1Size; i ++) {
299+
nums1Cnt[nums1[i]]++;
300+
}
301+
302+
/* Check if the value in nums2 is existing in nums1 count array */
303+
for(i = 0; i < nums2Size; i ++) {
304+
if(nums1Cnt[nums2[i]] > 0) {
305+
result[resultIndex] = nums2[i];
306+
resultIndex ++;
307+
/* Clear this count to avoid duplicated value */
308+
nums1Cnt[nums2[i]] = 0;
309+
}
310+
}
311+
* returnSize = resultIndex;
312+
return result;
313+
}
314+
```
315+
284316
## 相关题目
285317
286318
* 350.两个数组的交集 II

problems/0491.递增子序列.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,35 @@ var findSubsequences = function(nums) {
396396

397397
```
398398

399+
## TypeScript
400+
401+
```typescript
402+
function findSubsequences(nums: number[]): number[][] {
403+
const resArr: number[][] = [];
404+
backTracking(nums, 0, []);
405+
return resArr;
406+
function backTracking(nums: number[], startIndex: number, route: number[]): void {
407+
let length: number = nums.length;
408+
if (route.length >= 2) {
409+
resArr.push(route.slice());
410+
}
411+
const usedSet: Set<number> = new Set();
412+
for (let i = startIndex; i < length; i++) {
413+
if (
414+
nums[i] < route[route.length - 1] ||
415+
usedSet.has(nums[i])
416+
) continue;
417+
usedSet.add(nums[i]);
418+
route.push(nums[i]);
419+
backTracking(nums, i + 1, route);
420+
route.pop();
421+
}
422+
}
423+
};
424+
```
425+
399426
### C
427+
400428
```c
401429
int* path;
402430
int pathTop;

problems/0704.二分查找.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ func search(nums []int, target int) int {
276276
```
277277

278278
**JavaScript:**
279-
(版本一)左闭右闭区间
279+
(版本一)左闭右闭区间 [left, right]
280280

281281
```js
282282
/**
@@ -285,10 +285,12 @@ func search(nums []int, target int) int {
285285
* @return {number}
286286
*/
287287
var search = function(nums, target) {
288+
// right是数组最后一个数的下标,num[right]在查找范围内,是左闭右闭区间
288289
let left = 0, right = nums.length - 1;
289-
// 使用左闭右闭区间
290+
// 当left=right时,由于nums[right]在查找范围内,所以要包括此情况
290291
while (left <= right) {
291292
let mid = left + Math.floor((right - left)/2);
293+
// 如果中间数大于目标值,要把中间数排除查找范围,所以右边界更新为mid-1;如果右边界更新为mid,那中间数还在下次查找范围内
292294
if (nums[mid] > target) {
293295
right = mid - 1; // 去左面闭区间寻找
294296
} else if (nums[mid] < target) {
@@ -300,7 +302,7 @@ var search = function(nums, target) {
300302
return -1;
301303
};
302304
```
303-
(版本二)左闭右开区间
305+
(版本二)左闭右开区间 [left, right)
304306

305307
```js
306308
/**
@@ -309,10 +311,13 @@ var search = function(nums, target) {
309311
* @return {number}
310312
*/
311313
var search = function(nums, target) {
312-
let left = 0, right = nums.length;
313-
// 使用左闭右开区间 [left, right)
314+
// right是数组最后一个数的下标+1,nums[right]不在查找范围内,是左闭右开区间
315+
let left = 0, right = nums.length;
316+
// 当left=right时,由于nums[right]不在查找范围,所以不必包括此情况
314317
while (left < right) {
315318
let mid = left + Math.floor((right - left)/2);
319+
// 如果中间值大于目标值,中间值不应在下次查找的范围内,但中间值的前一个值应在;
320+
// 由于right本来就不在查找范围内,所以将右边界更新为中间值,如果更新右边界为mid-1则将中间值的前一个值也踢出了下次寻找范围
316321
if (nums[mid] > target) {
317322
right = mid; // 去左区间寻找
318323
} else if (nums[mid] < target) {

0 commit comments

Comments
 (0)