Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
AhkunTa
Arithmetic
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Arithmetic
/
LeetCode
/
67_javascript.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
63 lines (52 loc) · 1.52 KB
master
Breadcrumbs
Arithmetic
/
LeetCode
/
67_javascript.js
Copy path
Top
File metadata and controls
Code
Blame
63 lines (52 loc) · 1.52 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 给定两个二进制字符串,返回他们的和(用二进制表示)。
// 输入为非空字符串且只包含数字 1 和 0。
// 示例 1:
// 输入: a = "11", b = "1"
// 输出: "100"
// 示例 2:
// 输入: a = "1010", b = "1011"
// 输出: "10101"
/**
* @param {string} a
* @param {string} b
* @return {string}
*/
// 1
var addBinary = function(a, b) {
let isUp = 0; //是否进位 进位为1 加在下一次循环上
let i = a.length-1, j= b.length-1;
let sum = ''// 返回的字符串
// 从后往前计算将ab 字符串最后两位相加 判断是否进位 有3种情况
// num = 0 num = 1 不进位 则 isUp = 0
// num = 2 num = 3 进位 isUp = 1
while(i>-1 || j>-1){
// 计算一位的 值
let num = (a[i] || 0)*1 + (b[j] || 0)*1 + isUp;
sum = num%2 + sum;
isUp = Math.floor(num/2);
i--;
j--;
}
// 判断第一位是否进位 若进位则在字符串前加1
let returnNum = isUp == 1 ? '1' + sum : sum;
return returnNum
};
// 2 暴力解法 直接用parseInt做 不过在数据量过大时会报错 通不过编译
var addBinary = function(a, b) {
let A = parseInt(a,2);
let B = parseInt(b,2);
return (A + B).toString(2);
}
// 3 别人的代码,更加简洁。
var addBinary = function(a, b) {
let res = ''
let c = 0
a = a.split('')
b = b.split('')
while (a.length || b.length || c) {
c = ~~a.pop() + ~~b.pop() +c
res = c % 2 + res
c = c > 1
}
return res
}
You can’t perform that action at this time.