Leetcode_字符串string(2)12. 13. 14. 22

目录

 

LC_14 最长公共前缀

LC_13. 罗马数字转整数

LC_12 整数转罗马数字

LC_22. 括号生成


LC_14 最长公共前缀

61.01% 11.32%

class Solution {
public:
    int compare2(string a,string b){
	    int len=min(a.length(),b.length());
	    for(int i=0;i<len;i++){
		    if(a[i]!=b[i])
			    return i;
	    }
	    return len;
    }
    string longestCommonPrefix(vector<string>& strs) {
	    int l=strs.size();
        if(l==1) return strs[0];
        int maxn=1e9+7;
	    string res;
	    for(int i=1;i<l;i++){
		    int x=compare2(strs[i],strs[i-1]);
            if(maxn > x){
                maxn=x;
                if(x==0) return "";
			    res=strs[i].substr(0,x);
            }
	    }
	return res;
    }
};

LC_13. 罗马数字转整数

60.95% 63.95%

class Solution {
public:
    int romanToInt(string s) {
        map<char,int> mp;
	mp['I']=1; mp['V']=5; mp['X']=10; mp['L']=50;	
	mp['C']=100; mp['D']=500; mp['M']=1000;
	int len=s.length();
    if(len==1) return mp[s[0]];
	int res=0;
	for(int i=0;i<len-1;i++){
		int x1=mp[s[i]],x2=mp[s[i+1]];
		if(x1>=x2){
			res+=x1;
			if(i==len-2) res+=x2;
		}else{
            if(i==len-3) res+=mp[s[len-1]]; 
			res+=(x2-x1);
			i++;
		}	
	}
	return res;
    }   
};

LC_12 整数转罗马数字

87.45%,100.00%

class Solution {
public:
    string intToRoman(int num) {
    string roman[13]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
	int nums[13]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
	string res="";
	for(int i=0;i<13;i++){
		while(num>=nums[i]){
			res+=roman[i];
			num-=nums[i];
		}
	}
	return res;
    }
};
public class Solution {

    public String intToRoman(int num) {
        // 把阿拉伯数字与罗马数字可能出现的所有情况和对应关系,放在两个数组中
        // 并且按照阿拉伯数字的大小降序排列,这是贪心选择思想
        int[] nums = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String[] romans = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

        StringBuilder stringBuilder = new StringBuilder();
        int index = 0;
        while (index < 13) {
            while (num >= nums[index]) {            // 注意:这里是等号
                // 注意:这里是等于号,表示尽量使用大的"面值"
                stringBuilder.append(romans[index]);
                num -= nums[index];
            }
            index++;
        }
        return stringBuilder.toString();
    }
}

LC_22. 括号生成

给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。

例如,给出 = 3,生成结果为:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]
class Solution {
    public List<String> generateParenthesis(int n) {
        List<String> ans = new ArrayList();
        backtrack(ans, "", 0, 0, n);
        return ans;
    }

    public void backtrack(List<String> ans, String cur, int open, int close, int max){
        if (cur.length() == max * 2) {
            ans.add(cur);
            return;
        }

        if (open < max)
            backtrack(ans, cur+"(", open+1, close, max);
        if (close < open)
            backtrack(ans, cur+")", open, close+1, max);
    }
}
class Solution {
    public List<String> generateParenthesis(int n) {
        List<String> combinations = new ArrayList();
        generateAll(new char[2 * n], 0, combinations);
        return combinations;
    }

    public void generateAll(char[] current, int pos, List<String> result) {
        if (pos == current.length) {
            if (valid(current))
                result.add(new String(current));
        } else {
            current[pos] = '(';
            generateAll(current, pos+1, result);
            current[pos] = ')';
            generateAll(current, pos+1, result);
        }
    }

    public boolean valid(char[] current) {
        int balance = 0;
        for (char c: current) {
            if (c == '(') balance++;
            else balance--;
            if (balance < 0) return false;
        }
        return (balance == 0);
    }
}
发布了88 篇原创文章 · 获赞 77 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43107805/article/details/104856771