【編程】【leetcode】344. Reverse String

1.字符編碼

//字節和字符的區別    http://jiapumin.iteye.com/blog/1006144   getBytes和操作系統編碼格式相關聯的
import java.io.UnsupportedEncodingException;
public class Test01 {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String gb=new String("中國".getBytes(),"gb2312");
        System.out.println(gb);
        String ios=new String(gb.getBytes("gb2312"),"ISO-8859-1");
        System.out.println(ios);  //原本編碼格式轉換成另外一種以後也是可能出現亂碼的
        gb=new String(ios.getBytes("ISO-8859-1"),"gb2312");
        System.out.println(gb);

        //在IDEA中上面的中國都是亂碼,只有在使用了utf-8以後纔不是亂碼  主要的原因在於ISO-8859-1沒有對應的中文符號
        String gb2=new String("中國".getBytes(),"utf-8");
        System.out.println(gb2);
        String ios2=new String(gb2.getBytes("utf-8"),"ISO-8859-1");
        System.out.println(ios2);
        gb2=new String(ios2.getBytes("ISO-8859-1"),"utf-8");
        System.out.println(gb2);
    }
}

2.曾經項目中utf-b和ansi編碼格式轉換代碼:

#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;

/*首先明確一點:ansi轉utf8編碼,不能直接相互轉換,只能是經過unicode編碼格式。*/

/*別人分解版本*/
string Unicode2Ascii(wstring wstrsrc);

wstring Ascii2Unicode(string astrsrc);

string Unicode2UTF8(wstring wstrsrc);

wstring UTF82Unicode(string utf8strsrc);


/*自己版本*/

string FromUtf8ToAnsi(const string& str)
{
	//從utf-8到unicode編碼轉換
	int len = 0;
	len = str.length();
	int  unicodeLen = ::MultiByteToWideChar(CP_UTF8,NULL,str.c_str(),-1,NULL,0 );
	wchar_t *  pUnicode;
	pUnicode = new  wchar_t[unicodeLen+1];
	memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
	::MultiByteToWideChar( CP_UTF8,NULL,str.c_str(),-1,(LPWSTR)pUnicode,unicodeLen );
	wstring  rt;
	rt = ( wchar_t* )pUnicode;
	delete  pUnicode;
	//return  rt;


	//從unicode到ansi編碼轉換
	char *pElementText;
	int iTextLen;
	iTextLen = WideCharToMultiByte( CP_ACP,NULL,rt.c_str(),-1,NULL,0,NULL,NULL );
	pElementText = new char[iTextLen + 1];
	memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
	::WideCharToMultiByte( CP_ACP,0,rt.c_str(),-1,pElementText,iTextLen,NULL,NULL );
	string strText;
	strText = pElementText;
	delete[] pElementText;
	return strText;
}

//字符串從utf-8編碼到ansi編碼
string changestringencodingFromUtf8ToAnsi(const string& utf8str)
{
	string utf8str2 =utf8str;
	string ansistr ="";
	ansistr=FromUtf8ToAnsi(utf8str2);
	return ansistr;
}

string FromAnsitoUtf8(const string& ansistr)
{

	//ansi到unicode編碼轉換
	string ansistr2=ansistr;
	int sLen = MultiByteToWideChar(CP_ACP, NULL, ansistr2.c_str(), -1, NULL, 0);
	wchar_t* sUnicode = new wchar_t[sLen];
	//wchar_t* sUnicode = (wchar_t*)malloc(sLen*sizeof(wchar_t));
	MultiByteToWideChar(CP_ACP, NULL, ansistr2.c_str(), -1, sUnicode, sLen);
	//wstring  rt;
	//rt = ( wchar_t* )sUnicode;
	//delete  sUnicode;

	//從unicode編碼到ansi編碼轉換
	//wchar_t *sUnicode = L"Convert Unicode to ANSI, Unicode 轉換爲 ANSI";
	int sLen2 = WideCharToMultiByte(CP_ACP, NULL, sUnicode, -1, NULL, 0, NULL, NULL);
	char* sAnsi = new char[sLen2];
	//char* sAnsi = (char*)malloc(sLen);
	WideCharToMultiByte(CP_ACP, NULL, sUnicode, -1, sAnsi, sLen2, NULL, NULL);
	string strText;
	strText = sAnsi;
	delete[] sAnsi;
	return strText;
}

//字符串從ansi編碼到utf-8編碼
string changestringencodingFromAnsiToUtf8(const string& ansistr)
{
	string ansistr2 =ansistr;
	string ansistr3 ="";
	ansistr3=FromAnsitoUtf8(ansistr2);
	return ansistr3;
}

int main(int argc, char* argv[])
{
	char szText[] = "這是一個ANSI轉化爲UTF8的例子!\r\n";
	wstring strUnicode;
	string strAnsi, strUTF8;
	strAnsi = szText;
	strUnicode = Ascii2Unicode(strAnsi);
	strUTF8 = Unicode2UTF8(strUnicode);
	cout << strUTF8 << endl;

	strUnicode = UTF82Unicode(strUTF8);
	strAnsi = Unicode2Ascii(strUnicode);
	cout << strAnsi << endl;

	/*char szText[] = "這是一個ANSI轉化爲UTF8的例子!\r\n";
	string strutf8="";
	string stransi="";
	strutf8=changestringencodingFromAnsiToUtf8(szText);
	cout<<strutf8<<endl;

	stransi=changestringencodingFromUtf8ToAnsi(strutf8);
	cout<<stransi<<endl;*/
	return 0;
}

string Unicode2Ascii(wstring wstrsrc)
{
	int nLength = ::WideCharToMultiByte(CP_OEMCP, 0, wstrsrc.c_str(), -1, NULL, 0, NULL, NULL);
	if(nLength <= 0) return string("");
	char *szbuffer = new char[nLength + 2];
	::WideCharToMultiByte(CP_OEMCP, 0, wstrsrc.c_str(), -1, szbuffer, nLength, NULL, NULL);
	string strnew = szbuffer;
	delete [] szbuffer;
	return strnew;
}
wstring Ascii2Unicode(string astrsrc)
{
	int nLength = ::MultiByteToWideChar(CP_ACP, 0, astrsrc.c_str(), -1, NULL, 0);
	if(nLength <= 0) return wstring(L"");
	wchar_t *szbuffer = new wchar_t[nLength + 2];
	::MultiByteToWideChar(CP_ACP, 0, astrsrc.c_str(), -1, szbuffer, nLength);
	wstring strnew = szbuffer;
	delete [] szbuffer;
	return strnew;
}
string Unicode2UTF8(wstring wstrsrc)
{
	int nLength = ::WideCharToMultiByte(CP_UTF8, 0, wstrsrc.c_str(), -1, NULL, 0, NULL, NULL);
	if(nLength <= 0) return string("");
	char *szbuffer = new char[nLength + 2];
	::WideCharToMultiByte(CP_UTF8, 0, wstrsrc.c_str(), -1, szbuffer, nLength, NULL, NULL);
	string strnew = szbuffer;
	delete [] szbuffer;
	return strnew;
}
wstring UTF82Unicode(string utf8strsrc)
{
	int nLength = ::MultiByteToWideChar(CP_UTF8, 0, utf8strsrc.c_str(), -1, NULL, 0);
	if(nLength <= 0) return wstring(L"");
	wchar_t *szbuffer = new wchar_t[nLength + 2];
	::MultiByteToWideChar(CP_UTF8, 0, utf8strsrc.c_str(), -1, szbuffer, nLength);
	wstring strnew = szbuffer;
	delete [] szbuffer;
	return strnew;
}


3.c++內置反轉函數

#include <iostream>     // std::cout
#include <algorithm>    // std::reverse
#include <vector>       // std::vector

int main() {
	std::vector<int> myvector;

	// set some values:
	for (int i = 1; i < 10; ++i) myvector.push_back(i);   // 1 2 3 4 5 6 7 8 9

	std::reverse(myvector.begin(), myvector.end());    // 9 8 7 6 5 4 3 2 1

	// print out content:
	std::cout << "myvector contains:";
	for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
		std::cout << ' ' << *it;
	std::cout << '\n';
	return 0;
}
----------------------------------------------------------------------------------
// string::rbegin/rend
#include <iostream>
#include <string>

int main ()
{
  std::string str ("now step live...");
  for (std::string::reverse_iterator rit=str.rbegin(); rit!=str.rend(); ++rit)
    std::cout << *rit;
  return 0;
}
---------------------------------------------------------------------------------
#include <iostream>
#include<String>
using namespace std;
int main(){
	string text="i am student!";
	text=string(text.rbegin(),text.rend());
	cout<<text<<endl;
	return 0;
}


1.
public class Solution {
    public String reverseString(String s) {
        char[] word = s.toCharArray();
        int i = 0;
        int j = s.length() - 1;
        while (i < j) {
            char temp = word[i];
            word[i] = word[j];
            word[j] = temp;
            i++;
            j--;
        }
        return new String(word);
    }
}

2.
public class Solution {
    public String reverseString(String s) {
        byte[] bytes = s.getBytes();
        int i = 0;
        int j = s.length() - 1;
        while (i < j) {
            byte temp = bytes[i];
            bytes[i] = bytes[j];
            bytes[j] = temp;
            i++;
            j--;
        }
        return new String(bytes);
    }
}


//異或操作運算符
3.
public class Solution {
    public String reverseString(String s) {
        char[] word = s.toCharArray();
        int i = 0;
        int j = s.length() - 1;
        while (i < j) {
            word[i] = (char) (word[i] ^ word[j]);
            word[j] = (char) (word[i] ^ word[j]);
            word[i] = (char) (word[i] ^ word[j]);
            i++;
            j--;
        }
        return new String(bytes);
    }
}

4.
public class Solution {
    public String reverseString(String s) {
        byte[] bytes = s.getBytes();
        int i = 0;
        int j = s.length() - 1;
        while (i < j) {
            bytes[i] = (byte) (bytes[i] ^ bytes[j]);
            bytes[j] = (byte) (bytes[i] ^ bytes[j]);
            bytes[i] = (byte) (bytes[i] ^ bytes[j]);
            i++;
            j--;
        }
        return new String(bytes);
    }
}


//Java library
5.
class Solution {
public:
    string reverseString(string s) {
        s=string(s.rbegin(),s.rend());
        return s;
    }
};

6.
public class Solution {
    public String reverseString(String s) {
        return new StringBuilder(s).reverse().toString();
    }
}


//分治 遞歸
7.
public class Solution {
    public String reverseString(String s) {
        int length = s.length();
        if (length <= 1) return s;
        String leftStr = s.substring(0, length / 2);
        String rightStr = s.substring(length / 2, length);
        return reverseString(rightStr) + reverseString(leftStr);
    }
}


發佈了88 篇原創文章 · 獲贊 97 · 訪問量 52萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章