From 50b939cf644e340727e3171b2d71478e3e98fa85 Mon Sep 17 00:00:00 2001 From: limbowandering <591176276@qq.com> Date: Sun, 21 Oct 2018 17:51:11 +0800 Subject: [PATCH 1/3] add js solution for 191 --- solution/191. Number of 1 Bits/Solution.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 solution/191. Number of 1 Bits/Solution.js diff --git a/solution/191. Number of 1 Bits/Solution.js b/solution/191. Number of 1 Bits/Solution.js new file mode 100644 index 0000000000000..e5dcf21d897d4 --- /dev/null +++ b/solution/191. Number of 1 Bits/Solution.js @@ -0,0 +1,8 @@ +const hammingWeight = function(n){ + let result = 0; + while(n){ + result += n & 1; + n = n >>> 1; + } + return result; +} \ No newline at end of file From 2c897296728e4283bf9cbd27fb828920bd6b2209 Mon Sep 17 00:00:00 2001 From: limbowandering <591176276@qq.com> Date: Sun, 21 Oct 2018 17:51:34 +0800 Subject: [PATCH 2/3] add js solution for 926 --- .../Solution.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 solution/926. Flip String to Monotone Increasing/Solution.js diff --git a/solution/926. Flip String to Monotone Increasing/Solution.js b/solution/926. Flip String to Monotone Increasing/Solution.js new file mode 100644 index 0000000000000..0b30dc295464a --- /dev/null +++ b/solution/926. Flip String to Monotone Increasing/Solution.js @@ -0,0 +1,17 @@ +const minFlipsMonoIncr = function (S) { + let n = S.length; + let f = []; + for (let i = 0; i < n + 1; i++) { + f.push(0); + } + for (let i = 0; i < n; i++) { + f[i + 1] = f[i] + (S[i] === '1'); + } + let ans = n; + for (let i = 0; i <= n; i++) { + let a = f[i]; + let b = (n - i) - (f[n] - f[i]); + ans = Math.min(ans, b + a); + } + return ans; +}; \ No newline at end of file From 99fabad956b036eaa90679f4cb5227a9f4f50658 Mon Sep 17 00:00:00 2001 From: limbowandering <591176276@qq.com> Date: Sun, 21 Oct 2018 17:58:54 +0800 Subject: [PATCH 3/3] add js solution for 011 --- .../011. Container With Most Water/Solution.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 solution/011. Container With Most Water/Solution.js diff --git a/solution/011. Container With Most Water/Solution.js b/solution/011. Container With Most Water/Solution.js new file mode 100644 index 0000000000000..5fe55494d506d --- /dev/null +++ b/solution/011. Container With Most Water/Solution.js @@ -0,0 +1,18 @@ +const maxArea2 = function(height){ + let result = 0; + for(let i = 0; i < height.length; i++){ + for(let j = i + 1; j < height.length; j++){ + result = Math.max(result, Math.min(height[i],height[j])*(j-i)); + } + } + return result; +}; + +const maxArea = function(height){ + let result = 0, l = 0, r = height.length - 1; + while(l < r){ + result = Math.max(result, Math.min(height[l],height[r])*(r-l)); + height[l] < height[r] ? l++ : r--; + } + return result; +}