Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

打印出 1~10000 以内的对称数 #137

Open
Sunny-117 opened this issue Nov 3, 2022 · 5 comments
Open

打印出 1~10000 以内的对称数 #137

Sunny-117 opened this issue Nov 3, 2022 · 5 comments

Comments

@Sunny-117
Copy link
Owner

No description provided.

@lxy-Jason
Copy link
Contributor

function isSymmetryNum(start,end){
    for(let i = start; i < end + 1; i++){
        let str = String(i);
        if(str.length <= 1) continue;
        if(str.split("").reverse().join("") === str){
            console.log(i);
        }
    }
}
isSymmetryNum(1,10000)

@weirong111
Copy link

function getDuicheng() {
  const res = [];
  for (let i = 1; i < 10; i++) {
    res.push(i);
    res.push(i * 11);
    for (let j = 0; j < 10; j++) {
      res.push(i * 101 + j * 10); //101-999
      res.push(i * 1001 + j * 110); //1001-9999
    }
  }
  return res;
}

@bearki99
Copy link

const res = [];
function getSymNum() {
  for (let i = 1; i <= 10000; i++) {
    let str1 = i + "";
    if (judgeSym(str1)) res.push(i);
  }
}
function judgeSym(str) {
  let l = 0,
    r = str.length - 1;
  while (l < r) {
    if (str[l] !== str[r]) return false;
    r--;
    l++;
  }
  return true;
}
getSymNum();
console.log(res);

简单的双指针

@MoYuanJun
Copy link

const isSymmetryNum = (start, end) => {
const res = []

for (let i = start; i < end; i++) {
const toStr = String(i)
const reverse = [...toStr].reverse().join('')
if (toStr === reverse) {
res.push(i)
}
}

return res
}

@fangbw17
Copy link

function symmetric(num) {
    const result = [];

    const nums = Array.from({ length: num }, (_, i) => 1 + i);
    while (nums.length > 0) {
        const lastNum = nums.pop();
        if (String(lastNum).split("").reverse().join("") === String(lastNum)) {
            result.push(lastNum);
        }
    }
    return result;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants