Guest User

Untitled

a guest
Jan 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. function solution(N) {
  2. const bin = N.toString(2);
  3.  
  4. let currentGap = 0;
  5. let gaps = [];
  6.  
  7. for (i=0; i<bin.length; i++){
  8.  
  9. if (bin[i]==="0"){
  10. currentGap++;
  11.  
  12. if (bin[i+1]==="1"){
  13. gaps.push(currentGap);
  14. currentGap = 0;
  15. }
  16. }
  17. }
  18.  
  19. if (gaps.length===1){
  20. return gaps[0];
  21. } else if (gaps.length>1){
  22. return Math.max(...gaps)
  23. } else {
  24. return 0
  25. }
  26. }
  27.  
  28. function solution(n) {
  29. var maxZeros = 0;
  30. while(n !== 0 && n % 2 === 0) {
  31. n >>>= 1;
  32. }
  33. for(var curr=0; n !== 0; n>>>=1) {
  34. if(n % 2 === 0) {
  35. curr++;
  36. } else {
  37. curr = 0;
  38. }
  39. maxZeros = Math.max(maxZeros, curr);
  40. }
  41. return maxZeros;
  42. }
  43.  
  44. class Solution {
  45. public int solution(int N) {
  46. int count = 0;
  47. string binary;
  48. int maxLength = 0;
  49. char str = '0';
  50. binary = Convert.ToString(N, 2);
  51. for (int i = 0; i < binary.Length; i++)
  52. {
  53. if (binary[i] == str)
  54. count++;
  55. else
  56. {
  57. if (maxLength < count)
  58. maxLength = count;
  59. count = 0;
  60. }
  61. }
  62. return maxLength;
  63. }
  64. }
Add Comment
Please, Sign In to add comment