LeetCode 字符串(一)

6. ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:

string convert(string s, int numRows);
Example 1:

Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Example 2:

Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:

P     I    N
A   L S  I G
Y A   H R
P     I

 观察规律:

0  6  C
1 57 BD
24 8A E
3  9  F
0,6处于相同位置,和行数有关:Total = 2*(numRows- 1)

中间上升位置4,5,A,B规律为  j + 2*numRows-2 - 2*i    j表示列数,i表示行数
//这简直了 数学规律啊啊啊   参考了大神们的思路   虐心
class Solution {
public:
    string convert(string s, int numRows)
    {
        int nLen = s.size();
        if(numRows<=1)
        {
            return s;
        }
        string nResult = "";
        int num = 2*numRows-2;
        for(int i=0;i<numRows;i++)
        {//行
            for(int j = i;j<nLen;j+=num)
            {//列,上图 第一行  0 6 C
                nResult += s[j];
                int pTmp = j+2*numRows-2-2*i;
                if (i != 0 && i != numRows - 1 && pTmp < s.size())
                   {
                        nResult += s[pTmp];
                   }    
            }
        }
        return nResult;

    }
};

 

344. Reverse String

Write a function that reverses a string. The input string is given as an array of characters char[].

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

You may assume all the characters consist of printable ascii characters.

Example 1:

Input: ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]

思路:双指针进行字符交换

class Solution {
public:
	void reverseString(vector<char>& s)
	{//双指针
		int left = 0;
		int right = s.size()-1;
		while(left<right)
		{
			char c = s[left];
			s[left++] = s[right];
			s[right--] = c;
			//left++;right--; //指针移动
		}
	}
};

 657. Robot Return to Origin

There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.

The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.

Note: The way that the robot is "facing" is irrelevant. "R" will always make the robot move to the right once, "L" will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.

Example 1:

Input: "UD"
Output: true 
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.

思路:回到原点--> 一个U 对应一个D;一个L对应一个R;

也就是判断 两组(U与D,L与R)的个数;

class Solution {
public:
	bool judgeCircle(string moves)
	{
		int nCount1=0,nCount2=0;
		for(char c : moves)
		{
			if(c=='U')nCount1++;
			else if(c=='D')nCount1--;
			else if(c=='L')nCount2++;
			else if(c=='R')nCount2--;
		}
		//判断nCount1,nCount2是否同时为零
		if(nCount1==0 && nCount2==0)
			return true;
		else 
			return false;
		//return nCount1 && nCount2;
	}
};
//for(char c:moves)就是定义一个遍历字符c,让它分别等于字符串数组moves里面的各个字符,然后执行下面的语句,当c被赋值为moves里面所有字符各一次后,就会退出这个循环.

709. To Lower Case

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
Example 1:

Input: "Hello"
Output: "hello"

class Solution 
{
public:
	string toLowerCase(string str) 
	{
		if(str.size()<=0)
			return str;
		for(int i=0;i<str.size();i++)
		{
			char c = str[i];
			if(c>='A' && c<='Z')
			{
				c +=32;
				str[i] = c;
			}

		}
		return str;
	}
};
//大神的代码
class Solution {
public:
    string toLowerCase(string str) {
        for (char &c : str) 
        {//遍历字符 c 等于 str 每个字符
            if (c >= 'A' && c <= 'Z') c += 32;
        }
        return str;
    }
};

804. Unique Morse Code Words

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on.

For convenience, the full table for the 26 letters of the English alphabet is given below:

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cba" can be written as "-.-..--...", (which is the concatenation "-.-." + "-..." + ".-"). We'll call such a concatenation, the transformation of a word.

Return the number of different transformations among all words we have.

Example:
Input: words = ["gin", "zen", "gig", "msg"]
Output: 2
Explanation: 
The transformation of each word is:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值