目前分類:leetcode (2)

瀏覽方式: 標題列表 簡短摘要

題目

Given an integer num, return a string representing its hexadecimal representation. For negative integers, two’s complement method is used.

All the letters in the answer string should be lowercase characters, and there should not be any leading zeros in the answer except for the zero itself.

Note: You are not allowed to use any built-in library method to directly solve this problem.

Example 1:

Input: num = 26
Output: “1a”
Example 2:

文章標籤

Darwin的AI天地 發表在 痞客邦 留言(0) 人氣()

 

由於我高階語言寫習慣了,在python中翻轉字串也就是一行的事

my_string[::-1]

但在C語言中卻不是如此,最簡單的寫法就是創建一個新的string然後再從後面慢慢地加入要的字元

 

#include <iostream>
using namespace std;

int main() {
  
  string greeting = "Hello!";

  string new_greeting;

  for(int n = greeting.length()-1; n >= 0; n--){
    new_greeting.push_back(greeting[n]);
  }
  cout<<"Original string: "<< greeting << endl;
  cout<<"New reversed string: "<< new_greeting << endl;
  
}

 

Darwin的AI天地 發表在 痞客邦 留言(0) 人氣()