Skip to content
New issue

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

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

Already on GitHub? # to your account

URL反转 #93

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

URL反转 #93

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

Comments

@Sunny-117
Copy link
Owner

No description provided.

@Nasuke
Copy link
Contributor

Nasuke commented Nov 4, 2022

是不能用split 和 join那题吗

        let url = 'www.baidu.toutiao.com'
        function reverseURL(str) {
            const len = str.length
            let i = j = 0
            let res = ''
            while(j < len){
                if(str[j] === '.'){
                    res = '.' + str.slice(i,j) + res
                    i = ++j
                }else {
                    j++
                }
            }
            res = str.slice(i, j) + res
            console.log(res);
        }
        console.log(reveerseURL(url)) // 'com.taobao.baidu.www'

@veneno-o
Copy link
Contributor

// 那我这样也不算违规吧...
Array.prototype._join = function(char){
    let res = "";
    for(let i = 0; i < this.length; ++i){
        if(i === this.length - 1){
            res += this[i];
        }else{
            res += this[i] + char;
        }
    }
    return res;
}

Array.prototype._reverse = function(){
    for(let i = 0, j = this.length - 1; i < j; ++i, --j){
        [this[i], this[j]] = [this[j], this[i]];
    }
    return this;
}

String.prototype._split = function(char){
    const res = [];
    let temp = "";
    for(let i = 0; i < this.length; ++i){
        if(this[i] === char){
            res.push(temp);
            temp = "";
        }else{
            temp += this[i];
        }
    }
    if(temp){
        res.push(temp);
    }
    return res;
}

function reverseURL(url) {
    return url._split(".")._reverse()._join(".");
}
console.log(reverseURL(url))

@veneno-o
Copy link
Contributor

// 时间复杂度O(n) 空间复杂度O(1)
function reverseURL(url) {
    const arr = [];
    let res = "";
    for(let i = url.length - 1; i >= 0; --i){
        url[i] !== "." && (res = url[i] + res);
        if(i === 0 || url[i] === "."){
            arr.push(res);
            res = "";
        }
    }

    for(let i = 0; i < arr.length; ++i){
        res += arr[i];
        if(i !== arr.length - 1){
            res += "."
        }
    }
    return res;
}

@kangkang123269
Copy link

var str = "www.baidu.taobao.com";
var reversedStr = '';
var temp = '';

for (let i = str.length - 1; i >= 0; i--) {
    if (str[i] === '.') {
        reversedStr += temp + '.';
        temp = '';
    } else {
        temp = str[i] + temp;
    }
}

reversedStr += temp;

console.log(reversedStr);

@Windseek
Copy link

Windseek commented Apr 5, 2024

let url = 'www.baidu.toutiao.com'

function reverse(str) {
let _str = '';
let _item = '';
for(var i = 0; i < str.length; i++) {
if(str[i] != '.') {
_item = _item+str[i]
} else {
if(_str) {
_str = _item +'.' + _str;
} else {
_str = _item;
}
console.log('_str', _str);
_item = '';
}
}
return _item + '.' + _str;
}

console.log(reverse(url));

@dizao006
Copy link

function toreverse(str) {
  let st = "";
  let reg = /[a-z]+./g;
  let arr = str.match(reg);

  for (let i = arr.length - 1; i >= 0; i--) {
    if (arr[i][arr[i].length - 1] === ".") {
      arr[i] = arr[i].slice(0, arr[i].length - 1);
    }
    st += arr[i] + ".";
  }
  return st.slice(0, st.length - 1);
}
let url = "www.baidu.toutiao.com";
console.log(toreverse(url));

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

No branches or pull requests

6 participants