Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcd722f4

Browse files
authored
Merge pull requestneetcode-gh#1008 from Ykhan799/main
Create: 78-Subsets.swift, 287-Find-The-Duplicate-Number.swift, 53-Maximum-Subarray.swift, 134-Gas-Station.swift, 287-Find-The-Duplicate-Number.c, 268-Missing-Number.c, 190-Reverse-Bits.c, 191-Number-of-1-Bits.c, 136-Single-Number.c, 213-House-Robber-II.c, 153-Find-Minimum-in-Rotated-Sorted-Array.c, 202-Happy-Number.c, 55-Jump-Game.c
2 parents7940ca5 +b991518 commitcd722f4

13 files changed

+207
-0
lines changed

‎c/136-Single-Number.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
intsingleNumber(int*nums,intnumsSize){
2+
intres=0;
3+
for (inti=0;i<numsSize;i++) {
4+
res=nums[i] ^res;
5+
}
6+
returnres;
7+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
intfindMin(int*nums,intnumsSize){
2+
// set the starting indicies to find minimum pivot
3+
intleft=0;
4+
intright=numsSize-1;
5+
6+
while (left<right) {
7+
// calculate middle index
8+
intmiddle= (left+right) /2;
9+
10+
// set the left or right index accordingly
11+
if (nums[middle]>nums[right]) {
12+
left=middle+1;
13+
}
14+
else {
15+
right=middle;
16+
}
17+
}
18+
returnnums[left];
19+
}

‎c/190-Reverse-Bits.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
uint32_treverseBits(uint32_tn) {
2+
uint32_tres=0;
3+
4+
for (inti=0;i<32;i++) {
5+
res <<=1;
6+
res |=n&1;
7+
n >>=1;
8+
}
9+
returnres;
10+
}

‎c/191-Number-of-1-Bits.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
inthammingWeight(uint32_tn) {
2+
intres=0;
3+
intbit=0;
4+
while (n!=0) {
5+
bit=n&1;
6+
if (bit==1) {
7+
res++;
8+
}
9+
n=n >>1;
10+
}
11+
returnres;
12+
}

‎c/202-Happy-Number.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
boolisHappy(intn) {
2+
intslow=n;
3+
intfast=sumSquareDigits(n);
4+
5+
while (slow!=fast) {
6+
fast=sumSquareDigits(sumSquareDigits(fast));
7+
slow=sumSquareDigits(slow);
8+
}
9+
returnfast==1;
10+
}
11+
12+
intsumSquareDigits(n) {
13+
intresult=0;
14+
while (n!=0) {
15+
intdigit=n %10;
16+
result+=digit*digit;
17+
n /=10;
18+
}
19+
returnresult;
20+
}

‎c/213-House-Robber-II.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
introb(int*nums,intnumsSize){
2+
intn=numsSize;
3+
4+
if (n==1) {
5+
returnnums[0];
6+
}
7+
8+
intrange1=robber(nums,0,n-2);
9+
intrange2=robber(nums,1,n-1);
10+
11+
return (range1>range2) ?range1 :range2;
12+
}
13+
14+
introbber(int*nums,intstart,intend) {
15+
intprev=0;
16+
intcurr=0;
17+
intnext=0;
18+
19+
for (inti=start;i <=end;i++) {
20+
next= (curr>prev+nums[i]) ?curr :prev+nums[i];
21+
prev=curr;
22+
curr=next;
23+
}
24+
returncurr;
25+
}

‎c/268-Missing-Number.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
intmissingNumber(int*nums,intnumsSize){
2+
intres=numsSize;
3+
4+
for (inti=0;i<numsSize;i++) {
5+
res+=i-nums[i];
6+
}
7+
returnres;
8+
}

‎c/287-Find-The-Duplicate-Number.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
intfindDuplicate(int*nums,intnumsSize){
2+
intslow=nums[0];
3+
intfast=nums[nums[0]];
4+
5+
while (slow!=fast) {
6+
slow=nums[slow];
7+
fast=nums[nums[fast]];
8+
}
9+
10+
slow=0;
11+
while (slow!=fast) {
12+
slow=nums[slow];
13+
fast=nums[fast];
14+
}
15+
returnslow;
16+
}

‎c/55-Jump-Game.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
boolcanJump(int*nums,intnumsSize){
2+
intgoal=numsSize-1;
3+
4+
for (inti=numsSize-2;i >=0;i--) {
5+
if (i+nums[i] >=goal) {
6+
goal=i;
7+
}
8+
}
9+
returngoal==0;
10+
}

‎swift/134-Gas-Station.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
classSolution{
2+
func canCompleteCircuit(_ gas:[Int], _ cost:[Int])->Int{
3+
if gas.reduce(0,+)< cost.reduce(0,+){
4+
return-1
5+
}
6+
varstart= gas.count-1,end=0
7+
vartotal=gas[start]- cost[start]
8+
9+
while start>= end{
10+
while total<0 && start>= end{
11+
start-=1
12+
total+=gas[start]- cost[start]
13+
}
14+
if start== end{
15+
return start
16+
}
17+
total+=gas[end]- cost[end]
18+
end+=1
19+
}
20+
return-1
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
classSolution{
2+
func findDuplicate(_ nums:[Int])->Int{
3+
varslow=0,fast=0
4+
whiletrue{
5+
slow=nums[slow]
6+
fast=nums[nums[fast]]
7+
if slow== fast{
8+
break
9+
}
10+
}
11+
12+
varslow2=0
13+
whiletrue{
14+
slow=nums[slow]
15+
slow2=nums[slow2]
16+
if slow== slow2{
17+
return slow
18+
}
19+
}
20+
}
21+
}

‎swift/53-Maximum-Subarray.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
classSolution{
2+
func maxSubArray(_ nums:[Int])->Int{
3+
varres=nums[0]
4+
vartotal=0
5+
6+
fornin nums{
7+
total+= n
8+
res=max(res, total)
9+
if total<0{
10+
total=0
11+
}
12+
}
13+
return res
14+
}
15+
}

‎swift/78-Subsets.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
classSolution{
2+
func subsets(_ nums:[Int])->[[Int]]{
3+
varres:[[Int]]=[]
4+
varsubset:[Int]=[]
5+
6+
func dfs(_ i:Int){
7+
if i>= nums.count{
8+
res.append(subset)
9+
return
10+
}
11+
// decision to include nums[i]
12+
subset.append(nums[i])
13+
dfs(i+1)
14+
// decision NOT to include nums[i]
15+
subset.removeLast()
16+
dfs(i+1)
17+
}
18+
19+
dfs(0)
20+
return res
21+
}
22+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp